C

CSS Handbook

Clean • Professional

CSS Advanced Selectors — Interview Questions & Answers

3 minute

CSS Advanced Selectors — Interview Questions & Answers

Ques: What are CSS Attribute Selectors?

Ans: Attribute selectors style elements based on their attributes or attribute values.

SelectorDescriptionExample
[attr]Selects elements with a given attribute[title] { color: blue; }
[attr="value"]Matches an exact valueinput[type="text"] { color: red; }
[attr~="value"]Matches a word in a space-separated list[class~="active"]
[attr^="value"]Matches value starting with given texta[href^="https"]
[attr$="value"]Matches value ending with textimg[src$=".png"]
[attr*="value"]Matches value containing texta[href*="login"]

Example:

a[href^="https"] { color: green; }    /* Starts with https */
img[src$=".jpg"] { border: 2px solid red; }  /* Ends with .jpg */

Ques: What are CSS combinators?

Ans: Combinators define relationships between elements — helping target nested or sibling elements.

CombinatorDescriptionExample
A BDescendant — B inside Adiv p
A > BChild — Direct childul > li
A + BAdjacent sibling — B immediately after Ah1 + p
A ~ BGeneral sibling — All B siblings after Ah1 ~ p

Example:

div > p { color: blue; }     /* Direct child */
h1 + p { font-weight: bold; } /* Paragraph right after h1 */

Ques: What are pseudo-classes in CSS?

Ans: Pseudo-classes target elements based on their state, position, or user interaction.

Pseudo-ClassDescriptionExample
:hoverWhen mouse is over an elementa:hover { color: red; }
:focusWhen an element is focusedinput:focus { border-color: blue; }
:activeWhen clickedbutton:active { background: yellow; }
:first-childFirst child of parentli:first-child
:last-childLast child of parentli:last-child
:nth-child(n)Selects specific childrenli:nth-child(2)
:checkedChecked form elementsinput:checked
:disabled / :enabledInput stateinput:disabled

Example:

button:hover { background: #333; color: white; }
input:focus { outline: 2px solid skyblue; }

Ques: What are pseudo-elements in CSS?

Ans: Pseudo-elements create virtual elements you can style — even though they don’t exist in HTML.

Pseudo-ElementDescriptionExample
::beforeAdds content before an elementp::before { content: "→ "; }
::afterAdds content after an elementp::after { content: " ✓"; }
::first-letterStyles first letterp::first-letter { font-size: 2em; }
::first-lineStyles first linep::first-line { color: blue; }
::selectionStyles selected text::selection { background: yellow; }

Example:

h1::before { content: "★ "; color: gold; }
p::after { content: " ✨"; }

Ques: What is the difference between pseudo-class and pseudo-element?

FeaturePseudo-ClassPseudo-Element
PurposeTargets state or condition of an elementTargets part of an element
Syntax: (single colon):: (double colon)
Examplea:hoverp::after

Ques: What are advanced selectors introduced in modern CSS?

Ans: Modern CSS (Selectors Level 4) introduced powerful, readable, and performance-friendly selectors:

SelectorDescriptionExample
:is()Matches any of multiple selectors:is(h1, h2, h3) { color: red; }
:where()Like :is(), but 0 specificity:where(p, li) { margin: 0; }
:has()Parent selector — checks if element contains somethingarticle:has(img)
:not()Excludes elementsp:not(.intro)

Example:

section:has(h2) { border: 1px solid blue; }
:is(h1, h2, h3) { color: darkgreen; }

Ques: What is CSS nesting?

Ans: CSS nesting allows writing selectors inside other selectors, similar to SCSS — making code cleaner.

Example (modern CSS):

.card {
  padding: 10px;

  & h2 {
    color: blue;
  }

  & p {
    font-size: 14px;
  }
}

Ques: What are best practices for using selectors?

  • Avoid over-specific selectors (div.container ul li span)
  • Use class selectors for reusability
  • Prefer semantic HTML and minimal nesting
  • Group selectors to reduce repetition
  • Use modern selectors (:is(), :where(), :has()) for clean, scalable code

Ques: Real-world usage example of multiple selectors

button.primary:hover,
a.button:hover {
  background: #007bff;
  color: #fff;
  transition: 0.3s;
}

 

Article 0 of 0