Type selector
Targets every element of that type. Example: p. Use carefully.
Selectors are how CSS finds HTML. Learn to choose elements without making your stylesheet fragile or overly specific.
.pricing-card--featured {
border-color: #8cffc1;
}
CSS Basics
A selector tells the browser which elements a rule applies to. Simple selectors include element names, classes, IDs and attributes.
Selectors can also describe relationships: a link inside a nav, a paragraph directly after a heading or a button in hover state.
The beginner mistake is making selectors too broad or too specific. Professional CSS uses selectors that are clear, reusable and easy to override when needed.
Targets every element of that type. Example: p. Use carefully.
Targets reusable styling hooks. Example: .card. This is the workhorse.
Targets one unique id. Useful for anchors, risky for reusable styling.
Targets state or position. Example: :hover, :focus-visible, :first-child.
Visual model
CSS becomes easier when you can see the parts: what is selected, what is declared, what the browser loads and what the final interface receives.
.feature-card > h2
Find elements with this class.
Only direct children, not every nested descendant.
Target heading elements inside that component.
Apply the style to matched headings.
Examples
.pricing-card {
padding: 2rem;
border-radius: 1.5rem;
}
.pricing-card__title {
font-size: 2rem;
}
.pricing-card:hover {
transform: translateY(-2px);
}
body main section div:nth-child(2) article h2 span {
color: red;
}
#pricing .card .content .header .title {
font-size: 2rem;
}
Rules that matter
Beginners often try to learn CSS by collecting declarations. That is backwards. Learn the decisions first, then the properties become much easier to remember.
Classes are reusable and do not depend too much on document structure.
Long selector chains break when markup changes.
IDs are very specific and can make overrides harder.
A broad selector such as button or a can be fine in base CSS, but dangerous inside random pages.
:hover and :focus-visible should make interaction clearer, not only prettier.
A class such as .alert is stronger than .red-box because purpose survives redesign.
Production thinking
Selectors decide how maintainable CSS feels. A good selector is easy to find, easy to understand and easy to change without starting a fight with specificity.
State selectors should include keyboard states. If you style :hover, often you should also style :focus-visible.
Production CSS works best with a predictable naming strategy. Classes should be meaningful and selectors should avoid unnecessary depth.
Selectors do not directly affect SEO, but they control the readability and usability of content that visitors and search engines discover.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read the lesson. Answer these questions out loud or write the answers in your own notes. CSS becomes real when you can explain what the browser is doing.
Senior audit upgrade
Selectors such as :is(), :where(), :not() and :has() are powerful. They should make CSS clearer, not turn selectors into puzzles.
:where() always has zero specificity. It is useful for defaults that should stay easy to override.
These take specificity from the most specific selector inside their argument list.
Use ::before and ::after for decorative generated content, not for meaningful text users need.
:where(article, section) h2 { margin-block-start: 2rem; }
.card:is(:hover, :focus-within) { border-color: var(--accent); }
.form:has(:invalid) { --border: var(--danger); }