Thumbnail for pseudo-classes episode
4 minutes

Pseudo-classes Unveiled

Greetings, fellow sorcerers of style! Today, we′re venturing deep into the enchanting world of CSS pseudo-classes—those magical selectors that bring life and vibrancy to your web spells. Get ready for a comprehensive tour, where each pseudo-class is a chapter in the book of web wonders.

Pseudo-classes in CSS are special keywords that allow you to select and style elements based on their state or position within the document. They add a dynamic aspect to your styles, enabling you to respond to user interactions, structural relationships, or certain conditions. Let′s delve deeper into the details of pseudo-classes and explore a few examples:

The Hovering Ballet - :hover:

Behold the graceful ballet of the web with the legendary :hover pseudo-class! Like a gentle waltz, it lets you adorn elements with style when users dance their cursor over them:

a:hover {
  color: #ff4500; /* Transform text to orange on hover */
}

Feel the magic as hyperlinks twirl into a new dimension of color when touched by the user′s cursor.

The Visited Chronicles - :visited:

Embark on a journey through the visited pages with the :visited pseudo-class. It styles links that have been visited, adding a touch of history to your styles:

a:visited {
  color: #8e44ad; /* Paint visited links in a regal purple */
}

See as visited links don the regal color of history, creating a visual diary within your web pages.

The Focus Spotlight - :focus:

Now, direct your attention to the spotlight with the mesmerizing :focus pseudo-class. It’s the star of the show when an element gains keyboard focus:

input:focus {
  border: 2px solid #27ae60; /* Add a border when input
                                is in the spotlight */
}

Witness as input fields step into the spotlight, adorned with a captivating border when they take center stage.

The First-child Grandeur - :first-child:

Step into the regal world of the :first-child pseudo-class. It crowns the first child of a parent element with grandeur:

li:first-child {
  font-weight: bold; /* Make the first list item bold */
  color: #4caf50; /* Paint it green for extra emphasis */
}

As expected, there is also a :last-child pseudo-class that selects the last child of a parent element.

The Odd and Even Illusion - :nth-child(odd/even):

Embark on a dance of odd and even with the rhythmic :nth-child pseudo-class. It weaves a tapestry of background colors for alternate list items:

li:nth-child(odd) {
  background-color: #f2f2f2; /* Give odd list items a subtle background */
}

Feel the beat as odd list items groove to a subtle background rhythm, creating a visually appealing pattern. If you prefer even items, just replace odd with even.

The Checked Carnival - :checked:

Join the carnival of user interaction with the dynamic :checked pseudo-class. It transforms checkboxes into golden wonders when selected:

input[type='checkbox']:checked {
  accent-color: #ffd700; /* Turn the checkbox gold when checked */
}

Witness the magic as checkboxes don a golden hue, adding a touch of splendor to user selections.

The Rebel Alliance - :not() Pseudo-class:

Meet the rebels of the selector world—the :not() pseudo-class. It defies conformity, targeting elements that do not match a specified selector:

input:not([type='submit']) {
  border: 1px solid #e74c3c; /* Add a border to all inputs
                                except submit buttons */
}

Join the rebellion as certain input elements break free from the norm, adorned with rebellious borders. Note that checkboxes and radio buttons won’t get any border either.

The Nth Marvel - :nth-of-type():

Enter the marvel of position-based styling with the :nth-of-type() pseudo-class. It lets you target elements based on their position within a parent, considering only elements of the same type:

p:nth-of-type(2) {
  color: #9b59b6; /* Style the second paragraph within a parent */
}

Watch as the second paragraph steals the show with a unique color, standing out within the parent′s embrace.

The Empty Canvas - :empty:

Discover the simplicity and power of the :empty pseudo-class. It targets elements that have no children, offering a canvas for your magical touch:

div:empty {
  display: none; /* Hide empty div elements,
                    a blank canvas for magic yet to come */
}

Witness as empty divs gracefully fade into the background, leaving space for the next enchantment.

As we close this spellbinding journey, you′ve mastered the magic of pseudo-classes. May your web spells be dynamic, interactive, and endlessly enchanting. Until our paths cross again, happy coding and may your styles be ever magical! ✨🧙‍♂️

Photo of Woody Kelly on Unsplash