Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 1.65 KB

README.md

File metadata and controls

55 lines (37 loc) · 1.65 KB

18. Selectors

Target elements accurately without brittle markup.

Learn

  • type
  • class
  • ID
  • attribute
  • combinators
  • grouping
  • pseudo-classes
  • pseudo-elements

Build

Style a navigation list and form using classes, attributes, and interaction states.

Check your work

  • Open the result in a browser and inspect the HTML and computed CSS.
  • Test at narrow and wide widths and use the keyboard for interactive elements.
  • Write down one thing you changed after testing.

Deep lesson

How it works

Selectors describe which elements a rule matches. Type selectors are broad, classes are reusable, IDs identify one element, attribute selectors match data or control types, and combinators describe relationships in the tree. Pseudo-classes match states or positions; pseudo-elements address parts of a rendered element. Prefer simple classes for component styling and use relational selectors when the relationship is meaningful.

Common mistake

A deeply nested selector becomes fragile when HTML changes. An ID selector often creates more specificity than a reusable component needs.

Guided challenge

Style a list with a direct-child selector, one attribute selector, and one interaction state. Move an element in the DOM and predict which rules still apply.

Completion check

  • Rebuild the core idea without copying the example line by line.
  • Explain the key HTML or CSS choice in your own words.
  • Test the result at a narrow width and with keyboard navigation where relevant.

Run the example

Open example.html in a browser. Edit style.css, save, and refresh.

Try: Add a selector that targets only the first card.