What are media queries and how do you write one?
A media query applies CSS only when a condition about the device or viewport is true, most often its width.
HTML<style> .card { padding: 12px; background: #fde68a; } .card::after { content: " (narrow: under 500px)"; } @media (min-width: 500px) { .card { background: #bbf7d0; } .card::after { content: " (wide: 500px or more)"; } } </style> <div class="card">The preview's width picks the color</div>Change
500pxto2000pxto see the other branch. You can also test preferences (prefers-color-scheme), input (pointer: coarse),print, and ranges like@media (width >= 600px). More in media queries.Why does a responsive page need the viewport meta tag?
Without it, mobile browsers lay the page out about 980px wide and zoom out, so your small-screen media queries never match and the text is tiny.
<meta name="viewport" content="width=device-width, initial-scale=1">Do not add
maximum-scale=1oruser-scalable=no: blocking zoom is an accessibility failure.What is mobile-first CSS?
Base styles target the smallest screen, and
min-widthmedia queries enhance the layout as the screen grows. Desktop-first is the reverse, withmax-widthqueries that undo things..list { display: grid; gap: 12px; } /* phones: one column */ @media (min-width: 640px) { .list { grid-template-columns: 1fr 1fr; } /* tablets */ } @media (min-width: 1024px) { .list { grid-template-columns: repeat(4, 1fr); } }Phones then skip the wide-screen rules instead of applying and undoing them. Put breakpoints where your content breaks, not at device widths.
What is the difference between em and rem?
remis relative to the root font size;emto the element's own font size (forfont-size, its parent's), soemcompounds when nested.HTML<style> .em { font-size: 1.2em; } .rem { font-size: 1.2rem; } </style> <div class="em">1.2em <div class="em">1.2em <div class="em">1.2em</div></div></div> <div class="rem">1.2rem <div class="rem">1.2rem <div class="rem">1.2rem</div></div></div>With a 16px root the
emlevels are 19.2px, 23.04px and 27.648px; everyremlevel is 19.2px. Useremfor type and spacing,emwhere sizes should follow the element's own text.How do you make font sizes fluid with clamp()?
clamp(min, preferred, max)returns the preferred value kept between the limits. With a viewport unit in the middle, the size grows with the screen with no breakpoints.HTML<style> h1 { font-size: clamp(1.5rem, 1rem + 4vw, 3rem); margin: 0 0 8px; } .box { width: clamp(200px, 50%, 400px); background: #dbeafe; padding: 8px; } </style> <h1>Fluid heading</h1> <div class="box">Between 200px and 400px, otherwise half the width</div>Keep a
rempart (1rem + 4vw, not4vw): pure viewport units ignore browser zoom and the user's font size.How do you make images responsive?
In CSS,
max-width: 100%; height: auto;stops overflow and keeps the ratio, andobject-fit: covercrops into a fixed box. In HTML,srcsetandsizeslet the browser pick the file:<img src="photo-800.jpg" srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w" sizes="(min-width: 800px) 50vw, 100vw" width="800" height="600" alt="Team at the office">Keep
widthandheight: the browser reserves the space before loading, which prevents layout shift (CLS).What are container queries and how are they different from media queries?
A media query asks about the viewport; a container query asks about an ancestor marked as a container, so one card can stack in a sidebar and sit side by side in the main column.
HTML<style> .slot { container-type: inline-size; border: 1px dashed #9ca3af; padding: 6px; margin-bottom: 8px; } .narrow { width: 240px; } .wide { width: 480px; } .card { display: grid; gap: 8px; } .thumb { height: 60px; background: #2563eb; } @container (min-width: 400px) { .card { grid-template-columns: 100px 1fr; } } </style> <div class="slot narrow"><div class="card"><div class="thumb"></div><p>Narrow container: stacked</p></div></div> <div class="slot wide"><div class="card"><div class="thumb"></div><p>Wide container: side by side</p></div></div>container-type: inline-sizeis required so the container's width cannot depend on its children.Why does height: 100vh cause problems on mobile?
On mobile,
100vhassumes the address bar is hidden, so while it shows, the bottom of a100vhsection is cut off. Usesvh(browser UI shown),lvh(UI hidden) ordvh(follows the UI)..hero { min-height: 100vh; /* fallback for old browsers */ min-height: 100svh; /* never hidden under the address bar */ }Prefer
svh:dvhresizes while the user scrolls. Usemin-heightso content can grow.What are breakpoints, and how do you choose them?
A breakpoint is a viewport width at which a media query changes the layout. Set one where the content starts to look wrong (lines too long, cards too cramped) rather than per phone model; two or three are usually enough. Writing them in
em, as in@media (min-width: 48em), has a bonus: in a media queryemmeans the browser's default font size, so the layout also adapts when a user enlarges their text.Why does width: 100vw cause a horizontal scrollbar?
100vwis the full viewport width including the vertical scrollbar, while the page's content area excludes it. On desktop browsers with classic scrollbars, an element at100vwis wider than the page by the scrollbar's width and adds a horizontal scrollbar. Phones and macOS hide their scrollbars over the content, so the bug often shows up late. Usewidth: 100%instead;100vwis only safe when the page cannot scroll vertically.
Responsive Design CSS Interview Questions
Media queries, mobile-first, relative units, fluid sizes and container queries.
10 questions