Pseudo Class in CSS

  • A pseudo-class in CSS is a keyword added to selectors that specifies a special state of the selected elements. Pseudo-classes let you apply a style to an element not only in relation to the content of the document tree, but also in relation to external factors like the history of the navigator (:visited, for example), the status of its content (like :checked on certain form elements), or the position of the mouse (like :hover, which lets you know if the mouse is over an element).
Here are a few commonly used CSS pseudo-classes:
  • hover: Applies styles when the user hovers over an element with the cursor.
  • focus: Applies styles when an element has received focus, either from the cursor or by tab navigation.
  • active: Applies styles when an element is being activated by the user (e.g., clicked on).
  • first-child: Targets the first child element within its parent.
  • last-child: Targets the last child element within its parent.
  • nth-child(): Targets elements based on a formula (like even, odd, or specific numbers).
Examples of Using Pseudo-Classes
  • Let's explore how these pseudo-classes can be used in CSS through some practical examples.
  • Example 1: :hover and :active


  button {
    background-color: lightblue;
    border: none;
    padding: 10px 20px;
    transition: background-color 0.3s;
  }

  button:hover {
    background-color: blue;
    color: white;
  }

  button:active {
    background-color: navy;
  }

  • In this example, a button changes color when a user hovers over it and becomes a darker shade when clicked.
  • Example 2: :first-child and :last-child


  ul li:first-child {
    font-weight: bold;
  }

  ul li:last-child {
    font-style: italic;
  }

  • Here, the first `<li>` element within any `<ul>` is bolded, and the last `<li>` is italicized.
  • Example 3: :nth-child


  p:nth-child(odd) {
    color: red;
  }

  p:nth-child(even) {
    color: blue;
  }

  p:nth-child(3n+1) {
    font-size: 20px;
  }

  • This snippet applies different styles to odd and even paragraphs, and every third paragraph starting from the first (`3n+1`) is given a larger font size.
Pseudo-classes are crucial for:
  • Enhancing User Experience: They make it possible to respond to user interactions, providing immediate feedback, such as highlighting a button when hovered or clicked.
  • Structural Targeting: Pseudo-classes like :first-child and :nth-child allow designers to apply styles based on the position of an element within its parent, without additional classes or ID markers.
  • Conditional Styles: Conditions such as :focus and :checked enable the styling of elements based on user interaction or input state without JavaScript.
  • By using pseudo-classes, developers can create more dynamic, responsive, and intuitive interfaces. They provide a powerful toolset for styling that adapates to both document structure and user interaction, enhancing both aesthetic qualities and functionality in web design.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...