What could be the reason for this CSS selector not functioning as anticipated?

What is the reason that the text "Won" is not turning red in the code snippet below? The CSS selector #sisters>*~#too should target all elements that come before the element with ID #too, including the element with ID #won. However, only the element with ID #too is being styled by this selector.

To summarize, Why is the preceding sibling #won not being selected by the CSS rule #sisters>*~#too?

<style>
#sisters>#too~*,
#sisters>*~#too {
    color: red;
}
</style>
<div id="sisters">
    <div id="won">Won</div>
    <div id="too">Too</div>
    <div id="tree">Tree</div>
    <div id="fore">Fore</div>
    <div id="jive">Jive</div>
</div>

See Working Example: http://jsfiddle.net/Supuhstar/XY4Dg/

Answer №1

In CSS, it is not possible to select elements in an ascending manner. You can only select descendant and subsequent elements. For example, with the following selector:

#sisters>*~#too 

You are essentially looking for:

  • All elements with the ID too
    • That are siblings ~
      • Of any direct children >*
        • Within a tag with the ID of sisters

This means that CSS selectors always choose the last item specified in the selector that meets all the previous criteria.

Answer №2

The tilde character, referred to as the General Sibling Combinator in CSS, is used to select any sibling that appears after the element on the left side of it. However, there currently isn't a selector available in CSS that allows for selecting preceding elements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Menu collapse button failing to Show content

My navigation menu button is not functioning correctly. I am currently working on this project using CodePen, so Bootstrap is already integrated into the code. <div class="navbar-header"> <button type="button" class="btn navbar-toggl ...

The rules for prioritizing CSS selectors

Struggling with CSS selector rules and specificity? I've already checked this for specifics. Even tried using firebug and inspecting elements in Chrome to copy the CSS path, but still can't get it to work. This is my first time creating a website ...

Persistent JavaScript animation loop troubles

Hey there, I'm new to web development and I've been working on a simple webpage with a text animation that moves from the top of the screen to the middle when the page loads. The animation itself is working fine, but I'm having trouble stopp ...

Tips for positioning a Navbar in the middle of the screen at 90% width

I am struggling to center the navbar as a whole, rather than just the contents within it. Below is the CSS and HTML for my navbar. .navbar { border-radius: 10px 10px 30px 30px; background: #3CE18F; overflow: visible; position: fixed; bottom: ...

Designing borders for tables

My current table styling approach is as follows: #content table { width: 100%; margin-top: 1em; border-collapse: collapse; border: 1px solid #222; } #content table td { border: 1px solid #888; padding: .3em; } I aim to create tab ...

Tips on creating responsive email designs

What is the best way to make emails responsive in a php application when external css files are not supported and media queries cannot be included inline? Looking for alternatives to make emails responsive without external css files and media queries, any ...

Mastering the art of CSS float and positioning techniques

Struggling with CSS positioning yet again. I'm trying to design a webpage with one main element in the center, surrounded by 10 others. The challenge is to maintain consistency across different screen sizes (excluding mobile). However, every time I r ...

What could be causing the issue with object-fit not functioning properly within a container with a max

My goal is to insert a video into a container that has a width of 100% and automatically adjusts its height while maintaining the aspect ratio, but with a maximum height set. I want the video to completely fill the container even if some parts are cropped ...

As I populate my mobile content, background images are being stretched out

I'm currently in the process of enhancing my website by incorporating background images using the background property. My goal is to ensure that each image fills the width and height of its container without sacrificing quality or aspect ratio. Here i ...

What are some methods for equalizing the height of images in a flexbox layout when I am unsure of their individual dimensions?

I'm looking to create 2 images with different widths that are displayed at the same height side-by-side, preferably using a flex box. The method below works if I have the image dimensions (962x706 and 962x1275). Is there a way to achieve this withou ...

What is the proper syntax for creating a layout with DIV and CSS?

Is there a way to create a master page for asp.net that includes 3 sections using div elements? I want to split the window into a left pane for tree view navigation, a top banner type div, and a main content window under it where child pages will be loaded ...

Menu with option to delete next to each selection item

My task is to create a drop-down menu with a delete 'X' option next to each item. This functionality should work even when the drop-down is dynamically populated, without resorting to using a list as an alternative. UPDATE: The icons next to eac ...

Preventing the element from breaking when it is not the child: Tips and Tricks

In order to align both .quote-container and #new-quote elements in the same line, even when the window width is very small (e.g., 83 pixels), I used min-width on the .quote-container element successfully. However, applying the same technique to the #new-qu ...

What is the best way to align a form in the center of a page both horizontally and vertically

Currently, this is the HTML code I am working with: <body> <form id="form_login"> <p> <input type="text" id="username" placeholder="username" /> </p> ...

What's the best way to determine the background image on this website? I'm in the process of replicating a site on

I have been asked to clone the website in order to create a Wordpress site. The original site is currently on Kajabi platform. I managed to download all images from the Kajabi site by right-clicking and selecting download. However, there are certain image ...

Position SVG element within a Bootstrap 4 row

Is there a way to center align both an icon (<svg>) and text within a container with a small spacing between the two elements? | ICON TEXT | Here is a JSBin link for reference: https://jsbin.com/tatomas/edit?html,css,output I have Bootst ...

How to Align Bootstrap 4 Navbar Brand Logo Separate from Navbar Toggler Button

Can anyone help me with center aligning navbar-brand? When I right align navbar-toggler for mobile devices, it causes the logo to be off-center. Is there a way to independently align these two classes, and if so, how can I achieve this? <nav class="n ...

Captions in Bootstrap 4 carousel vanish when adjusting size

My Bootstrap 4 carousel is displaying GIFs with captions for different project features. The captions work well on medium/large screens, but disappear entirely on smaller screens. How can I ensure the captions remain visible on smaller screens? <div ...

Tips for positioning elements on an HTML page by utilizing CSS styling

I have a primary container with the following CSS: .major { width:30%; height:100%; position:absolute; } Now, I want to insert web information at the bottom part which includes: About | Terms | Policies | Contact Us This content will be within the .web ...

What is the best way to center the content vertically within flex items?

I am working with a flexbox header that has two child flex items, each with their own children. Is there a way to change the vertical alignment of the flex items so that their children are aligned at the bottom? div.flexbox { display: flex; align ...