Welcome to Day 23 of the 180 Days of Frontend Development Challenge. Today we'll dive deep into CSS selectors - the core mechanism for targeting HTML elements. For comprehensive selector techniques, see the Learn Frontend Development in 180 Days ebook.
1. Element Selectors (Type Selectors)
Basic Syntax
element {
property: value;
}
Implementation Examples
/* Target all <p> elements */
p {
font-size: 1.1rem;
line-height: 1.6;
color: #333;
}
/* Style all <h2> headings */
h2 {
color: #2c3e50;
margin-bottom: 1.5rem;
}
/* Style all links */
a {
text-decoration: none;
color: #3498db;
}
When to Use
- Base/reset styles
- Typography defaults
- Broad element-specific styling
Professional Considerations
/* Avoid overusing - can be too broad */
div {
padding: 1rem; /* Affects ALL divs */
}
2. Class Selectors (Reusable Styles)
Basic Syntax
.class-name {
property: value;
}
Implementation Examples
<!-- HTML -->
<button class="btn btn-primary">Submit</button>
<article class="card featured"></article>
/* CSS */
.btn {
padding: 0.75rem 1.5rem;
border-radius: 4px;
font-weight: 600;
}
.btn-primary {
background-color: #3498db;
color: white;
}
.card {
border: 1px solid #ddd;
border-radius: 8px;
padding: 1.5rem;
}
.featured {
border-color: #3498db;
box-shadow: 0 2px 8px rgba(52, 152, 219, 0.2);
}
When to Use
- Component-based styling
- Reusable UI patterns
- State variations
- Most styling needs
Best Practices
/* Use semantic names */
.article-card {} /* Good */
.box-style-1 {} /* Avoid */
/* Follow BEM methodology */
.nav__item--active {}
3. ID Selectors (Unique Elements)
Basic Syntax
#id-name {
property: value;
}
Implementation Examples
<!-- HTML -->
<header id="main-header"></header>
<section id="pricing"></section>
/* CSS */
#main-header {
position: sticky;
top: 0;
background: white;
z-index: 100;
}
#pricing {
background: #f8f9fa;
padding: 4rem 0;
}
When to Use
- Unique page sections
- Anchor link destinations
- JavaScript hooks
Professional Considerations
/* Avoid styling with IDs - too specific */
#submit-button {
/* Hard to override */
}
/* Better alternative */
.button-submit {}
4. Grouping Selectors
Basic Syntax
selector1,
selector2,
selector3 {
property: value;
}
Implementation Examples
/* Reset margins for headings */
h1, h2, h3, h4 {
margin-top: 0;
}
/* Shared button styles */
.btn,
input[type="submit"],
input[type="button"] {
cursor: pointer;
}
/* Print styles */
@media print {
nav,
.ad-banner,
.page-actions {
display: none;
}
}
When to Use
- Applying common styles
- Resets/normalization
- Print/special media queries
- Vendor prefix groupings
Best Practices
/* Group logically related selectors */
.primary-btn,
.secondary-btn,
.danger-btn {
min-width: 120px;
}
/* Avoid unrelated groupings */
h1, .alert-box, #user-profile {
/* Potentially confusing */
}
5. Selector Specificity Hierarchy
Specificity Weight
- Inline styles (1000)
- ID selectors (100)
- Class/attribute selectors (10)
- Element/pseudo-element selectors (1)
Calculation Examples
#nav .item.active {} /* 100 + 10 + 10 = 120 */
section#about p {} /* 1 + 100 + 1 = 102 */
.btn:hover {} /* 10 + 10 = 20 */
div {} /* 1 */
Important Rule
/* Avoid when possible */
.warning-text {
color: red !important; /* Overrides everything */
}
6. Practical Application
HTML Structure
<article class="card featured" id="feature-card">
<h2 class="card__title">Featured Product</h2>
<p class="card__text">Description here...</p>
<button class="btn" id="buy-now">Purchase</button>
</article>
CSS Styling
/* Element selector */
article {
margin-bottom: 2rem;
}
/* Class selector */
.card {
border: 1px solid #ddd;
padding: 1.5rem;
}
/* ID selector */
#feature-card {
border-color: #3498db;
}
/* Grouped selectors */
.card__title,
.card__text {
margin: 0 0 1rem 0;
}
/* Combined selectors */
.card.featured {
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
/* Specificity example */
#feature-card .btn {
background: #3498db; /* Wins over .btn */
}
7. Exercises
-
Selector Practice
- Create styles using all selector types
- Demonstrate specificity conflicts
- Show how grouping reduces code
Refactor This CSS
#header nav ul li a {
color: white;
}
/* Convert to class-based */
-
Specificity Wars
- Create competing selectors for the same element
- Predict which styles will apply
- Verify in browser DevTools
What's Next?
Tomorrow (Day 24) explores Advanced CSS Selectors including attribute selectors, pseudo-classes, and combinators. For complete selector mastery including CSS4 features, see Chapter 18 in the Learn Frontend Development in 180 Days ebook.
Pro Tip: Use specificity graphs to audit your CSS:
# Install specificity-graph
npm install -g specificity-graph
# Generate report
specificity-graph styles/*.css
Top comments (0)