Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
& symbol references the parent selector..card { }
.card .title { }
.card .content { }
.card:hover { }
.card button { }
.card button:hover { }.card {
.title { }
.content { }
&:hover { }
button {
&:hover { }
}
}.card {
background: white;
padding: 20px;
/* Nested selector for .card h2 */
h2 {
color: blue;
margin-bottom: 10px;
}
/* Nested selector for .card p */
p {
color: gray;
}
/* Nested selector for .card button */
button {
background: blue;
color: white;
}
}โจ Elements nested inside will automatically become descendants
Class Nesting
.card {
.header { }
.content { }
}ID Nesting
#main {
#sidebar { }
#content { }
}Multiple Levels
.nav {
ul {
li {
a { }
}
}
}The & symbol represents the parent selector. Essential for pseudo-classes and modifiers!
.button {
background: blue;
color: white;
/* &:hover = .button:hover */
&:hover {
background: darkblue;
}
/* &:focus = .button:focus */
&:focus {
outline: 2px solid blue;
}
/* &.active = .button.active */
&.active {
background: green;
}
/* &--large = .button--large (BEM) */
&--large {
padding: 20px;
font-size: 18px;
}
}Pseudo-classes
&:hover { }
&:focus { }
&:active { }Pseudo-elements
&::before { }
&::after { }State modifiers
&.active { }
&.disabled { }BEM modifiers
&--primary { }
&--large { }Compound selectors
&[disabled] { }
&[type="text"] { }<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div class="card">
<div class="header">
<div class="icon">๐จ</div>
<h2>CSS Nesting</h2>
</div>
<div class="content">
<p>
This card is styled with <strong>native CSS nesting</strong>!
No Sass, no build tools needed.
</p>
<p>
All the nested selectors, hover states, and even
<strong>dark mode</strong> support work natively in browsers.
</p>
</div>
<button>Learn More</button>
</div>
</body>
</html>Loading preview...
&:hover, &.active, etc.h2 { }, .header { }โ Works
.card {
&:hover { } /* GOOD */
h2 { } /* GOOD */
}โ Wrong
.card {
:hover { } /* BAD - needs & */
.active { } /* OK but & preferred */
}h2 { }