Master Modern CSS Selectors to Simplify and Strengthen Your Stylesheets
This article introduces advanced CSS selector techniques—including :is(), :where(), :has(), attribute wildcards, :nth-child variations, :not(), @layer, :focus-visible, :empty, sibling combinators, composite selectors, and container queries—to help developers write cleaner, more maintainable, and more responsive CSS code.
CSS selector techniques directly affect code quality and maintainability. Below are next‑generation selector tricks that make code cleaner and stylesheets more maintainable.
1. :is() selector – the ultimate grouping tool
The :is() selector groups multiple selectors, dramatically reducing repetition.
/* previous syntax */
header p,
main p,
footer p {
line-height: 1.6;
}
/* using :is() */
:is(header, main, footer) p {
line-height: 1.6;
}2. :where() selector – zero‑specificity blessing
The :where() works like :is() but has a specificity of 0, making styles easier to override.
3. :has() relational selector – parent selection revolution
The :has() selector finally allows selecting a parent element based on its children.
/* select paragraphs containing an image */
p:has(img) {
display: flex;
align-items: center;
}
/* select paragraphs followed by a heading */
p:has(+ h2) {
margin-bottom: 2em;
}4. Attribute selector wildcard matching
Wildcard matching in attribute selectors provides flexible element selection.
5. :nth-child() advanced usage
Use formulas to target specific elements for complex selection patterns.
/* select every element after the 4th */
li:nth-child(n+4) {
margin-top: 1em;
}
/* select the first five elements */
li:nth-child(-n+5) {
font-weight: bold;
}6. :not() multi‑condition exclusion
The new :not() supports multiple selectors, greatly enhancing exclusion capabilities.
7. Layer management with @layer
Use @layer to control style priority and reduce specificity wars.
@layer base, components, utilities;
@layer base {
/* base styles */
}
@layer components {
/* component styles */
}8. :focus-visible – intelligent focus handling
Provides smarter focus state management, reducing unnecessary outlines.
/* show focus only on keyboard interaction */
button:focus-visible {
outline: 2px solid blue;
outline-offset: 2px;
}9. :empty – handling empty elements gracefully
Elegant handling of empty elements without extra class markers.
/* hide empty elements */
section:empty {
display: none;
}10. Adjacent sibling combinators
Flexibly use + and ~ selectors to manage element relationships.
/* select adjacent list items */
li + li {
margin-top: 1rem;
}
/* select all following paragraphs */
h2 ~ p {
padding-left: 1em;
}11. Composite selector optimization
Combine multiple selector conditions for precise matching.
/* satisfy multiple conditions */
button[type="submit"]:not(:disabled):hover {
transform: translateY(-1px);
}12. Container queries for responsive design
Use container queries to achieve more precise responsive layouts.
@container sidebar (min-width: 400px) {
.widget {
display: grid;
grid-template-columns: 1fr 1fr;
}
}JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
