HTML & CSS Interview Question #53

What is a CSS selector?

CSS Fundamentals, Selectors, Cascade, Specificity, Box Model, Units & Typography Mid-Level Intermediate

Quick Interview Answer

A CSS selector identifies the elements to which a style rule applies. Selectors can target element names, classes, IDs, attributes, relationships, states, and structural conditions.

Detailed Explanation

A CSS selector identifies the elements to which a style rule applies. Selectors can target element names, classes, IDs, attributes, relationships, states, and structural conditions.

Understanding selectors is fundamental because maintainable CSS depends on choosing selectors that are expressive without being unnecessarily specific or tightly coupled to the document structure.

Code Example

/* Element selector */
p { line-height: 1.6; }

/* Class selector */
.card { padding: 1rem; }

/* ID selector */
#main-content { max-width: 1200px; }

/* Attribute selector */
input[type="email"] { border: 1px solid #ccc; }