-
Hajipur, Bihar, 844101
CSS Combinators define relationships between elements. They allow you to target elements based on their structural relationship to other elements in the DOM.
There are 4 main types of CSS combinators:
Descendant (space
)
Child (>
)
Adjacent sibling (+
)
General sibling (~
)
A B
)Targets all B elements inside A, at any depth.
div p {
color: blue;
}
All
<p>
elements inside<div>
, including nested ones.
A > B
)Targets B elements that are direct children of A.
ul > li {
list-style-type: square;
}
Only
<li>
elements directly inside<ul>
(not deeper nested).
A + B
)Targets B element immediately after A, sharing the same parent.
h2 + p {
color: red;
}
Styles the first
<p>
that comes right after an<h2>
.
A ~ B
)Targets all B elements after A, sharing the same parent.
h2 ~ p {
font-style: italic;
}
All
<p>
elements that are siblings after an<h2>
.
Q1. Style all <p>
elements inside a <section>
.
Q2. Change color of direct <li>
children of a <ul>
.
Q3. Style a <p>
that comes right after any <h1>
.
Q4. Italicize all paragraphs after an <h2>
in a section.
Q5. Remove bullet from top-level <li>
only.
Q6. Change font of <span>
inside <div class="note">
.
Q7. Apply border to input elements that come directly after a <label>
.
Q8. Make all sibling <img>
tags after a <figure>
50% width.
Q9. Target only the first <p>
after a <div>
tag.
Q10. Apply underline to all <a>
tags that come after a <nav>
.
Q1: Which combinator selects any descendant?
Q2: Which combinator selects only direct children?
Q3: Which combinator selects an element immediately following another?
Q4: Which combinator selects all siblings after an element?
Q5: What does div p select?
Q6: What does ul > li select?
Q7: What does h2 + p select?
Q8: What does h2 ~ p select?
Q9: Which combinator is most specific (least inclusive)?
Q10: What is the output of nav a?