CSS Selector Cheat Sheet: Types, Examples, and Combinations

Шпаргалка CSS селекторів: типи, приклади та поєднання

A CSS selector cheat sheet is useful when you need to target an HTML element quickly without trial and error. For layout work, style fixes, and debugging, it is the fastest way to reach the right block, button, or element state.

Basic CSS selectors for everyday layout work

Basic CSS selectors cover most common tasks: selecting by tag, class, id, or a universal pattern.

  • Tag targets every element of the same type, such as p, a, or button.
  • Class selects all elements with a specific class, such as .card or .active.
  • id is a unique selector for one element, such as #header.
  • * is the universal selector for every element on the page.

The most practical choice for most interfaces is a class because it stays flexible and does not tie styles to one specific element. The check is simple: if the style applies to every block you expected, the selector is correct; if it affects extra elements, the selector needs to be narrowed.

Combined CSS selectors for more precise targeting

Combined CSS selectors help you reach the right element in the page structure without adding extra classes everywhere.

  • Descendant.menu a selects all links inside a menu.
  • Child.menu > a selects only direct children.
  • Adjacent siblingh2 + p selects the paragraph immediately after a heading.
  • General siblingh2 ~ p selects all paragraphs after a heading at the same nesting level.

These selectors are especially useful when the structure is stable and you want to style content without adding helper classes. If a rule does not work, check the HTML nesting first: the most common mistake is expecting a direct child where the element is only a descendant.

Attribute selectors and pseudo-classes

Attribute selectors and pseudo-classes add precision when you need to account for state, type, or attribute value.

  • [type="email"] selects fields with a specific type.
  • [href^="https"] finds links that start with a given value.
  • [data-state="open"] is useful for interfaces with custom attributes.
  • :hover, :focus, and :checked respond to user interaction.
  • :first-child, :last-child, and :nth-child(2) work with an element’s position in a list.

Attribute selectors are especially helpful in components where there are too many classes and the state is already stored in the markup. Pseudo-classes are convenient for checking behavior: if an element highlights on hover or focus, the selector is working; if it does not, check whether that element can actually receive the state you are targeting.

How to combine CSS selectors without unnecessary complexity

Combining CSS selectors gives you control over precision, but too much detail quickly makes styles fragile.

The practical rule is simple: start with the simplest selector and make it more specific only when there is a conflict or the scope is too broad. For most tasks, a class or a class with one qualifier is enough, such as .card .title or .tabs > .tab.

  • Use classes for reusable components.
  • Use id for unique elements, but do not build your whole styling system around it.
  • Use pseudo-classes and attributes for states and interaction.
  • Use descendant and child selectors for page structure.

If a rule is hard to override, the selector is probably too specific. In that case, it is safer to simplify it now than to fight the cascade and unpredictable style overrides later.

Quick reference for fast checking

A CSS selector cheat sheet works best when you can apply it in a few seconds during layout work.

  • Need all elements of one type — use a tag selector.
  • Need a reusable component — use a class.
  • Need one specific block — use an id or a more precise class.
  • Need an element in a structure — use a descendant or child selector.
  • Need a state or attribute — use a pseudo-class or an attribute selector.

The best way to verify a selector is to apply a temporary visible style, such as a border color or background, and check whether the expected elements are highlighted. If they are not, the problem is usually nesting, specificity, or choosing the wrong selector type.