🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

CSS Rules

AI & DATA SCIENCE // css-rules

A CSS rule (or rule set) pairs one or more selectors with a declaration block, and a stylesheet is simply a sequence of such rules applied to a document in order.

Syntax

selector1, selector2 {
  property: value;
}

Deep Dive Course

A single rule can target multiple selectors at once by separating them with commas, applying the exact same declaration block to every element matched by any of those selectors, which avoids repeating identical declarations across several separate rules. When multiple rules in a stylesheet target the same element with the same property, CSS resolves the conflict using specificity first, more specific selectors win regardless of their order in the file, and then, among equally specific rules, source order, with the rule appearing later in the stylesheet winning.

1Understanding CSS Rules

A single rule can target multiple selectors at once by separating them with commas, applying the exact same declaration block to every element matched by any of those selectors, which avoids repeating identical declarations across several separate rules. When multiple rules in a stylesheet target the same element with the same property, CSS resolves the conflict using specificity first, more specific selectors win regardless of their order in the file, and then, among equally specific rules, source order, with the rule appearing later in the stylesheet winning.

💡

Group selectors with a comma, h1, h2, h3 { margin-top: 0; }, whenever several different selectors need the exact identical declaration block — it avoids repeating the same declarations across multiple separate rules, and keeps them easier to update together.

editor.html
h1, h2, h3 {
  font-family: Georgia, serif;
  margin-top: 0;
}
localhost:3000

2Practical Example

Here is a real-world application of CSS Rules showing how it is used in production CSS code.

editor.html
.box { color: blue; }
.box { color: green; }

<div class="box">Text</div>
localhost:3000

3Best Practices

Follow these guidelines when working with CSS Rules:

1. Group selectors with commas when several of them need identical declarations, rather than repeating the same declaration block in multiple separate rules

2. Remember specificity, not just source order, determines which conflicting rule wins — a highly specific rule earlier in the file can still beat a less specific one written later

3. Order equally-specific, conflicting rules deliberately, since among rules of the same specificity, the one appearing later in the stylesheet wins

⚠️

Tip: Group selectors with a comma, h1, h2, h3 { margin-top: 0; }, whenever several different selectors need the exact identical declaration block — it avoids repeating the same declarations across multiple separate rules, and keeps them easier to update together.

editor.html
h1, h2, h3 {
  font-family: Georgia, serif;
  margin-top: 0;
}
localhost:3000

Examples

Example 01Basic Usage
h1, h2, h3 {
  font-family: Georgia, serif;
  margin-top: 0;
}
Example 02Advanced Example
.box { color: blue; }
.box { color: green; }

<div class="box">Text</div>

Best Practices

  • Group selectors with commas when several of them need identical declarations, rather than repeating the same declaration block in multiple separate rules
  • Remember specificity, not just source order, determines which conflicting rule wins — a highly specific rule earlier in the file can still beat a less specific one written later
  • Order equally-specific, conflicting rules deliberately, since among rules of the same specificity, the one appearing later in the stylesheet wins

Interview Question

If two rules with equal specificity both set the same property on the same element, which one actually applies, and why?

Hint: Think about how a CSS parser processes a stylesheet from top to bottom, and what happens when it encounters a later declaration for a property it's already seen.

When two rules have exactly equal specificity, and both attempt to set the same property on the same matched element, CSS falls back to source order as the deciding tiebreaker: the declaration that appears later in the stylesheet, whether that's later in the same file or in a later-linked stylesheet, is the one that actually takes effect, effectively overwriting the earlier one. This mirrors how a parser processes the stylesheet sequentially from top to bottom, applying each matching declaration for that property as it's encountered, so the very last one it processes for a given element and property is naturally the one left standing once parsing completes. This is exactly why, for equally-specific conflicting rules, the order in which they're written in the stylesheet matters directly, and is a common, subtle source of confusion when two rules with the same specificity are scattered far apart in a large file, or come from separately-loaded stylesheets in a particular loading order.

Exercises

MediumPractice using CSS Rules in a real scenario.
View Solution
h1, h2, h3 {
  font-family: Georgia, serif;
  margin-top: 0;
}

Frequently Asked Questions

If two rules with equal specificity both set the same property on the same element, which one actually applies, and why?

When two rules have exactly equal specificity, and both attempt to set the same property on the same matched element, CSS falls back to source order as the deciding tiebreaker: the declaration that appears later in the stylesheet, whether that's later in the same file or in a later-linked stylesheet, is the one that actually takes effect, effectively overwriting the earlier one. This mirrors how a parser processes the stylesheet sequentially from top to bottom, applying each matching declaration for that property as it's encountered, so the very last one it processes for a given element and property is naturally the one left standing once parsing completes. This is exactly why, for equally-specific conflicting rules, the order in which they're written in the stylesheet matters directly, and is a common, subtle source of confusion when two rules with the same specificity are scattered far apart in a large file, or come from separately-loaded stylesheets in a particular loading order.

Related Functions

CSS-SyntaxClass-SelectorsID-Selectors