
Pseudo-Classes
The Secret Tricks of CSS Sorcerers
Welcome to the realm of pseudo-classes, the invisible guardians of your elements. They allow you to read between the lines of your HTML, granting you power to whisper subtle commands to your elements. Imagine telling an element, “Hey, you there! Only do something special when you’re the third child!”
With pseudo-classes, you master this secret trick of CSS magic. They function like unseen spells, animating your website without lifting a finger. From hovering effects to selecting specific elements in a sequence, they offer limitless possibilities to refine your designs.
Embrace this mystical power and turn your website into a truly magical experience!
Pseudo-Classes
The secret power of selectors

:nth-child
Parents’ Favorite
With :nth-child, you can target any element at a specific position within a group of siblings. Imagine a line of rabbits sitting on a meadow, and you want only the third one to stand up and hop around. With :nth-child(3), it works as if you’ve waved a magic wand! This level of precision gives you control over your designs with elegance and ease.
.bunny:nth-child(3) {
color: brown;
}
The .bunny element in the third position is styled with a brown color.

:nth-child(odd) / (even)
Odd and Even Magic
With odd and even, counting becomes enchanting. You can grant a spell to every bunny in an even position like 2, 4, or 6, or highlight those in odd spots like 1, 3, and 5. Even the largest rows are effortlessly organized, as if each bunny instinctively knows when it’s their turn!
.bunny:nth-child(odd) {
color: black;
}
All odd .bunny elements are colored black.

:first-child
The Eldest Child Gets the Crown
:first-child is the favorite, the eldest in the family. It often receives special treatment. This pseudo-class applies your magic only to the first element in a group. The other siblings? They just watch in envy.
.bunny:first-child {
color: gold;
}
The first .bunny element is highlighted in gold.

:last-child
The Youngest Child that Gets It All
:last-child is the youngest in the family, often treated as the special one. It’s always the last element of its parent and deserves its own spell. No matter how many siblings there are, :last-child stands as the baby of the group, receiving its own magical touch.
.bunny:last-child {
color: white;
}
The last .bunny element is styled with white text color.

:nth-of-type
Birds of a Feather Flock Together
While :nth-child counts all the children of a parent, :nth-of-type focuses solely on siblings of the same type. It’s like searching only for the dogs in a house full of pets, ignoring how many cats and hamsters are around.
.bunny:nth-of-type(2) {
color: white;
}
The second .bunny element of the same type (e.g., <div>, <p>) is styled with white text color.

:not
The Exclusion Spell
Sometimes, you want to enchant everything on the page except for one thing. Enter :not. With this spell, you can declare: “Do everything, but leave this one item untouched!” This keeps your magic flexible without neglecting the rest.
.bunny:not(.baby) {
color: pink;
}
All .bunny elements that do not have the .baby class are styled with a pink background color.

:is
The Mighty Yet Elegant
Sometimes, you want to tackle multiple tasks at once because there’s just no time to handle them individually. Enter :is(), your trusty ally. Imagine you want to style a group of elements all at once. Instead of addressing each one separately, :is() steps in and says, “No problem, I’ve got them all covered.” It’s like being a magician, but in reality, :is() is the one doing all the work!
.parent :is(p.bunny, h3.bunny) {
color: white;
}
This CSS rule will style all p elements with the .bunny class and all h3 elements with the .bunny class inside .parent, turning their text color to white.

:has
The Most Vigilant of All
Ah, and here we have :has(), the guardian. :has() isn’t like the others that simply follow orders. No, :has() looks around, raises an eyebrow, and asks, “Are the conditions met?” Only when :has() confirms the presence of certain elements does it spring into action. Want an element to react only when another is nearby? Then :has() is your clever agent, taking care of the essentials.
.parent:has(.flower) {
border: 3px solid orange;
}
If .parent contains a child element with the class .flower, it receives an orange border.

:where
The Humble Companion
:where() is like that quiet ally who works diligently behind the scenes without seeking any recognition. It performs all the tasks that :is() can, but does so as if it wasn’t even there. You assign a task to :where(), and it gets to work, leaving space for others to step in with their ideas later. It’s like offering a suggestion while humbly letting others take the lead if needed.
:where() is friendly, supportive, and, above all, modest – it doesn’t want to impose specificity, ensuring that other rules with higher importance can take precedence. It’s the kind of CSS helper you didn’t know you needed, but one you’ll always appreciate.
.parent :where(p.bunny) {
color: white;
}
This rule ensures that all
elements with the class .bunny inside .parent are styled with a white color, while :where() keeps the rule from increasing specificity, allowing other styles to override it if needed.