Intermediate Interview Questions
How is specificity calculated?
As a score (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements), compared left to right. Inline styles beat selectors, and !important beats normal declarations. With equal specificity, the last rule wins.
#nav .item a → (1, 1, 1)
.menu li.active → (0, 2, 1) /* loses to the one above */Explain the position values.
| Value | Behaviour |
|---|---|
| static | default, normal flow |
| relative | offset from its normal position, space kept |
| absolute | removed from flow, positioned to nearest positioned ancestor |
| fixed | positioned to the viewport |
| sticky | relative until a scroll threshold, then fixed within its container |
Flexbox vs Grid?
Flexbox is one-dimensional (a row or a column) and content-driven: great for navbars, toolbars and centering. Grid is two-dimensional (rows and columns) and layout-driven: great for page layouts and galleries. They are often combined.
How do you center a div horizontally and vertically?
.parent { display: flex; justify-content: center; align-items: center; }
/* or */
.parent { display: grid; place-items: center; }
/* or, for a positioned child */
.child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }em vs rem vs px?
px is fixed. em is relative to the element's own font size (it compounds when nested). rem is relative to the root font size, so it is predictable and respects the user's browser font setting. Prefer rem for font sizes and spacing.
What is a media query? What is mobile-first?
A media query applies CSS when a condition is true, e.g. @media (min-width: 768px). Mobile-first means writing base styles for small screens and adding min-width queries for larger ones, so phones get the least CSS.