Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 03 / 963%· free preview
CSS Basics3/7

CSS Selectors

Element, class, id, descendant, attribute — every way to target HTML.

Definition
SelectorTargetsExample
*Everything* { box-sizing: border-box; }
tagAll tagsp { ... }
.classClass.btn { ... }
#idSingle id#hero { ... }
a, bMultipleh1, h2 { ... }
a bDescendantnav a { ... }
a > bDirect childul > li { ... }
a + bAdjacent siblingh1 + p { ... }
a ~ bGeneral siblingh1 ~ p { ... }
[attr]Attribute[disabled] { ... }
[attr="val"]Attribute match[type="email"]
:hoverPseudo-classa:hover { ... }
::beforePseudo-elementp::before { ... }
Selector Cheat Sheet
css
* { margin: 0; padding: 0; box-sizing: border-box; }    /* universal */
body { font-family: sans-serif; }                        /* tag */
.card { padding: 1rem; }                                 /* class */
#hero { height: 400px; }                                 /* id */
nav a { color: orange; }                                 /* descendant */
ul > li { list-style: none; }                            /* direct child */
input[type="email"] { border: 1px solid blue; }          /* attribute */
a:hover { text-decoration: underline; }                  /* pseudo-class */
p::first-letter { font-size: 2em; }                      /* pseudo-element */
Specificity (priority)
  1. Inline style="" (1000)
  2. #id (100)
  3. .class, [attr], :hover (10)
  4. tag, ::before (1)

Higher score wins. Same score → last rule wins.

Common Mistakes
  • #id overuse — too specific to override later. Prefer .class.
  • Extreme nestingbody div ul li a — over-specific, brittle.
  • !important to win — fix specificity instead.
Interview Questions

Practice Exercises
  1. Style nav linksnav a { ... }. Hint: descendant.
  2. Email inputinput[type="email"]. Hint: attribute selector.
  3. First letterp::first-letter { font-size: 2em; }. Hint: pseudo-element.

💡 Think Like a Programmer: Selectors are CSS's query language. Learn 5 — tag, .class, #id, a b, a:hover — and you cover 90% of code.

AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

Ready to move on?
// example library
Want more hands-on snippets in CSS3?
Browse 1 runnable example · across 1 chapter · short, copy-paste-friendly · grouped by topic
Explore examples
// did you know?
One surprising fact before your next lesson
Bite-sized programming facts that make your next coffee-break explanation land.
Did You Know
// feedback.matters()
Did this lesson help you?