What are pseudo-classes and pseudo-elements

Pseudo-classes and pseudo-elements are keywords used in CSS that allow you to style HTML elements based on their state or specific parts of an element.

What are Pseudo-Classes?

Pseudo-classes are used to define the special states of an element. They allow you to style elements based on their interaction with users or their position in the document tree. Common examples include:

  • :hover - Applies when the user hovers over an element.
  • :focus - Applies when an element has focus (such as a text input).
  • :nth-child() - Applies styles to elements based on their position in a parent.

What are Pseudo-Elements?

Pseudo-elements allow you to style specific parts of an element. They help in targeting sub-elements that are not directly accessible in the HTML. Examples include:

  • ::before - Inserts content before the content of an element.
  • ::after - Inserts content after the content of an element.
  • ::first-line - Styles the first line of a block element.

Example of Pseudo-Classes and Pseudo-Elements

<style> a:hover { color: red; } p::first-line { font-weight: bold; } </style> <a href="#">Hover over me!</a> <p>This is a paragraph that demonstrates the first line styling.</p>

Pseudo-Classes Pseudo-Elements CSS Styling :hover :focus ::before ::after