Lesson 3 of 50
cssBasic Selectors
10 mins
Course progress6%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 3 of 50
css10 mins
Keep these ideas in mind as you work through the lesson.
Selectors are the language‑level address of CSS. Mastering them early gives you a mental model that works across Flexbox, Grid, Animations, and Component Libraries.
| Selector | Example | What it Matches |
|---|---|---|
* | * { box-sizing:border-box; } | Every element on the page. |
p | p { line-height:1.6; } | All <p> elements. |
.class | .btn { padding:0.5rem; } | Elements with class="btn". |
#id | #logo { width:120px; } | The element whose id equals logo. |
element, element2 | h1, h2 { margin:0; } | Group of selectors (comma‑separated). |
element > child | ul > li { list-style:none; } | Direct children only. |
element + sibling | h2 + p { margin-top:0; } | The adjacent sibling that follows. |
element ~ sibling | h2 ~ p { color:#555; } | Any sibling that follows. |
/* Group selector – same rules on two different tags */
button, .button {
background:#0066cc;
color:#fff;
border:none;
padding:0.6rem 1.2rem;
cursor:pointer;
}
Avoid deep descendant selectors such as
header nav ul li a. They raise specificity and make future refactoring painful. Prefer a single‑class approach (.nav-link).