Adaptive layout project
Definition and purpose
Combine fluid sizing, flexible tracks and a content-driven breakpoint to build a reusable gallery. A responsive component can often work with fewer media rules when its layout already adapts to space.
Automatic gallery
<!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: 340px; display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); } #a,#b { height: 30px; } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>Two minimum tracks cannot fit, so the cards stack.
Syntax and mental model
Use repeat(auto-fit, minmax(min(100%, 180px), 1fr)) for tracks that can shrink in very narrow containers. Use a gap for consistent spacing, a max-width for readability and media queries only for changes not handled by flexible layout.
Same grid with more room
<!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%; display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); } #a,#b { height: 30px; } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>Both viewports fit two cards; unused tracks collapse with auto-fit.
Common mistakes
Do not force the minimum track size above the available space. Test one item, multiple items, and narrow containers rather than assuming desktop columns always fit.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Container styles and scroll-state queries. Next lesson: Writing modes and direction.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Adaptive layout project — knowledge check
Five questions on adaptive layout project.
Which feature collapses empty automatic tracks?
Why min(100%, 220px) in a track minimum?
Which property separates tracks?
When is an extra breakpoint useful?
What should be tested?