CSS Adjacent Sibling Selectors: Complete Guide to Next Element Targeting

CSS adjacent sibling selectors are powerful tools that allow you to target elements based on their relationship to other elements in the DOM. The adjacent sibling selector (+) specifically targets an element that immediately follows another element at the same hierarchical level.

What is the Adjacent Sibling Selector?

The adjacent sibling selector uses the plus sign (+) to select an element that is the immediate next sibling of another element. Both elements must share the same parent and the target element must come directly after the reference element in the HTML structure.

Syntax: element1 + element2

This selects element2 that immediately follows element1

Basic Syntax and Structure

The general syntax follows this pattern:

selector1 + selector2 {
    /* CSS properties */
}

Where:

  • selector1 is the reference element
  • + is the adjacent sibling combinator
  • selector2 is the target element to be styled

Simple Example

Let’s start with a basic example to understand how adjacent sibling selectors work:

HTML:

<h2>Main Heading</h2>
<p>This paragraph immediately follows the heading.</p>
<p>This is the second paragraph.</p>

CSS:

h2 + p {
    color: #007bff;
    font-weight: bold;
    margin-top: 0;
}

Visual Output:

Main Heading

This paragraph immediately follows the heading.

This is the second paragraph.

In this example, only the first paragraph gets styled because it’s the immediate sibling following the h2 element.

Practical Use Cases

1. Styling Form Elements

Adjacent sibling selectors are particularly useful for styling form elements and their labels:

CSS:

input[type="text"]:focus + label {
    color: #28a745;
    font-weight: bold;
}

input[type="checkbox"] + label {
    margin-left: 8px;
    cursor: pointer;
}

HTML:

<input type="text" id="username">
<label for="username">Username</label>

<input type="checkbox" id="agree">
<label for="agree">I agree to the terms</label>

2. Navigation Menu Styling

Create spacing between navigation items:

CSS:

nav li + li {
    border-left: 1px solid #ddd;
    margin-left: 15px;
    padding-left: 15px;
}

nav a + a {
    margin-left: 20px;
}

Visual Result:

Interactive Example: Tab Content

Here’s an interactive example showing how adjacent sibling selectors can create tab functionality without JavaScript:


HTML Content

This tab shows HTML-related information. The content is revealed using CSS adjacent sibling selectors.


CSS Content

This tab demonstrates CSS functionality. Notice how we’re using the + selector to show/hide content.


JavaScript Content

This tab is about JavaScript, but ironically, no JavaScript was used to create this tab functionality!

CSS for the tabs:

.tab-input:checked + .tab-label {
    background: #007bff;
    color: white;
}

.tab-input:checked + .tab-label + .tab-content {
    display: block;
}

Advanced Techniques

Combining with Other Selectors

Adjacent sibling selectors can be combined with other CSS selectors for more specific targeting:

/* Target span that follows a strong element within a paragraph */
p strong + span {
    font-style: italic;
    color: #666;
}

/* Target any element with class 'highlight' that follows an h3 */
h3 + .highlight {
    background: yellow;
    padding: 10px;
}

/* Target the first paragraph after any heading */
h1 + p, h2 + p, h3 + p {
    font-size: 1.1em;
    margin-top: 0;
}

Creating Visual Separators

Use adjacent sibling selectors to add visual elements between content blocks:

CSS:

article + article::before {
    content: "";
    display: block;
    height: 2px;
    background: linear-gradient(to right, #007bff, transparent);
    margin: 30px 0;
}

Visual Result:

First Article

This is the content of the first article.

Second Article

This article has a separator line above it, created using the adjacent sibling selector.

Common Pitfalls and Solutions

1. Understanding “Adjacent” vs “General” Siblings

The adjacent sibling selector (+) only targets the immediate next sibling, not all following siblings. For targeting all following siblings, use the general sibling selector (~):

/* Adjacent sibling - only immediate next */
h2 + p { color: blue; }

/* General sibling - all following siblings */
h2 ~ p { color: red; }

2. Whitespace and Text Nodes

Whitespace between elements doesn’t affect adjacent sibling relationships, but text nodes do. Be careful with your HTML structure:

Won’t work:

<div>Some text</div>
Text node here
<div>Next div</div>

Will work:

<div>Some text</div>
<div>Next div</div>

Browser Support and Compatibility

The adjacent sibling selector has excellent browser support and has been available since CSS2. It works in:

  • All modern browsers (Chrome, Firefox, Safari, Edge)
  • Internet Explorer 7 and above
  • Mobile browsers
✅ Great for production use – No polyfills or fallbacks needed for the adjacent sibling selector.

Performance Considerations

Adjacent sibling selectors are relatively efficient because they only need to check one element (the immediate sibling). However, keep these tips in mind:

  • Avoid overly complex chained selectors
  • Combine with class selectors when possible for better performance
  • Test performance with large DOMs if using extensively

Real-World Examples

Alert Messages

Create contextual styling for alert messages that follow form inputs:

input:invalid + .error-message {
    display: block;
    color: #dc3545;
    font-size: 0.875em;
    margin-top: 5px;
}

input:valid + .error-message {
    display: none;
}

Accordion Interfaces

Build collapsible content sections:

.accordion-toggle:checked + .accordion-header + .accordion-content {
    max-height: 200px;
    padding: 15px;
    opacity: 1;
}

.accordion-content {
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transition: all 0.3s ease;
}

Best Practices

  1. Keep HTML structure clean – Adjacent sibling selectors rely on proper document structure
  2. Use semantic HTML – This makes your selectors more meaningful and maintainable
  3. Combine with classes for specificity – e.g., .button + .button instead of just button + button
  4. Document your intentions – Comment complex adjacent sibling selector usage
  5. Test across different layouts – Ensure selectors work when content changes

Conclusion

CSS adjacent sibling selectors provide a powerful way to style elements based on their relationship to preceding elements. They’re particularly useful for creating interactive interfaces, styling form elements, and adding visual enhancements without JavaScript. Master this selector to write more efficient and maintainable CSS code.

The key to success with adjacent sibling selectors is understanding the DOM structure and remembering that they only target the immediate next sibling. With practice, you’ll find numerous opportunities to use this selector in your web development projects.