Skip to content

CSS Selectors List

CSS selectors are used to target HTML elements and apply styles to them. This complete CSS selectors list explains selector types, examples, usage, specificity, pseudo-classes, pseudo-elements, combinators, attribute selectors, best practices, and common mistakes in table format.

What Are CSS Selectors?

A CSS selector tells the browser which HTML element or group of elements should receive a style rule. Selectors can target elements by tag name, class, ID, attribute, state, position, relationship, or generated parts.

selector {
  property: value;
}

.card {
  padding: 1rem;
  border: 1px solid #dee2e6;
}

CSS Selectors List in Table Format

Category Selector Example Purpose
BasicUniversal Selector*Selects all elements.
BasicElement SelectorpSelects all matching HTML elements.
BasicClass Selector.cardSelects elements with a specific class.
BasicID Selector#main-headerSelects one element with a specific ID.
BasicSelector Listh1, h2, h3Applies the same styles to multiple selectors.
CombinationClass + Elementbutton.primarySelects a specific element with a class.
CombinationMultiple Classes.button.primarySelects elements that have both classes.
CombinationID + Class#app.theme-darkSelects an ID element with a class.
CombinatorDescendant Selector.card pSelects all matching descendants inside a parent.
CombinatorChild Selector.card > pSelects direct children only.
CombinatorAdjacent Sibling Selectorh2 + pSelects the next sibling immediately after another element.
CombinatorGeneral Sibling Selectorh2 ~ pSelects following siblings that match.
AttributeHas Attribute[disabled]Selects elements that contain an attribute.
AttributeExact Attribute Value[type="email"]Selects elements with an exact attribute value.
AttributeContains Word[class~="active"]Selects attribute values containing a specific word.
AttributeStarts With Prefix[lang|="en"]Selects exact value or value followed by a hyphen.
AttributeBegins With[href^="https"]Selects values that begin with specific text.
AttributeEnds With[src$=".webp"]Selects values that end with specific text.
AttributeContains Text[href*="tutorials"]Selects values containing specific text.
AttributeCase-insensitive Match[type="email" i]Selects matching values regardless of case.
Pseudo-classHovera:hoverStyles an element when pointer hovers over it.
Pseudo-classFocusinput:focusStyles an element when it receives focus.
Pseudo-classFocus Visiblebutton:focus-visibleShows focus styles mainly for keyboard users.
Pseudo-classActivebutton:activeStyles an element while it is being activated.
Pseudo-classVisiteda:visitedStyles visited links with limited styling support.
Pseudo-classCheckedinput:checkedSelects checked checkboxes or radio buttons.
Pseudo-classDisabledbutton:disabledSelects disabled controls.
Pseudo-classEnabledinput:enabledSelects enabled controls.
Pseudo-classRequiredinput:requiredSelects required form fields.
Pseudo-classOptionalinput:optionalSelects optional form fields.
Pseudo-classValidinput:validSelects valid form fields.
Pseudo-classInvalidinput:invalidSelects invalid form fields.
Pseudo-classRead Onlyinput:read-onlySelects read-only fields.
Pseudo-classRead Writeinput:read-writeSelects editable fields.
Structural Pseudo-classFirst Childli:first-childSelects the first child.
Structural Pseudo-classLast Childli:last-childSelects the last child.
Structural Pseudo-classOnly Childp:only-childSelects an element that is the only child.
Structural Pseudo-classFirst of Typep:first-of-typeSelects the first matching element type.
Structural Pseudo-classLast of Typep:last-of-typeSelects the last matching element type.
Structural Pseudo-classOnly of Typep:only-of-typeSelects the only element of its type.
Structural Pseudo-classNth Childtr:nth-child(even)Selects elements by position pattern.
Structural Pseudo-classNth Last Childli:nth-last-child(2)Selects by position from the end.
Structural Pseudo-classNth of Typep:nth-of-type(2)Selects matching type by position.
Structural Pseudo-classNth Last of Typep:nth-last-of-type(2)Selects matching type by position from the end.
Functional SelectorNot.card:not(.featured)Selects elements that do not match a selector.
Functional SelectorIs:is(h1, h2, h3)Groups selectors and uses highest specificity inside.
Functional SelectorWhere:where(section, article)Groups selectors with zero specificity.
Functional SelectorHas.card:has(img)Selects an element based on its descendants.
Pseudo-elementBefore.link::beforeCreates generated content before element content.
Pseudo-elementAfter.link::afterCreates generated content after element content.
Pseudo-elementFirst Letterp::first-letterStyles the first letter of text.
Pseudo-elementFirst Linep::first-lineStyles the first rendered line of text.
Pseudo-elementSelection::selectionStyles selected text.
Pseudo-elementMarkerli::markerStyles list bullets or numbers.
Pseudo-elementPlaceholderinput::placeholderStyles placeholder text.
Pseudo-elementBackdropdialog::backdropStyles the backdrop behind dialogs.

Most Used CSS Selectors for Beginners

Beginners should first master the selectors used in almost every project.

  • element selectors like p, h1, and button.
  • .class selectors for reusable component styling.
  • #id selectors for unique anchors or special cases.
  • Descendant selectors like .article p.
  • Child selectors like .nav > li.
  • Pseudo-classes like :hover, :focus, and :disabled.
  • Attribute selectors like [type="email"].

CSS Selectors and Specificity

Different selectors have different specificity weights. Higher specificity can override lower specificity when rules target the same property.

Selector Type Example Specificity
Universal * 0-0-0
Element p 0-0-1
Class .card 0-1-0
Attribute [type="email"] 0-1-0
Pseudo-class :hover 0-1-0
ID #header 1-0-0

CSS Selector Examples

/* Select all paragraphs */
p {
  line-height: 1.6;
}

/* Select elements with class */
.card {
  padding: 1rem;
}

/* Select direct child links */
.nav > a {
  text-decoration: none;
}

/* Select focused inputs */
input:focus {
  outline: 3px solid #0d6efd;
}

/* Select cards that contain images */
.card:has(img) {
  display: grid;
}

/* Add decorative icon */
.external-link::after {
  content: " ↗";
}

CSS Selectors and Accessibility

  • Use :focus and :focus-visible for keyboard-friendly focus styles.
  • Do not remove outlines without accessible replacements.
  • Use :disabled, :invalid, and :required to style form states clearly.
  • Do not rely only on :hover because touch users may not experience hover.
  • Use pseudo-elements only for decorative content, not essential text.
  • Keep selectors simple so accessibility states are easy to maintain.

CSS Selectors and SEO

CSS selectors do not directly create rankings, but clean selector architecture improves maintainability, accessibility, responsive design, page quality, and long-term site health.

  • Simple selectors reduce style conflicts.
  • Accessible states improve usability.
  • Maintainable CSS supports content updates.
  • Reliable styling prevents broken layouts.
  • Semantic HTML plus clean selectors improves page clarity.

Common CSS Selector Mistakes

  • Using overly complex descendant selectors.
  • Using IDs for styling when classes are easier to reuse.
  • Relying only on :hover for important interactions.
  • Using pseudo-elements for meaningful content instead of decoration.
  • Creating selectors with high specificity that are hard to override.
  • Using universal selectors carelessly in large pages.
  • Confusing pseudo-classes with pseudo-elements.

CSS Selector Best Practices

  • Prefer class selectors for component styling.
  • Keep selectors short and readable.
  • Avoid deeply nested selectors.
  • Use IDs mostly for anchors, labels, and JavaScript hooks, not reusable styling.
  • Use :where() for low-specificity base styles.
  • Use :focus-visible for accessible keyboard focus indicators.
  • Use attribute selectors for form states and data attributes when useful.
  • Use pseudo-elements for decorative UI only.
  • Check specificity before adding !important.

Key Takeaways

  • CSS selectors target HTML elements for styling.
  • Common selectors include element, class, ID, attribute, combinator, pseudo-class, and pseudo-element selectors.
  • Class selectors are usually best for reusable component styling.
  • Pseudo-classes style states like hover, focus, checked, disabled, valid, and invalid.
  • Pseudo-elements style generated or partial content like before, after, marker, and selection.
  • Simple, low-specificity selectors are easier to maintain.

Pro Tip

For scalable CSS, use simple class selectors for components, keep nesting shallow, and use :focus-visible for accessible keyboard states.