CSS Selectors

CSS Selectors

Selectors are the core of CSS — their job is to "select" the HTML elements you want to style. CSS offers multiple types of selectors, from simple tag selectors to complex selector combinations, giving you precise control over any element.

Basic Selectors

1. Tag Selector (Element Selector)

Uses HTML tag names directly as selectors, matching all tags with the same name in the document.

CSS
/* All <p> elements turn red */
p {
  color: red;
}

/* All <h1> elements turn blue */
h1 {
  color: blue;
}

/* All <div> elements get a border */
div {
  border: 1px solid black;
}

2. Class Selector

Matches elements by their class attribute. Class names are prefixed with a period ..

CSS
/* All elements with class="highlight" */
.highlight {
  background-color: yellow;
}

/* Only <p> elements with class="center" */
p.center {
  text-align: center;
}

HTML elements can have multiple classes, separated by spaces:

HTML
<p class="highlight center">This text is both highlighted and centered</p>

3. ID Selector

Matches elements by their id attribute, prefixed with a hash #.

CSS
/* Select element with id="header" */
#header {
  background-color: #333;
  color: white;
}

Note: An ID can only appear once per page. ID selectors are typically used for unique elements.

4. Universal Selector

Uses an asterisk * to match all elements in the document.

CSS
/* Reset margins and padding for all elements */
* {
  margin: 0;
  padding: 0;
}

The universal selector is convenient, but be careful about performance — if complex styles are applied, it can affect page rendering speed.

Selector Specificity

When multiple selectors apply to the same element, the browser uses specificity rules to determine which style takes effect:

Selector Type Specificity Value
Inline styles (style attribute) 1000
ID selectors 0100
Class/Attribute/Pseudo-class selectors 0010
Tag/Pseudo-element selectors 0001
Universal selector 0000

Priority from high to low:

Inline styles > ID selectors > Class selectors > Tag selectors

Specificity Calculation Example

CSS
/* Specificity: 0001 */
p {
  color: black;
}

/* Specificity: 0010 */
.text {
  color: blue;
}

/* Specificity: 0100 */
#special {
  color: green;
}

/* Specificity: 0011 (class + tag) */
p.text {
  color: red;
}

For the same <p class="text" id="special"> element, the final text color will be green (ID selector has the highest specificity).

!important

CSS provides a "nuclear weapon" — !important — that forces a style to have the highest priority:

CSS
p {
  color: red !important;
}

Don't use !important unless absolutely necessary — it makes style debugging difficult. Prioritize solving problems by increasing selector specificity.

Best Practices for Selectors

  1. Use class selectors frequently: Classes are reusable and the most flexible selector
  2. Use ID selectors sparingly: IDs are unique, hard to reuse, and their high specificity can cause override issues
  3. Avoid over-specification: div.container ul li a is too specific; .nav-link is enough
  4. Keep selectors concise: Longer selectors are harder to maintain and affect performance

❓ FAQ

Q When should I use class vs ID selectors?
A IDs can only appear once per page, suitable for unique elements (like top navigation, footer); classes can be reused, suitable for applying the same styles to multiple elements. In CSS, prefer classes; use IDs only for truly unique elements.
Q What's the difference between descendant selectors (space) and child selectors (>)?
A Descendant selectors select all levels of child elements; child selectors only select direct children. For example, `div p` selects all p elements inside div at any level, while `div > p` only selects direct child p elements, regardless of grandchildren.
Q Does the universal selector * affect performance?
A Minimal use has little impact, but don't use it extensively. Since `*` matches all elements, the browser checks each one. If nested in complex selectors (like `#nav * li`), it may cause performance waste. Simple global resets like `* { box-sizing: border-box; }` are fine.

📖 Summary

📝 Exercises

  1. Create an HTML page with headings, paragraphs, and lists.
  2. Use tag selectors to set text color for all paragraphs.
  3. Use class selectors to set different background colors for specific elements (define at least 2 different classes).
  4. Use ID selectors to set special styles for one element on the page.
  5. Test: Apply tag, class, and ID selectors to the same element and observe which has the highest priority.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏