Quiz

Describe pseudo-elements and discuss what they are used for.

Topics
CSS

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). They can be used for decoration (::first-line, ::first-letter) or for adding elements to the markup (combined with content: ...) without having to modify the markup itself (::before, ::after).

  • ::first-line and ::first-letter can be used to decorate text.
  • Used in the .clearfix hack to add a zero-space element with clear: both.
  • Triangular arrows in tooltips use ::before and ::after. This encourages separation of concerns because the triangle is considered part of styling and not really the DOM.

Notes

  • Pseudo-elements are different from pseudo-classes, which are used to style an element based on its state (such as :hover, :focus, etc.).

References

Edit on GitHub