Skip to main content

Command Palette

Search for a command to run...

CSS Selectors and Elements

Published
3 min readView as Markdown

Why CSS Selectors are needed

CSS selectors are needed to find (or select) specific HTML elements on a web page and apply styles to them. Without selectors, CSS rules would apply to everything indiscriminately or have no target at all, making precise web design impossible.

The primary reasons for using CSS selectors include:

  • Precise Styling Control: Selectors provide granular control, allowing developers to style specific elements (like all paragraphs), groups of elements (using a common class), or unique elements (using a unique ID). This ensures the website's appearance is consistent and visually appealing.

  • Separation of Concerns: Selectors enable the separation of structure (HTML) from presentation (CSS), making the code cleaner, more organized, and easier to maintain.

  • Efficiency and Reusability: Instead of manually styling each element, selectors allow the application of the same styles to multiple elements at once (e.g., all buttons get a blue background with a single class selector), which makes the code efficient and reusable.

  • Targeting Element States and Relationships: Selectors can target elements based on their state (e.g., changing a link's color when a user hovers over it using :hover) or their position/relationship within the HTML structure (e.g., selecting only the first paragraph of a section using :first-child).

  • Creating Responsive Designs and Interactivity: By using different types of selectors (including media queries), developers can apply varied styles based on screen size or device type, ensuring websites look good on all devices and supporting interactive experiences.

Element, Class and ID selector

Element (Type) Selector

  • Usage: Selects all instances of a specific HTML element type.

  • Syntax: Use the raw name of the HTML element (e.g., p, h1, div).

  • Example: The following CSS would apply to all paragraph (<p>) elements on the page:

Class Selector

  • Usage: Selects elements that have a specific value in their class attribute. Classes can be applied to multiple elements on a single page, and a single element can have multiple classes.

  • Syntax: Use a period (.) followed by the class name (e.g., .my-class).

  • Example: The following CSS would apply to any element with class="highlight"

ID Selector

  • Usage: Selects a single, unique element on the page with a specific value in its id attribute. The id attribute must be unique within the HTML document.

  • Syntax: Use a hash (#) symbol followed by the ID name (e.g., #unique-section).

  • Example: The following CSS would apply only to the single element with id="main-header":

Group and Descendent Selectors

Group selectors (using commas) apply the same CSS rules to multiple, separate elements, while descendant selectors (using spaces) target nested elements anywhere inside an ancestor, regardless of depth. Grouping optimizes code, whereas descendant selectors provide precise, contextual styling within specific containers.

Group Selectors (,)

  • Purpose: Applies the same style to multiple selectors at once, reducing repetition.

  • Syntax: h1, h2, p { color: blue; } (Styles all h1, h2, and p tags).

  • Usage: Used to unify styling across different HTML elements.

Descendant Selectors ( - Space)

  • Purpose: Selects elements that are nested inside another element, covering children, grandchildren, etc..

  • Syntax: div p { background-color: yellow; } (Styles all p elements located anywhere inside a div).

  • Key Detail: It does not require a direct child relationship; it can be nested many levels deep.

Selector Priority

From highest to lowest:

  • Inline styles (written directly on the element)

  • ID selectors

  • Class selectors

  • Element (tag) selectors

  • Universal selector