How do you choose relative and absolute CSS units for responsive design?

Instruction: Compare CSS unit reference frames and show when relative and absolute units are useful, including zoom and narrow-layout considerations.

Context: This question assesses the candidate's understanding of CSS units and the significance of choosing relative units (e.g., em, rem, vw, vh) to create adaptable, responsive layouts.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

Choose a unit according to what a dimension should follow. Relative units are useful, but a unit name alone does not make a layout responsive or accessible. CSS px is a reference unit, not necessarily one hardware pixel, and browser zoom can scale pixel-sized text too.

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS example</title> <style> html { font-size: 100%; } body { margin: 0; } .panel { box-sizing: border-box; width: 100%; max-width: 40rem; padding: 1rem; border: 1px solid; font-size: 1.25rem; } .label { font-size: 1em; padding: .5em; } h1 { font-size: clamp(1.5rem, 1rem + 2vw, 2.5rem); }...

Related Questions