Questions
15 of 25
1What are CSS combinators? How many types of CSS combinators are there?
2What is the purpose of using combinators in CSS?
3Can you list all the types of CSS combinators?
4What is the syntax for a combinator selector?
5What is the descendant combinator in CSS?
6How does the descendant combinator differ from a direct child combinator? Write an example of a descendant combinator selector. What is the impact of using multiple descendant selectors in a chain?
7How can overusing descendant combinators affect performance?
8How is the child combinator different from the descendant combinator?
9Write an example using the child combinator.
10Can the child combinator select a grandchild element? Why or why not?
11When would you prefer to use the child combinator instead of the descendant combinator?
12Can the adjacent sibling combinator select multiple elements? Write an example using the adjacent sibling combinator.
13How does the adjacent sibling combinator (+) differ from the general sibling combinator (~)? What is the general sibling combinator (~) used for?
14When would you use the general sibling combinator (~) instead of the adjacent sibling combinator (+)?
15Can the general sibling combinator (~) select elements that appear before the reference element?
16What happens if the sibling element doesn’t exist in the DOM? Can you combine multiple combinators in a single selector? Give an example.
17Can combinators be used together with pseudo-classes or pseudo-elements?
18What happens if you use a combinator between unrelated elements?
19How do combinators affect CSS inheritance and cascade?
20How would you select all <p> elements that are inside a <div> using combinators? How would you style only the direct child <li> elements inside a <ul>?
21How would you change the color of a paragraph that immediately follows a heading?
22How can you style all <span> elements that come after a specific <div>?
23Write a selector to target all <a> tags inside a <nav> but not inside a <footer>. How would you use combinators to create alternating styles between elements?
24What happens when combinators are used inside media queries? How can combinators improve maintainability in large CSS projects?
25Give a real-world example of using multiple combinators in a single rule.
15 / 25

Can the general sibling combinator (~) select elements that appear before the reference element?

Limitations of the General Sibling Combinator

No, the general sibling combinator (~) can only select elements that appear after the reference element in the DOM. It cannot select elements that precede the reference element.

Key Points
  1. 1

    A ~ B selects all B elements that are siblings following A.

  2. 2

    Elements before A are not targeted, as CSS selectors cannot traverse backward in the DOM.

  3. 3

    If you need to style elements before a reference element, you must use a parent selector, JavaScript, or restructure the HTML.

Example: General Sibling Combinator Limitation
Difficulty: 3/10
Topics: CSS sibling combinators, selector precedence, DOM traversal

Scenario Questions

0-2 years experience
  1. 1

    You're trying to style all paragraphs that come after a heading with class 'intro', but your CSS rule h1 ~ p { color: blue; } isn't working on a paragraph that appears before the h1. What's going on?

  2. 2

    If you write .button ~ .tooltip { display: block; } and the tooltip appears before the button in the HTML, will the tooltip ever show up? Why or why not?

  3. 3

    You have a list of cards and want to add margin-top to every card after the first one. You tried .card ~ .card { margin-top: 1rem; }, but it's not working on the second card. What's the mistake?

2-5 years experience
  1. 1

    A designer says the 'related articles' section should be highlighted when the user hovers over the main content block, but the related articles appear before the main block in the HTML. You can't restructure the HTML. How do you solve this without JavaScript?

  2. 2

    Your team's CSS component library uses ~ to style adjacent form elements, but in one page, a dynamic ad injects a div between two form fields, breaking the styling. Why did this happen, and how would you fix it without changing the markup?

  3. 3

    A bug report says 'the warning banner isn't turning red when the error message appears' — both are siblings, but the banner is before the error message in the DOM. The CSS uses .error ~ .banner. What's the root cause, and how do you explain this to a junior dev?

5-8 years experience
  1. 1

    You're building a reusable content block that dynamically inserts ads or banners before or after the main content based on user segments. How do you design a CSS architecture that reliably styles adjacent elements regardless of their order, without relying on JS or class toggling?

  2. 2

    In a legacy CMS, content blocks are rendered in unpredictable order due to templating logic. You need to style elements conditionally based on sibling presence, but ~ doesn't work backwards. What alternatives would you consider — and what are the performance, maintainability, and accessibility tradeoffs?

  3. 3

    A component library uses ~ combinators extensively for spacing between siblings. As the app scales, you notice layout shifts when dynamic content is injected. How do you audit and refactor this to avoid brittle styling dependencies?

8+ years experience
  1. 1

    You're leading a cross-team effort to migrate a legacy UI to a new design system. The old codebase relies heavily on ~ combinators to style elements based on assumed DOM order, but the new system uses React with dynamic rendering that breaks those assumptions. How do you architect a migration strategy that decouples styling from DOM structure without introducing technical debt?

  2. 2

    Your company's design system uses CSS sibling combinators for spacing and theming across hundreds of pages. As you scale to internationalized, user-generated content with unpredictable DOM structures, you're seeing inconsistent styling. How do you evolve the system to be order-agnostic while preserving performance and maintainability across teams?

  3. 3

    A legacy product uses ~ combinators to enforce visual hierarchy in a CMS where content editors can reorder blocks arbitrarily. You're proposing a shift to a CSS-in-JS or utility-first approach. What are the architectural, cultural, and long-term maintenance tradeoffs you'd present to stakeholders?

Follow-up Questions

  • What would you use instead if you needed to style an element based on a later sibling?
  • Have you ever debugged a layout where this tripped you up?
  • How does this compare to the adjacent sibling combinator (+)?