When you write CSS, you need a way to tell the browser which HTML elements you want to style. That's where selectors come in. They are patterns used to select specific elements for styling. Think of them as pointers. For example, p selects all paragraph tags, .my-class selects all elements with class="my-class", and #my-id selects the single element with id="my-id". Understanding selectors is the first step to applying styles exactly where you intend them to appear on your webpage.
What happens if multiple CSS rules try to style the same element with conflicting properties (e.g., two rules trying to set the color of a p tag differently)? This is where specificity steps in. Specificity is a scoring system the browser uses to determine which CSS rule's declaration is most relevant and should be applied. More specific selectors "win" over less specific ones. For instance, an ID selector (#my-id) is more specific (has a higher score) than a class selector (.my-class), which in turn is more specific than a type (tag) selector (p). Inline styles (styles written directly in the HTML element's style attribute) are even more specific than ID selectors.
Finally, all these concepts fall under the umbrella of the cascade. The cascade is the overarching algorithm browsers use to resolve conflicts when multiple styles apply to an element. It's a set of rules that determines the final computed style of an element. When styles conflict, the cascade considers three main factors in order: 1) Importance: Declarations with !important override most other rules (use sparingly!). 2) Specificity: As discussed, more specific selectors win. 3) Source Order: If two rules have the exact same specificity and importance, the one that appears later in the stylesheet (or in the linked stylesheets) wins. Understanding selectors, specificity, and the cascade empowers you to predict and control exactly how your web pages will look.
Key Takeaways
- Selectors are patterns used to target specific HTML elements for styling.
- Specificity is a scoring system that determines which conflicting CSS rule's styles apply.
- The cascade is the browser's overall algorithm for resolving style conflicts, considering importance, specificity, and source order.
- ID selectors are more specific than class selectors, which are more specific than tag selectors.
- When specificity and importance are equal, the last declared style in the source order wins.
Code Example
How this code works
This code example illustrates how CSS determines which styles to apply to an element when multiple rules could affect it, a core concept known as "specificity" and "the cascade." It shows a single paragraph of text (<p>Welcome to CSS fundamentals!</p>) that has three different color styles defined for it, but only one will actually be visible in the browser, demonstrating how CSS resolves these conflicts.
The stylesheet defines three distinct color properties for the p element. Initially, all p elements are given a color of blue by the p selector. A more specific rule targets elements with the class .highlight, setting their color to green. The most specific rule targets the element with the ID #intro, which sets its color to red. The subtle but crucial point here is that the browser prioritizes these rules based on their specificity, not their order in the stylesheet. Because the #intro ID selector is the most specific among the three, its color: red; rule overrides the others, making the text red. If the #intro ID were removed from the HTML, the .highlight class's green color would then take effect, as it's more specific than the general p selector.