Selectors and Specificity
Everything that decides which declaration actually applies to an element: what a selector matches, how its weight is scored, and how the cascade breaks the remaining ties. Interviewers open here because "why isn't my style applying" is the single most common real-world CSS bug.
part ofCSSoverview, primer and where to startread it →on this pageshowhide
explore
- Selector Types and Combinators5 questions
- Pseudo-classes5 questions
- Pseudo-elements5 questions
- :is(), :where(), and :has()5 questions
- The Specificity Algorithm4 questions
- Cascade, Origins, and Importance4 questions
- Inheritance and Global Values5 questions
- Cascade Layers (@layer)5 questions
- Native CSS Nesting5 questions
questions
page 2 of 2A user-origin stylesheet declares body { font-size: 20px !important } and the site's author stylesheet declares body { font-size: 12px !important }. Which declaration wins, and what cascade rule decides it?
basics
~20 sThe user's 20px wins. Marking a declaration !important moves it into the important band, where the normal origin order is inverted: important user-agent beats important user, which beats important author. A site cannot override a user's important declaration.
Why would the author of a CSS reset or component library wrap its base selectors in :where(), and what does that choice cost?
basics
~20 sWrapping base selectors in :where() drops them to zero specificity, so any consumer declaration — even a single type selector — overrides them without escalation or !important. The cost is that the library can never win a conflict, including against a consumer's accident.
How does adding !important change which CSS cascade layer wins?
basics
~10 sFor !important author declarations the layer order is reversed: the earliest-declared layer wins, and important unlayered declarations become the weakest of all. So !important inside a reset layer beats !important in a later layer.
How do you load a third-party stylesheet into its own cascade layer using CSS @import, and where must that rule appear in the file?
basics
~20 sWrite @import url("vendor.css") layer(vendor); which places every rule from that file into the vendor layer. The @import must come before all other rules except @charset and @layer ordering statements, so it sits at the very top of the stylesheet.
In CSS, why do :invalid styles appear on empty required fields the moment a form renders, and which selector avoids that?
basics
~20 s:invalid reflects constraint-validation state continuously, so an empty required field is invalid from the moment it is parsed — before anyone types. Use :user-invalid, which matches only after the user has interacted with the field or attempted submission.
When is CSS generated content (a content value on ::before or ::after) the wrong place to put text, and what breaks if meaningful copy lives there?
basics
~20 sGenerated content is a rendering instruction, not document text: it cannot be selected, copied or found with in-page search, translation tools skip it, and how assistive technology announces it varies by engine. Keep it decorative and put meaningful copy in the document.
In the CSS selector `.sidebar ul li a`, which compound is the key selector, and why does matching order matter for how you write descendant chains?
basics
~20 sThe key selector is the rightmost compound, here the a. Browsers test candidate elements against it first and only then walk up the ancestor chain, so a broad rightmost compound with a long chain to its left forces the most ancestor walking.
In CSS, how do declarations coming from a running transition or a running @keyframes animation rank against ordinary declarations and against !important ones?
basics
~20 sAnimation declarations sit above all normal declarations but below every important one, so an !important rule freezes an animated property. Transition declarations sit at the very top of the cascade, above even important declarations, for as long as the transition is running.
In CSS, what happens to a rule when one selector inside :is() is unknown to the browser, and how does that differ from a plain comma-separated selector list?
basics
~20 s:is() and :where() take forgiving selector lists: an unparseable argument is dropped and the rest of the selector still applies. In an ordinary comma-separated selector list, one invalid selector invalidates the entire rule, declarations included.
In CSS, why do width, padding and border have no effect when set on ::selection or ::marker?
basics
~20 sBecause each pseudo-element specification defines its own restricted property set. ::selection only paints over existing text, so it accepts colour, background-color, text-decoration and text-shadow; ::marker accepts font properties, colour and content. Neither generates a box you can size.
In CSS, what does the selector `.stack > * + *` match, and why is it used to space a list of children?
basics
~20 sIt matches every direct child of .stack that has a preceding element sibling — in other words, every child except the first. Applying margin-top there spaces the children apart without leaving a stray margin above the first one or below the last.
You embed a widget's markup into pages whose CSS you do not control. Using CSS's all shorthand, how do all: initial, all: unset and all: revert differ as the widget's reset, and what does all not cover?
basics
~20 sall sets every property to one global keyword: initial gives the specification's blank slate (display becomes inline), unset lets inherited properties come from the host page, and revert restores user-agent styles. all skips direction, unicode-bidi and custom properties, and applies to one element only.
A team removes Sass and rewrites its stylesheets with native CSS nesting. Which behaviours can change even when the nested source looks identical, and what would you check first?
basics
~10 sNative nesting is not a drop-in Sass replacement: & is a real selector with :is()-like specificity rather than text concatenation, so overrides can silently change winner and BEM suffix rules are dropped as invalid.
showing 31–43 of 43