Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by Mahmuda Akter Isha
Showcase Designs Using Before After Slider.
Displaying multiple images in HTML and CSS may look simple at first, but once you start building a real layout, a few common problems appear quickly.
Your images may stack vertically instead of appearing side by side. Some images may look too large, while others appear too small. The layout may look fine on desktop but break on mobile. You may also struggle to make all images the same size, add equal spacing, create a responsive gallery, or build a clean portfolio-style image section.
The good news is that HTML and CSS give you several easy ways to display multiple images. You can use a basic image row, Flexbox, CSS Grid, responsive media queries, captions, hover effects, and image optimization techniques to create a clean and professional image layout.
In this guide, you will learn how to display multiple images in HTML and CSS using beginner-friendly examples. We will cover side-by-side images, responsive image galleries, CSS Grid layouts, Flexbox galleries, same-size image cards, clickable images, captions, hover effects, and common troubleshooting tips.
By the end, you will have complete copy-paste HTML and CSS code that you can use for a website gallery, blog image section, portfolio, product showcase, or project preview layout.
Images are an important part of modern websites. Whether you are building a portfolio, blog, landing page, product page, photography gallery, comparison showcase, or service website, image layout affects both user experience and visual quality.
A good multiple-image layout helps you:
For example, if you are creating a portfolio website, you may want to display several project images in a grid. If you are writing a tutorial blog, you may want to place screenshots side by side. If you are creating a product comparison page, you may need a clean layout where images are the same size.
Instead of adding images randomly, it is better to use a proper HTML structure and CSS layout method.
The simplest way to display multiple images is to place several <img> tags inside a parent container.
<img>
Here is a basic HTML example:
<div class="image-container"> <img src="image1.jpg" alt="First sample image"> <img src="image2.jpg" alt="Second sample image"> <img src="image3.jpg" alt="Third sample image"> </div>
Each image is added with the <img> tag. The src attribute defines the image file path, and the alt attribute describes the image.
src
The alt text is important because it helps search engines and screen readers understand the image. Instead of writing generic alt text like “image” or “photo,” use clear descriptions such as “Responsive image gallery layout example” or “Website portfolio project preview.”
alt
However, if you only write the HTML above, your images may not automatically appear in the layout you want. That is where CSS comes in.
One of the most common search queries is: how to display multiple images side by side in HTML CSS.
The easiest modern way is to use Flexbox.
<div class="image-row"> <img src="image1.jpg" alt="Portfolio image one"> <img src="image2.jpg" alt="Portfolio image two"> <img src="image3.jpg" alt="Portfolio image three"> </div>
.image-row { display: flex; gap: 16px; } .image-row img { width: 33.33%; height: auto; border-radius: 8px; }
In this example, display: flex; places the images in a row. The gap property adds space between the images. The width: 33.33%; makes each image take one-third of the row.
display: flex;
gap
width: 33.33%;
This is a simple way to put images next to each other in HTML and CSS.
However, this layout may not look perfect on small screens because the images can become too narrow. To fix that, you need a responsive layout.
If you want images to appear in one row on desktop but stack nicely on mobile, use Flexbox with flex-wrap.
flex-wrap
<div class="responsive-row"> <img src="image1.jpg" alt="Responsive gallery image one"> <img src="image2.jpg" alt="Responsive gallery image two"> <img src="image3.jpg" alt="Responsive gallery image three"> </div>
.responsive-row { display: flex; flex-wrap: wrap; gap: 16px; } .responsive-row img { flex: 1 1 250px; max-width: 100%; height: auto; border-radius: 8px; }
Here, flex-wrap: wrap; allows images to move to the next line when there is not enough space. The flex: 1 1 250px; means each image should try to stay around 250px wide, but it can grow or shrink depending on available space.
flex-wrap: wrap;
flex: 1 1 250px;
This is a better option than giving every image a fixed width because it works more smoothly across desktop, tablet, and mobile devices.
Flexbox is one of the easiest ways to display multiple images in a row or wrapped layout. It is best when you want a simple horizontal image section, a row of cards, or a small responsive image gallery.
Use Flexbox when:
Here is a complete Flexbox image gallery example.
<div class="flex-gallery"> <img src="gallery1.jpg" alt="Gallery image one"> <img src="gallery2.jpg" alt="Gallery image two"> <img src="gallery3.jpg" alt="Gallery image three"> <img src="gallery4.jpg" alt="Gallery image four"> <img src="gallery5.jpg" alt="Gallery image five"> <img src="gallery6.jpg" alt="Gallery image six"> </div>
.flex-gallery { display: flex; flex-wrap: wrap; gap: 20px; } .flex-gallery img { flex: 1 1 280px; height: 220px; object-fit: cover; border-radius: 10px; }
This creates a flexible gallery where images automatically wrap based on available screen width. The object-fit: cover; property keeps the images visually consistent by cropping them neatly inside the same height.
object-fit: cover;
This is very useful when your images have different original dimensions.
CSS Grid is one of the best methods for creating a clean image gallery. If you want multiple columns, equal spacing, and a professional gallery layout, CSS Grid is usually the best choice.
Use CSS Grid when:
<div class="grid-gallery"> <img src="image1.jpg" alt="CSS Grid gallery image one"> <img src="image2.jpg" alt="CSS Grid gallery image two"> <img src="image3.jpg" alt="CSS Grid gallery image three"> <img src="image4.jpg" alt="CSS Grid gallery image four"> <img src="image5.jpg" alt="CSS Grid gallery image five"> <img src="image6.jpg" alt="CSS Grid gallery image six"> </div>
.grid-gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; } .grid-gallery img { width: 100%; height: 240px; object-fit: cover; border-radius: 10px; }
In this example, grid-template-columns: repeat(3, 1fr); creates three equal columns. The gap property adds spacing between images. Each image takes the full width of its grid column.
grid-template-columns: repeat(3, 1fr);
This method is perfect when you want to display 3 images in a row using HTML and CSS.
A fixed three-column grid may look good on desktop, but it can become too crowded on mobile. To solve this, you can use a responsive CSS Grid layout.
.responsive-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; } .responsive-grid img { width: 100%; height: 230px; object-fit: cover; border-radius: 10px; }
<div class="responsive-grid"> <img src="image1.jpg" alt="Responsive image grid example one"> <img src="image2.jpg" alt="Responsive image grid example two"> <img src="image3.jpg" alt="Responsive image grid example three"> <img src="image4.jpg" alt="Responsive image grid example four"> </div>
This is one of the best CSS solutions for a responsive image gallery.
The auto-fit value allows the grid to automatically fit as many columns as possible. The minmax(220px, 1fr) value means each column should be at least 220px wide but can grow to fill available space.
auto-fit
minmax(220px, 1fr)
This is a clean way to create a responsive image grid without writing multiple media queries.
Sometimes you may want to display exactly 4 images in one row on desktop. CSS Grid makes this easy.
<div class="four-column-gallery"> <img src="image1.jpg" alt="Four column gallery image one"> <img src="image2.jpg" alt="Four column gallery image two"> <img src="image3.jpg" alt="Four column gallery image three"> <img src="image4.jpg" alt="Four column gallery image four"> </div>
.four-column-gallery { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; } .four-column-gallery img { width: 100%; height: 200px; object-fit: cover; border-radius: 8px; }
To make it responsive, add media queries:
@media (max-width: 992px) { .four-column-gallery { grid-template-columns: repeat(2, 1fr); } } @media (max-width: 600px) { .four-column-gallery { grid-template-columns: 1fr; } }
Now the layout shows 4 columns on desktop, 2 columns on tablet, and 1 column on mobile.
This is a practical layout for product images, service previews, blog thumbnails, or portfolio projects.
There are different ways to display multiple images in HTML and CSS. The right method depends on the layout you want.
For most modern websites, use CSS Grid for full galleries and Flexbox for simple rows.
Float was commonly used in older layouts, but it is not the best choice for modern responsive image galleries.
A common problem when displaying multiple images is that every image has a different size. One image may be landscape, another may be portrait, and another may be square. This can make your gallery look uneven.
To make all images the same size, set a fixed height and use object-fit: cover.
object-fit: cover
.same-size-gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; } .same-size-gallery img { width: 100%; height: 240px; object-fit: cover; border-radius: 10px; }
The width: 100%; makes the image fill the column width. The height: 240px; gives every image the same height. The object-fit: cover; makes sure the image fills the box without stretching.
width: 100%;
height: 240px;
This is one of the best ways to create a clean image gallery with equal image sizes.
If you use only width: 100%; and height: auto;, the images will keep their original aspect ratios. That is good in some cases, but it may create uneven rows if your images have different dimensions.
height: auto;
The easiest way to add space between multiple images is to use the CSS gap property on the parent container.
For Flexbox:
.image-row { display: flex; gap: 20px; }
For CSS Grid:
.image-grid { display: grid; gap: 20px; }
Using gap is cleaner than adding margins to every image because it controls spacing from the parent layout.
You can use different gap values depending on your design:
gap: 10px; gap: 16px; gap: 24px; gap: 32px;
For most websites, a gap between 16px and 24px works well.
16px
24px
If you are creating a portfolio, tutorial, product showcase, or blog image gallery, captions can help visitors understand each image.
Use the <figure> and <figcaption> elements for image captions.
<figure>
<figcaption>
<div class="caption-gallery"> <figure> <img src="project1.jpg" alt="Homepage redesign preview"> <figcaption>Homepage redesign preview</figcaption> </figure> <figure> <img src="project2.jpg" alt="Mobile app dashboard design"> <figcaption>Mobile app dashboard design</figcaption> </figure> <figure> <img src="project3.jpg" alt="Landing page layout example"> <figcaption>Landing page layout example</figcaption> </figure> </div>
.caption-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; } .caption-gallery figure { margin: 0; background: #f8f8f8; border-radius: 10px; overflow: hidden; } .caption-gallery img { width: 100%; height: 220px; object-fit: cover; display: block; } .caption-gallery figcaption { padding: 12px; font-size: 15px; text-align: center; }
This creates a clean image gallery with captions under each image.
Captions are especially useful for portfolio pages, case studies, tutorials, photography galleries, and product showcases.
Hover effects can make your image gallery feel more interactive. For example, you can slightly zoom an image when the user hovers over it.
<div class="hover-gallery"> <img src="image1.jpg" alt="Hover gallery image one"> <img src="image2.jpg" alt="Hover gallery image two"> <img src="image3.jpg" alt="Hover gallery image three"> </div>
.hover-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; } .hover-gallery img { width: 100%; height: 230px; object-fit: cover; border-radius: 10px; transition: transform 0.3s ease; } .hover-gallery img:hover { transform: scale(1.05); }
This adds a smooth zoom effect when someone hovers over the images.
To make the effect cleaner, you can wrap each image in a container with overflow: hidden.
overflow: hidden
<div class="hover-card"> <img src="image1.jpg" alt="Image hover effect example"> </div>
.hover-card { overflow: hidden; border-radius: 10px; } .hover-card img { width: 100%; height: 230px; object-fit: cover; transition: transform 0.3s ease; } .hover-card img:hover { transform: scale(1.08); }
This prevents the zoomed image from overflowing outside the card.
Sometimes you may want each image to link to another page, a larger image, a product page, or a portfolio case study.
To make an image clickable, wrap it inside an anchor tag.
<div class="clickable-gallery"> <a href="project-one.html"> <img src="project1.jpg" alt="Project one preview"> </a> <a href="project-two.html"> <img src="project2.jpg" alt="Project two preview"> </a> <a href="project-three.html"> <img src="project3.jpg" alt="Project three preview"> </a> </div>
.clickable-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; } .clickable-gallery a { display: block; overflow: hidden; border-radius: 10px; } .clickable-gallery img { width: 100%; height: 230px; object-fit: cover; display: block; transition: transform 0.3s ease; } .clickable-gallery a:hover img { transform: scale(1.05); }
This is useful for portfolio galleries, product grids, blog cards, image previews, and service sections.
A masonry gallery is a layout where images appear in columns with different heights, similar to Pinterest-style layouts.
You can create a simple masonry-style gallery using CSS columns.
<div class="masonry-gallery"> <img src="image1.jpg" alt="Masonry gallery image one"> <img src="image2.jpg" alt="Masonry gallery image two"> <img src="image3.jpg" alt="Masonry gallery image three"> <img src="image4.jpg" alt="Masonry gallery image four"> <img src="image5.jpg" alt="Masonry gallery image five"> </div>
.masonry-gallery { column-count: 3; column-gap: 16px; } .masonry-gallery img { width: 100%; margin-bottom: 16px; border-radius: 10px; display: block; } @media (max-width: 900px) { .masonry-gallery { column-count: 2; } } @media (max-width: 600px) { .masonry-gallery { column-count: 1; } }
This layout works well for photography galleries, inspiration boards, design showcases, and creative portfolios.
However, if you need strict row alignment, CSS Grid is better. Masonry layouts are best when image heights can vary naturally.
Media queries allow you to change the layout based on screen size. This is helpful when you want full control over how many images appear per row on desktop, tablet, and mobile.
<div class="media-gallery"> <img src="image1.jpg" alt="Responsive media query gallery image one"> <img src="image2.jpg" alt="Responsive media query gallery image two"> <img src="image3.jpg" alt="Responsive media query gallery image three"> <img src="image4.jpg" alt="Responsive media query gallery image four"> </div>
.media-gallery { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; } .media-gallery img { width: 100%; height: 220px; object-fit: cover; border-radius: 10px; } @media (max-width: 1024px) { .media-gallery { grid-template-columns: repeat(3, 1fr); } } @media (max-width: 768px) { .media-gallery { grid-template-columns: repeat(2, 1fr); } } @media (max-width: 480px) { .media-gallery { grid-template-columns: 1fr; } }
This creates a layout with:
This is a great approach when you want predictable control over your responsive image gallery.
Here is a complete copy-paste example that includes a responsive CSS Grid gallery, equal-size images, captions, hover effects, lazy loading, and SEO-friendly alt text.
<section class="gallery-section"> <h2>Responsive Image Gallery</h2> <div class="complete-gallery"> <figure> <a href="image1.jpg"> <img src="image1.jpg" alt="Responsive gallery layout example one" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image One</figcaption> </figure> <figure> <a href="image2.jpg"> <img src="image2.jpg" alt="Responsive gallery layout example two" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image Two</figcaption> </figure> <figure> <a href="image3.jpg"> <img src="image3.jpg" alt="Responsive gallery layout example three" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image Three</figcaption> </figure> <figure> <a href="image4.jpg"> <img src="image4.jpg" alt="Responsive gallery layout example four" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image Four</figcaption> </figure> <figure> <a href="image5.jpg"> <img src="image5.jpg" alt="Responsive gallery layout example five" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image Five</figcaption> </figure> <figure> <a href="image6.jpg"> <img src="image6.jpg" alt="Responsive gallery layout example six" width="600" height="400" loading="lazy"> </a> <figcaption>Gallery Image Six</figcaption> </figure> </div> </section>
.gallery-section { max-width: 1200px; margin: 0 auto; padding: 60px 20px; } .gallery-section h2 { text-align: center; margin-bottom: 30px; font-size: 32px; } .complete-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; } .complete-gallery figure { margin: 0; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08); } .complete-gallery a { display: block; overflow: hidden; } .complete-gallery img { width: 100%; height: 230px; object-fit: cover; display: block; transition: transform 0.3s ease; } .complete-gallery a:hover img { transform: scale(1.06); } .complete-gallery figcaption { padding: 14px; text-align: center; font-size: 15px; color: #333333; } @media (max-width: 600px) { .gallery-section { padding: 40px 15px; } .gallery-section h2 { font-size: 26px; } .complete-gallery img { height: 210px; } }
This example is suitable for a portfolio gallery, blog image gallery, project showcase, product section, or website design gallery.
When you display multiple images on a page, design is not the only thing that matters. You should also optimize images for SEO, accessibility, and performance.
Here are some important best practices.
Instead of uploading an image named:
IMG_1234.jpg
Use a descriptive name like:
responsive-image-gallery-html-css.jpg
This gives search engines better context about the image.
Alt text should describe the image clearly. It should not be stuffed with keywords.
Bad example:
<img src="gallery.jpg" alt="image gallery image gallery html css gallery responsive gallery">
Good example:
<img src="gallery.jpg" alt="Responsive image gallery layout built with HTML and CSS">
Adding width and height helps the browser reserve space for the image before it loads.
<img src="gallery-image.jpg" alt="CSS Grid image gallery example" width="600" height="400">
This can help reduce layout shifts and improve user experience.
If your page has many images, use lazy loading for images that are not immediately visible.
<img src="gallery-image.jpg" alt="Lazy loaded image gallery example" loading="lazy">
Lazy loading can improve page speed because images load only when needed.
Large image files can slow down your website. Before uploading images, compress them using tools or export them in optimized formats such as WebP when possible.
For advanced responsive image optimization, you can use srcset.
srcset
<img src="gallery-800.jpg" srcset="gallery-400.jpg 400w, gallery-800.jpg 800w, gallery-1200.jpg 1200w" sizes="(max-width: 600px) 100vw, 50vw" alt="Responsive image example using srcset">
This allows the browser to choose the best image size based on the user’s device.
When beginners try to display multiple images, they often face layout issues. Here are the most common problems and how to fix them.
By default, images may appear inline, but depending on your HTML structure or CSS, they can stack vertically.
Use Flexbox or Grid to control the layout:
.image-container { display: flex; gap: 16px; }
Or:
.image-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
Use a fixed height and object-fit: cover.
.image-container img { width: 100%; height: 240px; object-fit: cover; }
Make sure images are not larger than their parent container.
.image-container img { max-width: 100%; height: auto; }
Use the gap property on the parent container.
.image-container { display: grid; gap: 20px; }
Use auto-fit and minmax() with CSS Grid.
minmax()
.image-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; }
Or use media queries:
@media (max-width: 600px) { .image-gallery { grid-template-columns: 1fr; } }
If your images look stretched, avoid forcing both width and height without object-fit.
object-fit
Use this:
.image-gallery img { width: 100%; height: 240px; object-fit: cover; }
This keeps the image looking natural while fitting it into the same-size box.
If you are building a custom HTML website, a simple landing page, or a static portfolio, HTML and CSS are enough to display multiple images.
However, if you are using WordPress and want more advanced image features, a plugin may save time. For example, if you want to show before-and-after results, comparison images, interactive sliders, or visual transformation showcases, you may need more than a basic image grid.
A normal HTML and CSS gallery is useful for displaying multiple images, but an interactive before-after image slider is better when you want users to compare two images directly.
For WordPress users, CodeCanel’s WP Before After Image Slider can be useful for creating interactive before-and-after image comparisons without writing custom code. This is especially helpful for designers, photographers, agencies, renovation services, beauty clinics, editors, and anyone who needs to show visual changes clearly.
You can use a basic HTML/CSS gallery when you simply want to display images. But when you want visitors to interact with visual comparisons, a slider or gallery plugin may be a better choice.
To create a clean and responsive image layout, follow these best practices:
A good image layout should be clean, responsive, fast-loading, and easy to understand.
Displaying multiple images in HTML and CSS is easy once you choose the right layout method. For simple side-by-side images, Flexbox is a great option. For full image galleries, CSS Grid is usually the best choice. If you want equal image sizes, use object-fit: cover. If you want the layout to work on mobile, use responsive Grid, Flexbox wrapping, or media queries.
The best approach depends on your goal. A simple image row may only need Flexbox. A portfolio or product gallery may need CSS Grid. A photography layout may work better with a masonry-style design. And if you are using WordPress for interactive visual comparisons, a dedicated image slider plugin can help you create a more engaging experience without custom coding.
By using clean HTML, modern CSS, responsive design, optimized images, and proper alt text, you can create a professional multiple-image layout that looks good on every device and performs better for users and search engines.
This page was last edited on 30 June 2026, at 6:18 pm
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy