Can you explain the significance of the "~" symbol in CSS selectors?

Locating the ~ symbol can be quite challenging. While reviewing some CSS code, I came across this:

.check:checked ~ .content {
}

I'm curious about its significance. Can anyone shed some light on it?

Answer №1

The selector using a tilde (~) character is known as the subsequent-sibling combinator, previously referred to as the general sibling combinator before 2017:

The subsequent-sibling combinator consists of the "~" character that separates two sets of simple selectors. These selectors represent elements with the same parent in the document tree, where the element from the first set comes before (not necessarily directly) the element from the second set.

Here's an example to illustrate this concept:

.a ~ .b {
  background-color: powderblue;
}
<ol>
  <li class="b">b</li>
  <li class="a">a</li>
  <li class="x">x</li>
  <li class="b">b</li>
  <li class="b">b</li>
</ol>

The rule .a ~ .b targets the 4th and 5th list items because they fulfill the following criteria:

  • They are elements with the class "b"
  • They are siblings of elements with the class "a"
  • They appear after elements with the class "a" in the HTML source order

Similarly, .check:checked ~ .content selects all elements with the class "content" that are siblings of elements with the class "check" when they come after it.

Answer №2

It's beneficial to explore the various combinators within the CSS selector family and delve deeper into the specifics of each one.

  • ul li
  • ul > li
  • ul + ul
  • ul ~ ul

Here is an example checklist:


The focus here is on the General sibling combinator / Subsequent-sibling combinator

Answer №3

Understanding the general sibling combinator

The concept of the general sibling combinator selector closely resembles that of the adjacent sibling combinator selector. The main distinction lies in the fact that the selected element does not necessarily have to immediately follow the first element, but can be located anywhere after it.

Find out more

Answer №4

Here are some more detailed examples based on the initial response.

In essence, using the symbol ~ allows you to:

  • Select multiple adjacent siblings
  • Select multiple sibling groups within the same tag
  • Select multiple instances throughout the document

.a ~ .b {
  background-color: powderblue;
}
<ul>
  <li class="a">1st</li>
  <li class="b">2nd</li>
  <li class="c">3rd</li>
  <li class="a">4th</li>
  <li class="b">5th</li>
  <li class="b">6th</li>
</ul>
<ul>
  <li class="a">1st</li>
  <li class="b">2nd</li>
  <li class="c">3rd</li>
  <li class="a">4th</li>
  <li class="b">5th</li>
  <li class="b">6th</li>
</ul>

On the other hand, using the symbol + allows you to:

  • Select exactly one adjacent sibling
  • Select multiple sibling groups within the same tag
  • Select multiple instances throughout the document

.a + .b {
  background-color: powderblue;
}
<ul>
  <li class="a">1st</li>
  <li class="b">2nd</li>
  <li class="c">3rd</li>
  <li class="a">4th</li>
  <li class="b">5th</li>
  <li class="b">6th</li>
</ul>
<ul>
  <li class="a">1st</li>
  <li class="b">2nd</li>
  <li class="c">3rd</li>
  <li class="a">4th</li>
  <li class="b">5th</li>
  <li class="b">6th</li>
</ul>

Put simply...

  • + selects one adjacent sibling
  • ~ selects all adjacent siblings

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

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Is it possible to select a specific side to align with in CSS? If not, will this feature be available in the future?

When it comes to positioning in CSS, there are a couple of things to consider. Firstly, with the use of absolute, specifying right:33px; will create space between the right side of the div and its parent's right side. Is there a way to dictate which ...

Changes in content result in modifications to the size of transition elements

Have you ever encountered a button with changing text upon being pressed? The transition in text leads to a shift in the button's width. Wouldn't it be amazing if one could smoothly transition the change in width of an element using a code snippe ...

JavaScript bug: receiving an 'undefined' error when using getElementsByClassName

How can I retrieve the children of a specific div that belongs to the "vid_div selected" class? First, I select the correct div using this code: var vid_div = document.getElementsByClassName('vid_div selected'); However, when attempting to acces ...

Two vertical containers dynamically adjusting their height together

I am facing a challenge with two containers that need to share the height of their parent element. The second container is added dynamically when needed, and both containers may contain a lot of content, requiring scroll capabilities. While I have a solut ...

Using JavaScript to dynamically alter element class in Django templates

Within my django template, there is a list of Event items displayed using a for loop. Each event includes a date_of_occurrence attribute that holds a python DateTime value. My goal is to highlight events with a red background if the date is in the past rel ...

Emojis representing flags displayed on screen

Is there an option to display a flag icon? For example, I am able to write Hello ...

What is the reason that columns do not float in Bootstrap when there are no rows?

I have been doing some research into Bootstrap and have come across the following resources: What is the purpose of .row in Bootstrap? and Despite reading these links, I am still struggling to understand why my columns do not float left as expected in th ...

What is the best way to incorporate styling and structure into a jQuery plugin?

I have created a custom jQuery plugin that enhances table operations, similar to a grid plugin. The plugin applies styles such as overflow:hidden; to cells and adds borders to the table and its cells. To ensure consistent border widths, I included the fol ...

The accuracy of getBoundingClientRect in calculating the width of table cells (td)

Currently, I am tackling a feature that necessitates me to specify the CSS width in pixels for each td element of a table upon clicking a button. My approach involves using getBoundingClientRect to compute the td width and retrieving the value in pixels (e ...

Enhancing your images with a light box effect

Struggling with CSS and JQuery, I'm attempting to integrate Magnific Popup into my app. Utilizing the https://github.com/joshuajansen/magnific-popup-rails, I find that the code I insert into the view is displaying on the page directly. After following ...

Adjust the position of all content below when the specific div slides down

I am attempting to create a notification div that smoothly slides down when active. The sliding effect is achieved through this script: <script type="text/javascript> $(document).ready(function(){ $('#notice').slideDown(1000); ...

css using argb for opacity

I am attempting to apply a watermark (string) with some opacity (css attribute) to both an image and a pdf using itext. For the pdf, I can simply use: PdfGState gstate = new PdfGState(); gstate.FillOpacity = textOpacity; Everything works fine. However, ...

Troubleshooting problem with responsive image display and blurred effects

Initial problem I have a photo gallery on my website. When you click on a photo, you can view a larger preview. To cater to mobile users, I set the following CSS: .overview img { height: auto; width: 90%; } However, I am facing an issue where the hei ...

Ways to interact with a button that toggles between states

I am working with a button that has both an enabled and disabled state. Enabled State: <button type="button" class="btt-download-csv site-toolbar-menu-button icon-download no-icon-margins ng-isolate-scope" title="Download CSV" rc-download-csv="" curren ...

The SVG image exceeds the boundaries of the parent column div in bootstrap

I am struggling to make a SVG fit inside a column in a bootstrap div with col-lg-4. I tried using img-fluid, but it did not work as expected. The SVG should automatically adjust its size to fit the parent div. However, that is not happening. .star-ratin ...

Seems like JavaScript's .active functionality is failing to function properly in my case

I'm encountering an issue with my website's homepage. I have a list of services displayed, and the intention is for a description to appear when one of the services is clicked. However, clicking on the buttons does not trigger the expected action ...

The CSS file fails to connect with the HTML document

As I delve into the world of web design, I've encountered a simple issue that has me stumped. I created a basic example page with both documents residing in the same folder. Oddly enough, the snippet works perfectly when viewed here, but fails to func ...

coming to a halt at a specific location near the bottom

I've searched through numerous threads on stackoverflow regarding this issue and attempted various solutions using jQuery, HTML, and CSS. Unfortunately, none of them seem to be working as expected for my situation. Can someone lend a helping hand? He ...

Challenges faced when using Selenium with Telerik RadMenu

For the past two days, I've been struggling to find an effective method for writing integration tests for my ASP.NET website using Selenium. The website features a Telerik RadMenu with a hierarchical structure like this: Root Menu - Menu 1 - Sub M ...