Sample questions
Question 1Easy
What does the selector article:has(img) match?
- Articles that come right after an img
- Images that are inside an article
- Articles that contain an img
- Any article when an img exists anywhere
Answer: C. Articles that contain an img
Explanation
:has() is a relational pseudo-class: it matches the element if the argument selector matches something relative to it (descendants by default).
Step-by-step solution
- The has pseudo-class matches an element based on what it contains.
- Article:has(img) matches an article that has an img descendant.
- So it matches articles that contain an img (option C).
Why the other options are wrong
- A. That would be a sibling selector, not has.
- B. Matching the images is img inside article, not article:has(img).
- D. The match is limited to articles that contain an image, not any article on the page.
Tip: Use has to select a parent based on its children, such as cards with images.
Question 2Easy
What do container queries let a component respond to?
- The page scroll position
- Its container's size
- The user's device model
- The viewport width only
Answer: B. Its container's size
Explanation
Container queries (@container) style elements based on an ancestor container's size, so a component adapts wherever it is placed.
Step-by-step solution
- Container queries style an element based on the size of an ancestor container.
- So a component adapts to the space it is given, wherever it is placed.
- The answer is the container's size (option B).
Why the other options are wrong
- A. Scroll position is not what container queries check.
- C. Device model is not a container query feature.
- D. Viewport width is for media queries, not container queries.
Tip: Use container queries for reusable components that should adapt to their container.
Question 3Easy
What does color: var(--accent, red) do if --accent is not defined?
- Makes the whole rule invalid and ignored
- Uses the value of the parent element's accent
- Uses the browser default text colour, never red
- Uses red as the fallback
Answer: D. Uses red as the fallback
Explanation
The second argument of var() is the fallback used when the property is not set.
Step-by-step solution
- The second argument of var is the fallback value.
- It is used when the custom property is not set.
- So the colour falls back to red (option D).
Why the other options are wrong
- A. The rule is valid, and the fallback is used.
- B. It does not look up the parent's accent; it uses the given fallback.
- C. The fallback is red, not the browser default.
Tip: Give var a fallback so the page still looks right when a theme variable is missing.
Question 4Easy
Unlike Sass variables, CSS custom properties:
- Can change at runtime in the browser
- Can only hold colour values, not lengths
- Are removed when the stylesheet is compiled
- Cannot be inherited by child elements
Answer: A. Can change at runtime in the browser
Explanation
Custom properties are part of the cascade at runtime, so JavaScript, media queries or classes can change them; Sass variables are compiled away.
Step-by-step solution
- CSS custom properties are part of the cascade at runtime.
- JavaScript, media queries or classes can change them while the page runs.
- Sass variables are compiled away and cannot change in the browser (option A).
Why the other options are wrong
- B. Custom properties can hold any value, including lengths.
- C. Custom properties are kept in the output, unlike Sass variables.
- D. Custom properties are inherited by child elements.
Tip: Use custom properties for values that must change at runtime, such as themes.
Question 5Medium
What do you need to enable before writing nested CSS rules in modern browsers?
- You must set nesting: enabled on :root first
- Nothing; nesting is native
- Nesting needs the @nest at-rule on every rule
- Only @layer blocks may contain nested rules
Answer: B. Nothing; nesting is native
Explanation
Modern CSS supports nesting natively, e.g. .card { & .title { ... } }, similar to Sass.
Step-by-step solution
- Modern browsers support CSS nesting natively.
- A rule can contain child rules using & for the parent selector.
- No flag, root setting or at-rule is needed.
- So the correct answer is nothing; nesting is native.
Why the other options are wrong
- A. There is no nesting property on :root.
- C. There is no @nest at-rule; nesting uses plain rules with &.
- D. Nested rules are allowed in normal rules, not only in @layer blocks.
Tip: Native CSS nesting works like Sass nesting, with & for the parent selector.
Question 6Medium
Custom properties are inherited. If :root sets --gap: 8px and .card sets --gap: 16px, what does a button inside .card get from var(--gap)?
- 24px
- 8px
- 16px
- 0px
Answer: C. 16px
Explanation
The nearest ancestor that sets the property wins through inheritance, just like other inherited properties.
Step-by-step solution
- .card sets --gap to 16px, so the custom property is overridden there.
- The button inside .card inherits custom properties from its nearest ancestor that sets them.
- Its nearest such ancestor is .card, not :root.
- So var(--gap) gives 16px, the correct answer.
Why the other options are wrong
- A. 24px is not set anywhere; it is 8px + 16px added wrongly.
- B. 8px comes from :root, but .card is closer and overrides it.
- D. 0px is not set anywhere; there is no fallback in var(--gap).
Tip: Custom properties inherit like normal properties: the nearest ancestor that sets the value wins.
Question 7Medium
What does h2:has(+ p) select?
- Every h2 that has any sibling element at all
- h2 elements immediately followed by a p
- p elements immediately after an h2
- h2 elements that contain a p
Answer: B. h2 elements immediately followed by a p
Explanation
Inside :has(), a leading + is relative to the subject, so it checks for an adjacent next sibling p.
Step-by-step solution
:has() selects an element based on what relates to it.h2:has(+ p) means an h2 that has an adjacent next sibling p.- So it selects h2 elements immediately followed by a
p.
Why the other options are wrong
- A. It requires a p sibling, not any sibling.
- C. The subject is the h2, not the p.
- D. Containing would be h2:has(p).
Tip: The element before :has is what gets selected.
Question 8Medium
What does clamp(1rem, 2.5vw, 2rem) return?
- 2.5vw, limited to between 1rem and 2rem
- The smallest of the three values
- The average of 1rem, 2.5vw and 2rem
- Always 2rem on any screen wider than 1rem
Answer: A. 2.5vw, limited to between 1rem and 2rem
Explanation
clamp(min, preferred, max) uses the preferred value but never goes below min or above max, useful for fluid type.
Step-by-step solution
- clamp(min, preferred, max) uses the preferred value when it falls in range.
- Here the preferred value is 2.5vw.
- It is kept between the minimum 1rem and the maximum 2rem.
- So the correct answer is 2.5vw limited to between 1rem and 2rem.
Why the other options are wrong
- B. Clamp is not a minimum of the three; it is the preferred value kept inside the range.
- C. Clamp does not average its values.
- D. 2rem is only the maximum; the result changes with the viewport width.
Tip: clamp(min, preferred, max): the middle value is used, bounded by the two ends.
Question 9Hard
A theme sets --size: 10px on .box and its CSS uses width: calc(var(--size) * 2). JavaScript runs box.style.setProperty("--size", "30px"). What is the box width?
- 30px
- 40px
- 20px
- 60px
Answer: D. 60px
Explanation
The inline custom property overrides the stylesheet value, and calc() re-evaluates with it: 30px × 2 = 60px.
Step-by-step solution
- The inline style sets --size to 30px, overriding the stylesheet 10px.
- calc(var(--size) * 2) is re-evaluated with 30px.
- 30px × 2 = 60px.
- So the correct answer is 60px.
Why the other options are wrong
- A. 30px is the custom property value, not the width after multiplying by 2.
- B. 40px would need a value of 20px for --size, which is not set.
- C. 20px is the old 10px value doubled, but the inline style replaced it.
Tip: Inline style values override stylesheet custom properties, and calc() uses the current value.
Question 10Hard
What do cascade layers (@layer) change about how conflicts are resolved?
- Layers only apply to !important rules
- The last layer in the file always wins
- Layer order is checked before specificity
- Specificity is ignored inside every layer
Answer: C. Layer order is checked before specificity
Explanation
For normal declarations, rules in a later layer beat earlier layers regardless of selector specificity; unlayered styles beat layered ones.
Step-by-step solution
- Cascade layers are compared in their declared order for normal declarations.
- A rule in a later layer beats one in an earlier layer.
- This check happens before specificity is compared.
- So the correct answer is that layer order is checked before specificity.
Why the other options are wrong
- A. Layers apply to normal declarations too, not only to !important ones.
- B. It is the layer order that matters, not that the last layer in the file always wins.
- D. Specificity still matters inside a layer; it is just checked after layer order.
Tip: For normal declarations: unlayered styles beat layered ones, and later layers beat earlier layers, before specificity.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →