Attribute, grouped, and universal selectors
Attribute selectors can match elements by their HTML attributes, while commas let one rule serve several selectors. The universal selector is powerful but should be used deliberately.
✓ What you will learn in this topic
- Explain the key idea: Attribute selectors target attributes; selector lists share rules; the universal selector matches all elements.
- Apply the concept: Both heading levels share a color, email fields get a blue border, and disabled buttons appear subdued.
- Avoid this common mistake:
Concept explanation
An attribute selector such as input[type=\"email\"] matches email controls. A comma-separated selector list shares declarations across targets, for example headings and paragraphs. The universal selector * matches every element and is commonly used in a small box-sizing reset.
Selector lists are alternatives, not requirements: h1, h2 means either heading can match. Keep selectors readable and avoid broad rules that accidentally alter controls or embedded content.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
h1, h2 {
color: #0f172a;
}
input[type="email"] {
border-color: #2563eb;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>h1, h2 {
color: #0f172a;
}
input[type="email"] {
border-color: #2563eb;
}Expected result: Both heading levels share a color, email fields get a blue border, and disabled buttons appear subdued.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>*,
*::before,
*::after {
box-sizing: border-box;
}
button[disabled] {
opacity: 0.55;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
A class selector starts with a dot, but the class value in HTML does not. An ID selector starts with a hash and should identify one unique element.
Best practice
Prefer clear class selectors for reusable components; keep specificity modest so later styles are easy to override.
Practice exercise
Style a page with a shared card class, a unique page heading, and a link that matches a selected attribute. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Attribute selectors target attributes; selector lists share rules; the universal selector matches all elements.
- Prefer clear class selectors for reusable components; keep specificity modest so later styles are easy to override.
Knowledge check
What does a comma between selectors mean?
Reveal the explanation
A selector list shares declarations among each listed target.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
cssh1, h2 {
color: #0f172a;
}
input[type="email"] {
border-color: #2563eb;
}
Expected result:
Both heading levels share a color, email fields get a blue border, and disabled buttons appear subdued.
Hands-on practice task
Style a page with a shared card class, a unique page heading, and a link that matches a selected attribute.
In this section
Other lessons in CSS Selectors
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.