Could you explain the functions of :: and ~ in CSS?

After coming across a code snippet on Twitter, I saw the following:

div::after {
-webkit-transform: rotate(2deg);
}

div ~ div {
-webkit-transform: rotate(0deg);
}

Can someone explain what this code does?

Answer №1

In CSS3, the double colon was introduced to replace single-colon selectors for pseudo-elements. This change was made to clearly differentiate between pseudo-classes and pseudo-elements. While the single-colon syntax is still acceptable for pre-CSS3 selectors, it's recommended to use the double-colon syntax for newer implementations. For example, :after represents a pseudo-class, while ::after represents a pseudo-element.


A new selector known as the general sibling selector was added in CSS3, with the tilde character (~) used as the combinator. This selector allows you to target elements that are siblings of a specific element. For instance, you can match a p element if it's a sibling of an h2 element using this selector:

Answer №2

The tilde symbol (~) functions as the siblings selector

h2 ~ p { color:red; }

For instance, using the CSS above would turn the paragraphs red in the following code snippet:

<h2>Heading</h2>
<p>The selector above matches this paragraph.</p>
<p>The selector above matches this paragraph.</p>

In contrast, the double colons (::) are employed for ::before and ::after pseudo-elements. When combined with the content: property, you can insert an icon before each link, such as in the example below:

a::before { content:url(link.png); }

Answer №3

In CSS3, the :: symbol is utilized for defining pseudo elements.

The ~ in CSS3 represents the general sibling combinator, which is used to target elements that come after another element at the same level.

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

Is it possible to align two buttons side by side on a webpage using only HTML and CSS?

Can you help me figure out how to align two buttons next to each other with some space between them using only css and html? If you take a look at my html code and css code, you'll see that the two buttons are not aligned properly. * { margin: 0 ...

Can you tell me the name of this specific page arrangement style?

What is the term for the specific layout style used on Asana's homepage? This layout consists of a single, long page divided into sections that are approximately the size of the viewport. While sometimes it includes parallax scrolling, in this case i ...

Displaying article title above container with backdrop picture

Currently, I am working on a specific layout design using Bootstrap 5 where I am trying to render a tag outside its original container so that it takes up the full width of the page. My attempts with absolute positioning on the img tag have proven to be ch ...

"Encountered an issue with locating elements using the driver.find_element

I need assistance in automating the process of clicking on the "Create New Network" button using Selenium. <button type="button" id="dt-refreshBtn" class="btn wc-btn--link" data-label="Create New Network" role=&q ...

Ensure that the top of a div stays in place as the user scrolls, and spans the entire width of the div

My goal is to create a sticky or fixed position header inside a div with a list, but I'm facing an issue where it only sticks to the width of the text. If I expand the width to 100%, it takes up the size of the entire page: import styled from 'st ...

Utilize the id in AngularJS to bring attention to a specific row within a table

I am brand new to using angularjs. I currently have a table structured like this: HTML <table class="table table-striped" id="manageResumeTable"> <thead class="text-center text-info text-capitalize"> <th class="text-center col- ...

Tips on applying a class to a host element using @hostbinding in Angular 2

I've been wanting to assign a class to the host element of my component, and initially I used the host property in this manner: @Component({ selector: 'left-bar', host: { 'class': 'left-bar' }, templateUrl: 'a ...

How can I utilize jQuery to iterate through every anchor tag on an HTML page?

I am looking to reference all anchor tags on the page that have a parent h2 tag. To achieve this, I need to iterate through each anchor tag that has a parent h2 and add an attribute using jQuery. <body> <h1>not me</h1> <a href ...

The previous button on Owl Carousel seems to be malfunctioning after navigating from a different page

In my case, I have two distinct pages: let's call them the first and second pages. On the first page, I utilize an owl carousel slider with a tag that links to a specific slide on the second page's owl carousel slider using an ID. Meanwhile, on ...

At a specific point in the route, all CSS and JS links in Node.js vanish

Hey everyone, I'm facing a strange issue where the CSS styling and JS code are not loading when I access the route http://localhost:5000/posts/edit/<%=idofblog%>. As a result, my webpage looks very unattractive, and I'm not sure what's ...

Styling text using CSS depending on the displayed text

Utilizing Awesome Tables (AT), I am extracting data from a Google Sheets document to display on a website. The HTML template in the sheets is used for formatting the data, specifically focusing on the table output with inline CSS styling. Since the templat ...

Tips for preventing table cells from vertically overflowing and displaying the table without any issues

I have tried numerous solutions from various sources, but none seem to work for my specific issue. My HTML page consists only of a table that I want to print perfectly on a horizontal A4 sheet. Here is the table without any text (it is a match referee): ...

Is it possible to utilize the ternary operator to handle classnames when working with CSS modules?

I am experiencing difficulty implementing a styling feature using the ternary operator within an element's className. My goal is to have three buttons that render a specific component when clicked. Additionally, I want to change the background color ...

Mobile Windows Z-Index

Struggling with the z-index issue on a video tag in Windows Mobile, particularly when it comes to my search box. <div portal:substituteby='common/search::search'></div> The goal is for the search box to appear above the video. ...

When using the `position: absolute` and `left: 50%` properties on content with a `width: fit-content`, the content gets wrapped in Windows,

Why does the below code behave differently on Windows and Mac? On Windows, the content wraps or the div occupies only 50% of the browser's width, causing the content to wrap if its width exceeds 50% of the browser's width. However, on Mac, it tri ...

Is there a way to automatically scroll to the top on refresh or load without using Jquery in AngularJs or Js?

One issue I'm facing is trying to set the page to scroll to the top when it loads. However, Chrome, IE, and Firefox are currently resetting the scroll position to where it was on the previous page. How can I achieve this? It appears that scrollTo doe ...

I am encountering an issue where my input is moving to the following line after the initial one. What could be

Trying to create a layout I like for this form has been a bit of a struggle. I thought I had something good at first, but after the first row of input, the formatting gets all messed up and I can't figure out what's going wrong. <!DOCTYPE htm ...

Is it possible to implement Photoshop-inspired color curves filters on an HTML tag?

I am attempting to recreate the color curves filter from Photoshop using an HTML video tag. https://i.stack.imgur.com/erB1C.png The solution closest to what I need so far is how to simulate Photoshop's RGB levels with CSS and SVG Filters, but it doe ...

Adding gaps between hyperlinks in the sidebar navigation using Bootstrap 4

I created a sidebar containing links and I am attempting to add space between each link. Here is the code snippet I have tried: .sidebar-nav>li>a{ padding-bottom: 500px; } Unfortunately, this code is not producing the desired result. Are there any ...

Recreating a CSS3 animation for Internet Explorer 9

While I am aware that CSS3 animations do not function properly in IE9 and below, I have managed to create a transition effect for a header that becomes fixed and shrinks in modern browsers with the following CSS: /** This triggers when the viewport is scr ...