CSS Combinators
Master CSS combinators to create precise element relationships. Target elements based on their position and hierarchy in the DOM structure.
What are CSS combinators?
Combinators select elements based on their relationship to other elements. A space selects descendants (div p), > selects direct children (ul > li), + selects the next sibling (h2 + p), and ~ selects all following siblings (h2 ~ p).
article p { } /* any <p> inside article */
ul > li { } /* only direct children */
h2 + p { } /* the <p> right after an h2 */
h2 ~ p { } /* every <p> after an h2 (same parent) */Interactive CSS Combinators Demo
Code Editor
<div class="box">
<p>Direct child paragraph</p>
<section><p>Nested (descendant) paragraph</p></section>
<h3>Heading</h3>
<p>Right after the heading (adjacent sibling)</p>
<p>Later sibling</p>
</div>Preview
Descendant Combinator (Space)
Basic Descendant Selection
/* Select all p elements inside div elements */
div p {
color: #1f2937;
line-height: 1.6;
}
/* Select all links inside navigation */
.navigation a {
text-decoration: none;
padding: 0.5rem 1rem;
border-radius: 4px;
}
/* Multiple level descendant */
.article .sidebar p {
font-size: 0.9em;
color: #6b7280;
}
/* Complex descendant selectors */
.form-container .form-group label {
display: block;
font-weight: 500;
margin-bottom: 0.5rem;
}The descendant combinator selects elements at any level within an ancestor.
Navigation Styling
/* Style all navigation links */
nav ul li a {
display: block;
padding: 0.75rem 1rem;
color: #374151;
transition: all 0.2s;
}
nav ul li a:hover {
background: #3b82f6;
color: white;
}
/* Dropdown styles */
.dropdown ul li a {
font-size: 0.9em;
padding-left: 2rem;
}Style navigation elements at any depth
Content Area Styling
/* Article content */
.article p {
margin: 1rem 0;
line-height: 1.7;
}
.article h2 {
color: #1f2937;
margin-top: 2rem;
}
/* Sidebar content differs */
.article .sidebar p {
font-size: 0.9em;
margin: 0.5rem 0;
}Apply different styles to nested content areas
Child Combinator (>)
Direct Child Selection
/* Select only direct children */
.menu > li {
display: inline-block;
margin-right: 1rem;
}
/* Nested lists won't be affected */
.nav-list > li {
background: #f3f4f6;
border-radius: 6px;
}
/* Form group direct children */
.form-group > label {
font-weight: 600;
color: #374151;
}
.form-group > input {
width: 100%;
padding: 0.75rem;
border: 2px solid #d1d5db;
}
/* Cards direct children */
.card > h3 {
margin-top: 0;
color: #1f2937;
}Target only immediate children, not deeper descendants.
Layout Control
/* Grid container direct children */
.grid > .grid-item {
padding: 1rem;
background: #f9fafb;
border-radius: 8px;
}
/* Flex container children */
.flex-container > div {
flex: 1;
margin: 0 0.5rem;
}
/* Section direct headings */
.section > h2 {
border-bottom: 2px solid #3b82f6;
padding-bottom: 0.5rem;
}Control layout for direct children only
Component Boundaries
/* Card component children */
.card > .card-header {
background: #f8fafc;
padding: 1rem;
border-bottom: 1px solid #e2e8f0;
}
.card > .card-body {
padding: 1rem;
}
.card > .card-footer {
background: #f1f5f9;
padding: 0.75rem 1rem;
}
/* Modal direct children */
.modal > .modal-backdrop {
position: fixed;
inset: 0;
}Maintain component structure boundaries
Adjacent Sibling Combinator (+)
Immediate Sibling Selection
/* Paragraph immediately after heading */
h2 + p {
font-size: 1.1em;
color: #3b82f6;
font-weight: 500;
}
/* Input immediately after label */
label + input {
margin-top: 0.5rem;
border: 2px solid #d1d5db;
}
/* Error message after input */
input + .error-message {
display: block;
color: #dc2626;
font-size: 0.875rem;
margin-top: 0.25rem;
}
/* Button after form field */
.form-field + .submit-btn {
margin-top: 1.5rem;
}Style elements that immediately follow another element.
Typography Flow
/* First paragraph after heading */
h1 + p,
h2 + p,
h3 + p {
margin-top: 0.5rem;
font-weight: 500;
}
/* Quote after paragraph */
p + blockquote {
margin-top: 1.5rem;
border-left: 4px solid #3b82f6;
}
/* Code block after paragraph */
p + pre {
margin-top: 1rem;
}Create proper typography flow and spacing
Form Validation
/* Invalid input + error message */
input:invalid + .error {
display: block;
color: #dc2626;
}
/* Valid input + success message */
input:valid + .success {
display: block;
color: #059669;
}
/* Checkbox + label styling */
input[type="checkbox"] + label {
cursor: pointer;
user-select: none;
}
/* Radio + label styling */
input[type="radio"]:checked + label {
font-weight: bold;
color: #3b82f6;
}Style form feedback and labels
General Sibling Combinator (~)
All Following Siblings
/* All paragraphs after heading */
h2 ~ p {
padding-left: 1rem;
border-left: 2px solid #e5e7eb;
}
/* All elements after sidebar */
.sidebar ~ * {
background: #f0fdf4;
margin-left: 2rem;
}
/* All form fields after error */
.error-field ~ .form-field {
opacity: 0.7;
}
/* All list items after active */
.nav-item.active ~ .nav-item {
opacity: 0.6;
}Style all following siblings at the same level.
Advanced Combinator Combinations
/* Complex combinations */
.article > h3 + p {
/* Direct child h3, followed by p */
font-weight: 600;
color: #1e40af;
}
.form-group > label + input:focus {
/* Input focused, immediately after label, direct child of form-group */
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
.nav-list > li:hover > a {
/* Link inside hovered list item */
background: #3b82f6;
color: white;
}
.content-section > .article > .sidebar ~ p {
/* Paragraphs after sidebar within article */
background: #f0fdf4;
padding: 1rem;
}Combine multiple combinators for precise targeting.
Best Practices & Performance
Best Practices
- • Use child combinator for better performance
- • Avoid overly complex combinator chains
- • Combine with classes for specificity
- • Test in different HTML structures
- • Use semantic HTML structure
- • Keep selectors readable and maintainable
Common Use Cases
- • Form field and label relationships
- • Typography flow and spacing
- • Navigation menu styling
- • Content area organization
- • Component boundary definition
- • Error message positioning
CSS Combinators — step by step
Descendant Combinator (space)
div p selects all p elements anywhere inside a div — at any nesting depth. This is the most common combinator but also the broadest in scope.
🎯 Quick Check
Which combinator selects only direct children, not deeper descendants?
CSS Combinators: summary
Key points
- The descendant combinator (space) matches at any depth;
>matches exactly one level down. - Sibling combinators only look forward. CSS can't select a previous sibling directly (except with
:has()). * + *(the "lobotomised owl") adds spacing between siblings without touching the first one.
Frequently asked questions
›What is the difference between > and a space in CSS?
div > p matches only <p> elements that are direct children of the div. div p matches <p> elements at any depth inside it.
›How do I select a previous sibling?
Use :has(): h2:has(+ p) selects an h2 that is directly followed by a p.