Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between pseudo-classes and pseudo-elements?

What is the difference between div::after {} and div:after {} ? When do we have to use :: over :?

Double colon and single-colon notation is to distinguish between pseudo-classes and pseudo-elements.

What is the actual meaning of the above statement?

like image 310
Byaku Avatar asked Jan 26 '17 05:01

Byaku


People also ask

What is a pseudo-element?

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph.

How W3C differentiate between pseudo-classes and pseudo-elements in CSS3?

The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt from W3C to distinguish between pseudo-classes and pseudo-elements. The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.

What are pseudo-classes?

Overview. CSS pseudo-classes are used to add styles to selectors, but only when those selectors meet certain conditions. A pseudo class is expressed by adding a colon (:) after a selector in CSS, followed by a pseudo-class such as "hover", "focus", or "active", like this: a:hover { /* your style here */ }

Can pseudo-elements and pseudo-classes be combined?

There are no special rules around combining pseudo-classes and pseudo-elements, besides the one rule that says there can only be one pseudo-element per complex selector and it must appear at the very end.


2 Answers

From https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Pseudo-classes_and_pseudo-elements

Pseudo-class :

A CSS pseudo-class is a keyword, preceded by a colon (:), added to the end of selectors to specify you want to style the selected elements, and only when they are in certain state. For example, you might want to style an element only when it is being hovered over by the mouse pointer, or a checkbox when it is disabled or checked, or an element that is the first child of its parent in the DOM tree.

Examples:

  • :active
  • :checked
  • :nth-child()
  • :first
  • :hover

Pseudo-elements ::

Pseudo-elements are very much like pseudo-classes, but they have differences. They are keywords, this time preceded by two colons (::), that can be added to the end of selectors to select a certain part of an element.

Examples:

  • ::after
  • ::before
  • ::first-letter
  • ::first-line
  • ::selection
  • ::backdrop

As stated by @stephanmg:

In practice ::before is used as :before and ::after is used as :after because of browser compatibility. Both are pseudo-elements, but may look like pseudo classes. This might be confusing if you read CSS code.

like image 200
Bas van Dijk Avatar answered Oct 16 '22 10:10

Bas van Dijk


Pseudo-classes : it is applied automatically by the browser depending on the position of the element or its interactive state.

For Example :

E:hover Matches elements of type E when the cursor is hovering over it.

Pseudo-elements : It is applies styles to content based on its position in the HTML hierarchy.

For Example :

E::first-letter This applies a style to the first letter of the first line inside a block-level element E.

So ,

The CSS3 Selectors specification prefixes pseudo-elements with two colons instead of one. So, :first–letter becomes ::first-letter and :first-line becomes ::first-line. IE 8 and earlier don’t understand the double-colon prefix, so you need use the single-colon versions to avoid styles breaking in older browsers.

like image 25
Ehsan Avatar answered Oct 16 '22 09:10

Ehsan