Fluid and mobile-first layouts
Definition and purpose
Responsive design adjusts layout to available space. Start with a readable narrow layout, then add a breakpoint when the content benefits from more room, rather than targeting a particular phone model.
Fluid panel
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { width: 100%; max-width: 600px; } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>The panel fills narrow space and stops growing at 600px.
Syntax and mental model
Percentages and max-width make a box fluid within limits. A mobile-first media rule such as @media (min-width: 600px) adds changes at and above the threshold. The viewport meta tag in these HTML files lets mobile browsers use a device-width layout viewport.
Add columns when space permits
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { display: grid; grid-template-columns: 1fr; } @media (min-width: 600px) { #p { grid-template-columns: repeat(2, 1fr); } } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>At 375px the items stack; at 1280px they share the row.
Common mistakes
Do not use a fixed desktop width for every screen. Check the content at intermediate widths and with enlarged text, not only at two presets.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Nested flex and baseline alignment, Grid versus flex and composition. Next lesson: Media query syntax and range comparisons.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Fluid and mobile-first layouts — knowledge check
Five questions on fluid and mobile-first layouts.
What should drive a breakpoint?
When does min-width: 600px match?
What caps a fluid box?
What should the narrow default provide?
Why test intermediate widths?