Card: Nesting

CSS-Nesting

The Magic of Nested Styles

In the realm of CSS, a new, powerful spell has emerged: nesting! Once the exclusive domain of preprocessors like SCSS, this enchanting construct has now woven its way directly into the sacred halls of native CSS.

Nesting is like a spell that lets you neatly group rules for parent and child elements into a single block, eliminating the need for endless repetition. Imagine an army of CSS soldiers lined up in perfect order, each ready to follow the next command. No more chaos, no more tangled selectors—nesting brings clarity and structure to your layouts. But as with any potent magic, it must be used wisely to maintain balance and avoid descending into disarray.

Nesting with Style

Less repetition, more clarity – let your CSS grow logically!

Token: CSS Nesting &

The Power of &

The Magical Anchor

The & symbol is your wand in CSS nesting, serving as the anchor for the parent selector. It seamlessly connects nested rules to the original selector, saving you from tedious repetition.

You’ll need the & whenever working with pseudo-classes, pseudo-elements, or specific additions tied directly to the parent selector.

For regular child elements, you can skip it—CSS Nesting automatically maintains that connection. Think of the & as the invisible thread that keeps the magical circle of rules intact and perfectly aligned.

.container {
    background: lightgray;
    padding: 20px;

    /* No & Needed For normal child elements */
    ul {
        list-style: none;
        padding: 0;
    }

     /* & Needed For pseudo- classes 
     and pseudo-elements */
    & li:hover {
        color: darkblue;
        cursor: pointer;
    }
}
Token: CSS Nesting Nested Media Queries

Nested Media Queries

The Dimensional Spell

Nesting empowers you to include media queries seamlessly within your CSS rules. This allows your design to adapt gracefully to different screen sizes exactly where needed, without disrupting the flow of your stylesheet.

In this example, the font size and link color for .parent and its children are defined. As the screen becomes narrower, the media query activates, adjusting the font size and color accordingly—all in a clean, nested style.

.parent {
    font-size: 20px;

    & a {
        color: dodgerblue;
    }

    @media (max-width: 600px) {
        font-size: 16px;

        & a {
            color: coral;
        }
    }
}
CSS
/* Menu */
.menu {
    list-style: none;
    padding: 0;

    li {
        padding: 10px;
        border: 1px solid #3e4451;
        margin-bottom: 5px;
        border-radius: 5px;
        cursor: pointer;
        background: #4a4f5c;
        transition: all 0.3s ease;

        &:hover {
            background-color: #22a7f0;
            color: white;
        }

        /* Sub Menu */
        .submenu {
            list-style: none;
            margin-top: 10px;
            padding-left: 20px;
            display: none;

            li {
                padding: 8px;
                margin-bottom: 5px;
                border: 1px solid #3b3f47;
                border-radius: 5px;
                background: #3e4451;
                transition: all 0.3s ease;

                &:hover {
                    background-color: #9b59b6;
                }
            }
        }

        &:hover .submenu {
            display: block;
        }
    }
}
Scroll to Top