Learn and master the top 20 CSS Combinators interview questions along with detailed answers and code snippets:

  1. What are CSS combinators?
    CSS combinators are symbols used to select elements based on their relationship to other elements in the document tree. They allow you to target specific elements or groups of elements based on their position or hierarchy.
  2. What are the different types of CSS combinators?
    There are four types of CSS combinators:
  • Descendant combinator (space): Selects an element that is a descendant of another element.
  • Child combinator (>): Selects an element that is a direct child of another element.
  • Adjacent sibling combinator (+): Selects an element that is an immediate sibling and comes immediately after another element.
  • General sibling combinator (~): Selects an element that is a sibling and comes after another element.

3. How do you use the descendant combinator in CSS?
The descendant combinator (space) is used to select an element that is a descendant of another element. It matches any element that is contained within another element.

.container span {
  color: red; /* Selects all <span> elements that are descendants of the .container element */
}
  1. How do you use the child combinator in CSS?
    The child combinator (>) is used to select an element that is a direct child of another element. It matches only the immediate children of the parent element.
.container > div {
  background-color: blue; /* Selects only the immediate <div> children of the .container element */
}
  1. How do you use the adjacent sibling combinator in CSS?
    The adjacent sibling combinator (+) is used to select an element that is an immediate sibling and comes immediately after another element.
h2 + p {
  font-weight: bold; /* Selects the <p> element that comes immediately after an <h2> element */
}
  1. How do you use the general sibling combinator in CSS?
    The general sibling combinator (~) is used to select an element that is a sibling and comes after another element.
h2 ~ p {
  color: green; /* Selects all <p> elements that are siblings and come after an <h2> element */
}
  1. What is the difference between the descendant combinator and the child combinator?
    The descendant combinator (space) selects any element that is a descendant of another element, regardless of the depth of nesting. The child combinator (>) selects only the immediate children of the parent element.
.container span {
  color: red; /* Selects all <span> elements that are descendants of the .container element */
}

.container > span {
  color: blue; /* Selects only the immediate <span> children of the .container element */
}
  1. How do you select an element that has a specific class using a combinator?
    You can combine a combinator with a class selector to select an element that has a specific class.
.container > .item {
  background-color: yellow; /* Selects the immediate children of the .container element with the .item class */
}
  1. How do you select an element that has multiple classes using a combinator?
    You can combine a combinator with multiple class selectors to select an element that has multiple classes.
.container .item.highlighted {
  color: red; /* Selects the .item elements with both the .container and .highlighted classes */
}
  1. How do you select an element that matches multiple conditions using combinators?
    You can combine multiple combinators to select an element that matches multiple conditions.
.container >

 ul ~ p {
  font-style: italic; /* Selects the <p> elements that are siblings and come after a <ul> element, which is a direct child of the .container element */
}
  1. How do you select the first child of an element using a combinator?
    You can use the :first-child pseudo-class along with the child combinator (>) to select the first child of an element.
.container > p:first-child {
  color: red; /* Selects the first <p> element that is a direct child of the .container element */
}
  1. How do you select the last child of an element using a combinator?
    You can use the :last-child pseudo-class along with the child combinator (>) to select the last child of an element.
.container > div:last-child {
  background-color: yellow; /* Selects the last <div> element that is a direct child of the .container element */
}
  1. How do you select the nth child of an element using a combinator?
    You can use the :nth-child() pseudo-class along with the child combinator (>) to select the nth child of an element. This allows you to target specific child elements based on their position.
.container > ul li:nth-child(2n) {
  background-color: gray; /* Selects every even-numbered <li> element that is a direct child of a <ul> element, within the .container element */
}
  1. How do you select the next sibling of an element using a combinator?
    You can use the + combinator to select the next sibling of an element.
h2 + p {
  font-weight: bold; /* Selects the <p> element that comes immediately after an <h2> element */
}
  1. How do you select all siblings after an element using a combinator?
    You can use the ~ combinator to select all siblings that come after an element.
h2 ~ p {
  color: green; /* Selects all <p> elements that are siblings and come after an <h2> element */
}
  1. How do you select an element that is both a descendant and a direct child of another element?
    You can use the combination of a descendant combinator (space) and a child combinator (>) to select an element that is both a descendant and a direct child of another element.
.container div > p {
  color: blue; /* Selects <p> elements that are direct children of <div> elements which are descendants of the .container element */
}
  1. How do you select elements based on their position within a specific parent using a combinator?
    You can use the combination of a child combinator (>) and the :nth-child() pseudo-class to select elements based on their position within a specific parent.
.container > div:nth-child(2) {
  background-color: yellow; /* Selects the second <div> element that is a direct child of the .container element */
}