Lesson 7: Mastering CSS Sibling Selectors
Learn how to precisely target HTML elements using adjacent and general sibling selectors for more maintainable and efficient styles.
Start LearningUnderstanding Sibling Relationships
Sibling selectors target elements that share the same parent in the HTML document tree. These selectors are powerful for styling sequences of elements without affecting all similar elements.
Adjacent Sibling Selector (+)
The adjacent sibling selector targets the element that is immediately after another specific element. It uses the plus symbol +
between two selectors.
Syntax
element1 + element2 { /* styles here */ }
Example: Form Labels
/* Targets labels immediately after inputs */ input + label { color: #e91e63; font-weight: bold; margin-left: 8px; } /* Only affects the first element after input */ input + span { display: block; font-size: 0.8em; color: #9c27b0; margin-top: 4px; }
General Sibling Selector (~)
The general sibling selector targets all matching elements that follow the first element, regardless of how many elements are in between.
Syntax
element1 ~ element2 { /* styles here */ }
Example: Task List
/* Targets all checkboxes that are checked */ input:checked ~ span { text-decoration: line-through; color: #9e9e9e; } /* Styles all labels after a checked checkbox */ input:checked ~ label { color: #4caf50; font-weight: bold; } /* Affects all paragraphs after the heading */ h3 ~ p { margin-left: 20px; }
Daily Tasks
Complete all tasks by end of day
Comparing Sibling Selectors
Understanding the difference between adjacent and general sibling selectors is crucial for writing precise CSS.
Selector | Symbol | Targets | Specificity | Example |
---|---|---|---|---|
h2 + p |
+ | First paragraph immediately after any h2 | (0,0,0,2) | First paragraph after a heading |
h2 ~ p |
~ | All paragraphs that follow an h2 | (0,0,0,2) | All paragraphs after a heading |
.active + li |
+ | List item immediately after an .active item | (0,0,1,1) | Next item in a list |
.active ~ li |
~ | All list items after an .active item | (0,0,1,1) | All subsequent items in a list |
Visual Comparison
Practical Applications
Sibling selectors are powerful tools for creating clean, maintainable styles in real-world scenarios:
Form Validation
Highlight error messages that appear immediately after invalid form inputs.
Task Lists
Style completed tasks differently when their checkbox is checked.
Content Styling
Add special styling to the first paragraph after each heading.
Data Tables
Highlight rows that follow a row with specific status or class.
Real-world Example: Notification System
/* Base notification styling */ .notification { padding: 15px; margin: 10px 0; border-radius: 4px; } /* Info notification */ .notification.info { background: #e3f2fd; border-left: 4px solid #2196f3; } /* Success notification */ .notification.success { background: #e8f5e9; border-left: 4px solid #4caf50; } /* Warning notification */ .notification.warning { background: #fff8e1; border-left: 4px solid #ffc107; } /* Target notifications that follow an error */ .notification.error ~ .notification { margin-top: 20px; background: rgba(0,0,0,0.03); } /* Style the notification immediately after an error */ .notification.error + .notification { border-top: 2px solid #f44336; }
Best Practices
Follow these guidelines for effective sibling selector usage:
.active ~ li
is more targeted than div ~ p
.
Common Pitfalls to Avoid
Pitfall | Problem | Solution |
---|---|---|
div + div |
Too generic, affects many unintended elements | Add classes for specificity |
h1 ~ * |
Affects all elements after h1 | Use more specific selectors |
.active + .active |
Only works if elements are adjacent | Use general sibling instead |
input:checked ~ label |
Only works if label comes after input | Ensure proper HTML structure |
Next Steps
Now that you understand sibling selectors, continue to the next lesson to learn about CSS precedence rules.