DEV Community

Peace Adekoya
Peace Adekoya

Posted on

Simplifying CSS: Understanding Specificity, Cascade, and Selectors

CSS makes your website look good. It's like a recipe for style. But there are concepts in CSS that makes this happen. These concepts include CSS cascade, selectors, and specificity.

CSS Selector

CSS selectors are patterns used to select and style elements in an HTML document. They allow you to target specific elements or groups of elements based on their attributes, types, relationships with other elements, or their positions in the document tree.

Here are some common types of CSS selectors:

  • Element Selector: Selects all elements of a specified type. For example,
p /* Selects all paragraphs. */
Enter fullscreen mode Exit fullscreen mode
  • Class Selector: Selects elements with a specific class attribute. For example,
.my-class /* Selects all elements with the class "my-class".*/
Enter fullscreen mode Exit fullscreen mode
  • ID Selector: Selects a single element with a specific ID attribute. For example,
#my-id /* Selects the element with the ID "my-id".*/
Enter fullscreen mode Exit fullscreen mode
  • Attribute Selector: Selects elements based on their attributes. For example,
[type="button"] /* Selects all elements with a "type" attribute set to "button" */
Enter fullscreen mode Exit fullscreen mode
  • Descendant Selector: Selects elements that are descendants of another element. For example,
div p /* Selects all paragraphs that are descendants of div elements. */
Enter fullscreen mode Exit fullscreen mode
  • Child Selector: Selects elements that are direct children of another element. For example,
div > p /* Selects all paragraphs that are direct children of div elements. */
Enter fullscreen mode Exit fullscreen mode
  • Adjacent Sibling Selector: Selects an element that is immediately preceded by a specific element. For example,
h2 + p /* Selects the paragraph immediately following an h2 element.*/
Enter fullscreen mode Exit fullscreen mode
  • General Sibling Selector: Selects elements that are siblings of a specified element. For example,
h2 ~ p /* selects all paragraphs that are siblings of h2 elements. */

Enter fullscreen mode Exit fullscreen mode

CSS Cascade

Think of CSS rules as a set of instructions. When there are many rules for the same thing, the cascade decides which one comes first. Some rules are more important than others. For example, a rule in your HTML directly beats a rule in a separate style sheet.

Also, the more specific a rule, the more weight it carries. So if you say "make all headings red," but then later say "make all h1 headings blue," the blue wins because it's more specific.

Understanding the cascade helps you control your website's look. It's your secret weapon for making things look just right.

CSS Specificity

CSS Specificity determines which CSS rule is applied by the browser when multiple competing rules target the same element.

Think of it like a hierarchy or ranking system for CSS rules.

Here's how it works:

  • Inline Styles: These have the highest specificity. They are styles applied directly to an HTML element using the style attribute.

  • IDs: Selectors that target an element by its ID have higher specificity compared to classes or element selectors. For example,

#myElement
Enter fullscreen mode Exit fullscreen mode

has higher specificity than

.myClass.
Enter fullscreen mode Exit fullscreen mode
  • Classes, attributes, and pseudo-classes: These selectors have medium specificity. They target elements based on their class, attributes, or pseudo-classes like
:hover or :focus

Enter fullscreen mode Exit fullscreen mode
  • Element selectors: These selectors have the lowest specificity. They target HTML elements directly, such as p, div, span, etc.

When multiple rules apply to the same element, the browser calculates their specificity and applies the rule with the highest specificity.

For example:

  • If an inline style is present, it overrides any other styles applied to the element.
  • If no inline styles are present, but there are conflicting styles applied via IDs, the style with the more specific ID selector will be applied.
  • If there are conflicting styles applied via classes, attributes, or pseudo-classes, the style with the more specific selector will be applied.
  • If all else fails, the style defined by the least specific selector (e.g., element selector) will be applied.

Understanding specificity helps you write CSS rules that apply the styles you intend and avoid unexpected styling behavior.

Top comments (0)