An adaptive gallery
Make a gallery of 8 images that has 4 columns on a wide screen and reduces the number of columns by itself on narrower ones (the smallest image width is 160px). The images have the same height (150px) and are not stretched.
A menu in one row, cards side by side, header–content–footer: today two tools are used to lay out elements. Flexbox arranges elements in one direction (a row or a column), and grid in a mesh of rows and columns.
The parent gets display: flex, and its children become "flex items" arranged in a row:
<style>
.menu {
display: flex;
gap: 10px; /* the space between the items */
justify-content: space-between; /* placement along the main axis (horizontal) */
align-items: center; /* alignment along the cross axis (vertical) */
background: #1f5f91;
padding: 10px;
}
.menu a { color: white; text-decoration: none; padding: 6px 10px; }
.logo { font-weight: bold; font-size: 1.4rem; color: #ffd166 !important; }
</style>
<nav class="menu">
<a class="logo" href="#">LOGO</a>
<div>
<a href="#">Home</a>
<a href="#">Projects</a>
<a href="#">Contact</a>
</div>
</nav>
Result:
| Property (parent) | Values |
|---|---|
flex-direction | row (a row, the default), column |
justify-content | flex-start, center, flex-end, space-between, space-around – along the main axis |
align-items | stretch, center, flex-start, flex-end – along the cross axis |
flex-wrap: wrap | items wrap onto a new row when there is no room |
gap | the space between the items |
| Property (child) | Meaning |
|---|---|
flex: 1 | the item takes the remaining space; two items with flex: 1 and flex: 2 share the space 1 : 2 |
flex: 0 0 200px | a fixed width of 200px – it neither grows nor shrinks |
display: flex; justify-content: center; align-items: center; on a parent that has a height.<style>
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card {
flex: 1 1 140px; /* grows, shrinks, starting width 140px */
background: #eef6fc;
border-radius: 10px;
padding: 12px;
}
</style>
<div class="cards">
<div class="card">HTML</div>
<div class="card">CSS</div>
<div class="card">JavaScript</div>
<div class="card">Python</div>
<div class="card">C++</div>
</div>
Result:
Make the browser window narrower: the cards move onto a new row by themselves when they do not fit. This way, without a single extra rule, you get a layout that also works on a phone.
<style>
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
gap: 8px;
}
.gallery div { background: #2b7bb9; color: white; padding: 20px; text-align: center; border-radius: 6px; }
.wide { grid-column: span 2; background: #1f5f91 !important; } /* takes 2 columns */
</style>
<div class="gallery">
<div>1</div><div>2</div><div>3</div>
<div class="wide">4 (two columns)</div><div>5</div>
<div>6</div><div>7</div><div>8</div>
</div>
Result:
| Property | Example |
|---|---|
grid-template-columns | 200px 1fr – the first column 200px, the second the rest; repeat(4, 1fr) – 4 equal ones |
fr | a share of the free space (fraction) |
repeat(auto-fill, minmax(150px, 1fr)) | as many columns of at least 150px as fit – an automatically adapting gallery |
grid-column: span 2 | the item takes two columns |
grid-template-areas | a layout given by area names (example below) |
<style>
.page {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"menu content"
"footer footer";
gap: 6px;
min-height: 220px;
}
header { grid-area: header; background: #1f5f91; color: white; }
nav { grid-area: menu; background: #cfe3f3; }
main { grid-area: content; background: #eef6fc; }
footer { grid-area: footer; background: #ddd; }
header, nav, main, footer { padding: 10px; }
</style>
<div class="page">
<header>Header</header>
<nav>Menu</nav>
<main>Main content</main>
<footer>Footer</footer>
</div>
Result:
The areas are "drawn" as text in grid-template-areas, and each element gets its place through grid-area. Changing the layout (e.g. for a phone) comes down to a different drawing.
| Situation | Tool |
|---|---|
| a menu, a row of buttons, centring one element | flexbox |
| an unknown number of cards that wrap onto a new row | flexbox (wrap) or grid (auto-fill) |
| a gallery with equal columns | grid |
| the layout of a whole page (header, menu, content, footer) | grid |
They are often combined: grid for the whole page, flexbox inside the header. For practice there are the games Flexbox Froggy and Grid Garden.
Each task has an example, a hint and a solution. The solutions have been checked in the browser.
Make a gallery of 8 images that has 4 columns on a wide screen and reduces the number of columns by itself on narrower ones (the smallest image width is 160px). The images have the same height (150px) and are not stretched.
Make a site footer with three columns (About us, Links, Contact) of equal width, with space between them, and at the bottom one row with the text "© 2026", centred.
display: flex or grid is written on the parent, not on the elements you are arranging.justify-content works along the main axis – with flex-direction: column that is the vertical.gap instead of a margin on each element.1. Which property makes flexbox items wrap onto a new row?
2. How do you centre an element horizontally and vertically in a flex parent?
3. What does grid-template-columns: 1fr 2fr mean?
4. What is better for the layout of a whole page with a header, a menu and content?