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?
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?
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:
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.
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:
ul li
- Looking inside - Selects all the li
elements nested within any ul
; Descendant combinatorul > li
- Looking inside - Selects only the direct children li
of ul
; Child combinatorul + ul
- Looking alongside - Selects the ul
that immediately follows another ul
; It focuses on adjacent siblings rather than descending elements; Adjacent sibling combinator / Next-sibling combinatorul ~ ul
- Looking alongside - Selects all following ul
's that share the same parent; General sibling combinator / Subsequent-sibling combinatorThe focus here is on the General sibling combinator / Subsequent-sibling combinator
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.
Here are some more detailed examples based on the initial response.
In essence, using the symbol ~
allows you to:
.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:
.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 siblingsCurrently, 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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Is there an option to display a flag icon? For example, I am able to write Hello ...
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 ...
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 ...
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 ...
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 ...
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); ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...