CSS Specificity - Detailed Overview

Introduction to CSS Specificity

CSS specificity determines which styles are applied by the browser when multiple rules could apply to the same element. Specificity is calculated based on the types of selectors used in the rules.

Specificity Hierarchy

The hierarchy of CSS specificity is as follows (from lowest to highest):

Examples of CSS Specificity

Below are examples illustrating different levels of CSS specificity:

/* General styles */
p {
    color: black;
}

/* Class selector with specificity */
.class-specific {
    color: red;
}

/* ID selector with higher specificity */
#id-specific {
    color: blue;
}

/* Inline styles with highest specificity */
.inline-example {
    color: green;
}

Example 1: General Styles

This text will be black by default.

Example 2: Class Selector

This text will be red because of the class selector.

Example 3: ID Selector

This text will be blue because of the ID selector.

Example 4: Inline Style

This text will be green because of the inline style.