A CSS selector identifies which elements receive a rule’s declarations. In an interview, be ready to explain not just what a selector matches, but what happens when several matching rules set the same property. section 2.2
Listing 2.6 CSS specificity example
const styles = `
.button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.primary-button {
background-color: blue;
color: white;
}
button {
background-color: red;
color: black;
}
.container .primary-button {
background-color: green;
}
#my-button {
color: yellow;
}
`;
interface ButtonProps {
children: React.ReactNode;
id: string;
}
const Button: React.FC<ButtonProps> = ({ children, id }) => (
<button className="button primary-button" id={id}>
{children}
</button>
);
const CssSpecificityExample = () => (
<div className="container">
<style>{styles}</style>
<Button id="my-button">Click me</Button>
</div>
);
Trace the button example by property: `.container .primary-button` supplies the green background, while `#my-button` supplies the yellow text. This is more precise than saying that one rule wins overall. section 2.2
For maintainable CSS, prefer simple class selectors over deep nesting and IDs when possible. This also appears among the suggested CSS performance optimizations. section 2.2








