CSS selectors and element states
W3C CSS Snapshot 2026
Select elements by their neighbors, position in a list, or current state.
On this page
Select siblings with + and ~
Both selectors look for elements under the same parent, after the first match. Use + for the immediate next sibling. Use ~ to match any later sibling, even when another element sits between them.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Select siblings with + and ~</title>
<style>
h2 + p { font-weight: bold; }
h2 ~ p { color: teal; }
</style>
</head>
<body>
<section>
<h2>Workshop</h2>
<p>Registration opens Monday.</p>
<hr>
<p>Bring a notebook.</p>
</section>
<p>See the full schedule online.</p>
</body>
</html>
What to expect The first paragraph is bold. Both paragraphs inside the section are teal, even with a divider between them. The last paragraph has a different parent, so neither selector matches it.
Select items by position
:nth-child(even) matches the second, fourth, and other even positions under the same parent. Change even to odd to select the first, third, and other odd positions.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Select items by position</title>
<style>
li:nth-child(even) { background: #def5f5; color: #123; }
</style>
</head>
<body>
<ul>
<li>Check in</li>
<li>Pick up materials</li>
<li>Find a seat</li>
<li>Start the activity</li>
</ul>
</body>
</html>
What to expect The second and fourth items have a colored background.
Style checked, disabled, and focused controls
:checked matches a selected checkbox or radio button. :disabled matches a disabled control. :focus-visible is useful for a visible focus outline during keyboard navigation.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Style checked, disabled, and focused controls</title>
<style>
input:checked + label { font-weight: bold; }
button:disabled { opacity: 0.6; }
:focus-visible { outline: 3px solid #007c80; outline-offset: 3px; }
</style>
</head>
<body>
<input type="checkbox" id="updates">
<label for="updates">Email me updates</label>
<button type="button" disabled>Unavailable</button>
</body>
</html>
What to expect Use Tab to reach the checkbox and Space to select it. The label becomes bold.