Card: Inheritance

Inheritance

The beloved family ties

In CSS, inheritance is like a chaotic family reunion where everyone is somehow connected. Parents pass their styles down to their children, children pass them on to their descendants, and suddenly everyone in the room is wearing the same color and font — whether they like it or not. It’s as if the clan elders have decided that everyone must wear the same robe.

But it’s not just about parents and children. There are also siblings, who can influence each other by appearing next to or after their relatives. If you want to get particularly creative, you can give specific relatives individual styles while the rest stick to tradition.
All this ensures that your webpage looks like one big, happy family.

Relationships in the family

so that you address everyone correctly

Token: inherit

inherit

The Passing Down of Family Bonds

With the inherit spell, an element proudly adopts the traits of its parents, as if declaring: “If my father looks like this, then so do I!”

p {
    font-size: inherit;
}

This spell ensures that all paragraphs inherit the font size of their ancestors—like a well-preserved family tradition.

Token: General Descendants

General Descendants

The Far-Reaching Ancestor Spell

The general descendant selector is like the wise elder at the family reunion, ensuring that all descendants adopt the same style. Whether high up in the family tree or hidden deep in its branches, no descendant escapes its influence.

.parent p {
    color: red;
}

A simple space in CSS, and the entire lineage of paragraphs glows in magnificent red.

Token: Child Selector

Child Selector >

The Loving Parent’s Gaze

The child selector is like the strict aunt who only cares for her immediate children. No attention is given to distant relatives—only the direct offspring of an element is affected by this precise spell.

.parent>p {
    color: dodgerblue;
}

This spell colors all direct child paragraphs of a parent element in red.

Token: Adjacent Sibling

Adjacent Sibling Selector +

The Eternal Sibling Rivalry

Siblings can influence each other—just like in any family. The adjacent sibling selector lets you connect directly neighboring elements with a magical touch.

h2+p {
    background-color: black;
}

Here, you color the next paragraph after an h2 in black—because siblings sometimes need to stand out with their colors.

Token: General Sibling Selector

General Sibling Selector ~

The Distant Cousins

Sometimes, it’s not just about direct siblings, but also all the cousins who show up at the family gathering. The general sibling selector works on all siblings, no matter how many elements stand between them.

h3~p {
    color: red;
}

One spell, and all paragraphs following an h3 glow in red—no matter how far apart they are.

Token: Grandchildren

Grandchildren

The Magical Grandkids That Don’t Exist

In CSS, there are spells for parents, children, and siblings, but what about the poor grandchildren? Unfortunately, there’s no specific spell just for them. The child selector cares for direct children, while the descendant selector affects all descendants. But for grandchildren? You have to make do with the existing spells. Perhaps one day, CSS wizards will invent a “grandparent spell,” but until then, it remains an unfulfilled wish!

CSS
.parent p {
    color: red;
}

.child>p {
    color: lightblue;
}

.parent p+p {
    color: orange;
}

.parent h1~p {
    color: green;
}
Scroll to Top