images
container design
image layout
HTML CSS
web development

Multiple images inside one container

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Putting multiple images inside one container is a normal web-layout task for galleries, comparison cards, product grids, and collages. The key is not just placing the images in one wrapper element, but controlling sizing, spacing, cropping, and responsiveness so the layout behaves well across different screens.

Basic HTML Structure

A simple container can hold as many images as you need.

html
1<div class="image-grid">
2  <img src="photo-1.jpg" alt="Mountain at sunrise">
3  <img src="photo-2.jpg" alt="Forest trail">
4  <img src="photo-3.jpg" alt="River stones">
5  <img src="photo-4.jpg" alt="Snowy peak">
6</div>

The real layout behavior comes from CSS.

Use CSS Grid For Structured Layouts

Grid is usually the easiest choice when you want neat rows and columns.

css
1.image-grid {
2  display: grid;
3  grid-template-columns: repeat(2, 1fr);
4  gap: 12px;
5  max-width: 640px;
6}
7
8.image-grid img {
9  width: 100%;
10  aspect-ratio: 4 / 3;
11  object-fit: cover;
12  display: block;
13  border-radius: 8px;
14}

This creates a two-column layout where every image fills its cell and is cropped cleanly with object-fit: cover.

Use Flexbox For Simpler Rows

If you only need a row or wrapping strip of images, Flexbox can be enough.

css
1.image-strip {
2  display: flex;
3  flex-wrap: wrap;
4  gap: 10px;
5}
6
7.image-strip img {
8  width: 180px;
9  height: 120px;
10  object-fit: cover;
11}

Flexbox is good for simpler one-dimensional arrangements. Grid is usually better when the layout has both rows and columns that need stronger control.

Responsive Behavior

A fixed desktop layout often fails on smaller screens. Responsive grid definitions solve that cleanly.

css
1.image-grid {
2  display: grid;
3  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
4  gap: 12px;
5}

With this pattern, the number of columns adjusts automatically based on available width. That keeps the images readable without separate mobile markup.

Handling Different Image Shapes

Real-world image sets rarely share the same dimensions. If you do nothing, mismatched aspect ratios can make the container look uneven.

A practical approach is to let the container define the display ratio and use object-fit:

  • 'cover fills the box and crops excess'
  • 'contain preserves the whole image but may leave empty space'

For gallery-style layouts, cover is usually the better choice because it creates a tighter visual grid.

Add Captions Or Overlay Content

A container can also hold image cards instead of bare img tags. That is useful when each image needs a label or action.

html
1<div class="card-grid">
2  <figure>
3    <img src="photo-1.jpg" alt="Mountain at sunrise">
4    <figcaption>Sunrise trail</figcaption>
5  </figure>
6</div>

This keeps the content semantic and makes the container easier to style as a reusable component.

Accessibility And Performance

Each image should have meaningful alt text unless it is purely decorative. That makes the container accessible to screen readers and improves overall document quality.

For performance, consider lazy loading:

html
<img src="photo-1.jpg" alt="Mountain at sunrise" loading="lazy">

That reduces initial page cost when a container holds many images below the fold.

Common Pitfalls

A common mistake is setting only the container layout and forgetting to control image sizing. Without width, height, or object-fit rules, the images can stretch or overflow unpredictably.

Another mistake is using fixed widths that work only on desktop. Responsive CSS avoids layouts that collapse on phones.

It is also easy to neglect accessibility. A multi-image container is still a set of real content images, so alt text matters unless the images are decorative only.

Summary

  • Put multiple images in one wrapper element and control the layout with CSS.
  • Use CSS Grid for structured galleries and Flexbox for simpler strips.
  • Add object-fit and aspect-ratio rules so different image sizes behave consistently.
  • Make the layout responsive with auto-fit and minmax when possible.
  • Include alt text and consider lazy loading for accessibility and performance.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.