Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS " :: " vs " : " -- pseudo-element vs pseudo-selector? [duplicate]

I'm researching css and typography, and ran into this intriguing concept of pseudo-selectors. I have used single colon psuedo selectors and am unfamiliar with the double colon version of the psuedo selectors. I understand that double colon is called a pseudo-element instead of a pseudo-selector - but why? And what is the difference?

I also understand that single colon is much more supported, so in what situation would one use the double colon pseudo-element? Are there use cases where double colon would be necessary, and single colon would not get the job done? what might that situation be?

"Unlike pseudo-elements, pseudo-classes can appear anywhere in selector chain." (quote from link) - I don't know what 'selector chain' is, but this also seems like yet another limitation of the double colon approach. Why would I need to use double colon if it is (in my understanding) just a less supported version of single colon?

edit: they appear not to be functionally the same: fiddle

<div><p>First Line</p></div> <div><p>Second Line</p></div> 

css

div:nth-child(1) > p {      color: green; }  div::nth-child(2) > p {      color: blue; } 
like image 521
J-Dizzle Avatar asked Apr 20 '15 17:04

J-Dizzle


People also ask

What are the differences between pseudo-element and pseudo class selectors?

Basically a pseudo-class is a selector that assists in the selection of something that cannot be expressed by a simple selector, for example :hover . A pseudo-element however allows us to create items that do not normally exist in the document tree, for example ``::after`.

When would you use the :: before or :: after pseudo-element in your CSS?

Special welcome offer: get $100 of free credit. CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What is VS :: in CSS?

Pseudo-elements ( :: ) allow you to style different parts of an element e.g. the first line, the first letter, inserting content before or after, etc. Originally they all used a single colon, but CSS3 introduced the double colon to separate the two. Follow this answer to receive notifications.

What is the Speciality of pseudo selectors compared to other selector forms?

Pseudo-classes are CSS classes used to define the state of an element. They target elements that can't be targeted with combinators or simple selectors like id or class. They are used to select elements based on their attributes, states, and relative position.

What is a pseudo class in CSS 3?

Pseudo-Classes Pseudo-classes are identified with a single colon according to the CSS3 specification. A pseudo-class is a selector that selects elements that are in a specific state. For instance, the specific state may be an HTML element that is clicked/hovered or an element that is the first child of its parent.

What are pseudo elements and pseudo selectors?

These are the pseudo elements or pseudo selectors that most CSS developers are familiar with. A CSS pseudo class is a pseudo selector that targets elements that match a certain criteria or state. And we'll talk about a few examples in just a moment. But a CSS pseudo class is signified by a single colon, followed by the name of the pseudo class.

What is a pseudo element in HTML?

Pseudo-elements behave in a similar way. However, they act as if you had added a whole new HTML element into the markup, rather than applying a class to existing elements. Pseudo-elements start with a double colon ::.

What is first line pseudo element in CSS?

A CSS pseudo-element is used to style specified parts of an element. The ::first-line pseudo-element is used to add a special style to the first line of a text. Note: The ::first-line pseudo-element can only be applied to block-level elements.


1 Answers

Pseudo-classes (:) allow you to style the different states of an element e.g. when you hover over it, when it is disabled, when it is the first child of its parent, etc.

Pseudo-elements (::) allow you to style different parts of an element e.g. the first line, the first letter, inserting content before or after, etc.

Originally they all used a single colon, but CSS3 introduced the double colon to separate the two.

like image 186
scronide Avatar answered Sep 18 '22 14:09

scronide