HTML & CSS interview questions
Show answer
Every element is a box: content, padding, border and margin. With the default
box-sizing: content-box,widthapplies only to content, so padding and border make the box wider.box-sizing: border-boxincludes padding and border inwidth, which makes layouts predictable — most projects set it globally.Show answer
Flexbox lays items out in one direction (a row or a column) and is ideal for toolbars, nav bars and centring. Grid controls rows and columns together and suits page layouts, card galleries and anything aligned on two axes. They combine well: a Grid page layout with Flexbox inside components.
Show answer
A
<button>is focusable, works with Enter and Space, is announced as a button by screen readers and can submit forms. A<div>needstabindex, a role, keyboard handlers and focus styles to come close — and is usually still incomplete. Use links for navigation and buttons for actions.Show answer
When rules conflict, the more specific selector wins: inline styles, then IDs, then classes/attributes/pseudo-classes, then elements. Equal specificity falls back to source order.
!importantoverrides normal rules and makes code hard to maintain. Cascade layers (@layer) let you control precedence between groups of styles explicitly.