A CSS Image Gallery is a collection of images arranged in an organized
layout using CSS. It allows you to display multiple images in rows,
columns, or responsive grids. CSS can also add borders, spacing, hover
effects, shadows, and animations to make image galleries more attractive
and interactive.
Why Use a CSS Image Gallery?
Image galleries make it easy to display photos, products, portfolios,
travel pictures, and artwork. CSS helps create responsive galleries that
automatically adjust to different screen sizes while maintaining a clean
and professional appearance.
Basic Image Gallery Syntax
What this code does: Places three images in a flex container with a 10px gap so they sit in a row.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic gallery syntax demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles the gallery container and the images inside it. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
.demo-gallery is the container that holds all the pictures. display: flex turns it into a flex container, so its direct children, the images, are placed side by side in a row instead of stacking.
gap: 10px adds exactly 10px between neighboring items. It only adds space between images, not around the outside, so you do not need margins on each image.
By default flex items stay on one line and are squeezed. flex-wrap: wrap lets images move to a new line when there is not enough width, which keeps them at their intended size in a narrow window.
Each image is 150px wide, and the height follows its ratio automatically. A 2px blue border shows the edge of every picture. The rule is on the image class, so it applies to every photo in the gallery.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the gallery container with its images.
This div is the flex container. The images must be its direct children for the flex layout to work.
Each image has the demo-photo class and its own alt text. The ?1, ?2 and ?3 after the URLs request different sample photos. They appear in the same order as in the HTML, from left to right.
</div> closes the gallery.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 1: Create an Image Gallery
Place multiple images inside a container and use display:flex with the gap property to arrange them in a row with equal spacing.
What this code does: Lays out three 180px-wide images in a row with a 15px gap, a light border and rounded corners.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the flex gallery demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule for the container and one for the photos. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
display: flex makes the three images items of a row layout. They line up from left to right in HTML order.
gap: 15px leaves 15px between each pair of images. This gap is also important in the next step, where hovered images grow into this space.
flex-wrap: wrap moves an image to the next line instead of squeezing it when the window is narrower than the three images (180px ร 3 plus gaps).
Every photo is 180px wide. Using the same width for all images gives the gallery a tidy, regular look.
A thin light-gray border frames each picture without drawing too much attention, and border-radius: 6px softens the corners.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the gallery container and three images.
The flex container for the gallery. Its direct children become gallery items.
The sample photos are 360 by 240 pixels, exactly twice the 180px display width, so they stay sharp on high-density screens. All three images have the same ratio, which keeps the row even.
</div> closes the gallery.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Add Hover Effects
CSS transitions and transforms can make images enlarge smoothly when the user moves the mouse over them.
What this code does: Makes each image grow by 10% with a smooth transition and a blue border when you hover over it.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the gallery hover effect demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo adds a transition and a hover state to the photos. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The same flex gallery as the last step. The 15px gap matters here: a 10% larger image grows by about 9px on each side, which fits inside the gap, so the enlarged image does not cover its neighbors.
The normal state of each photo: 180px wide with a light gray border. The hover state changes this border color.
transition: .3s makes changes animate over 0.3 seconds, so the image grows smoothly instead of jumping. It is written in the normal state so that the animation also plays when the mouse leaves. border-radius: 8px rounds the corners.
:hover applies while the mouse is over an image. transform: scale(1.1) enlarges it to 110% without moving the other images, because transforms do not change the layout. border-color changes only the color of the existing border to blue.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the gallery container and three images.
The flex container for the gallery.
Each image uses demo-photo, so the hover effect works on all of them. Move the mouse over each one to try it.
</div> closes the gallery.
</body> closes the body.
</html> is the final closing tag and ends the document.
Responsive Image Gallery Example
Modern websites often use CSS Grid to create responsive image galleries that automatically adjust the number of columns based on the available screen width.
What this code does: Uses CSS Grid with auto-fit and minmax() so six images fill the available width and wrap into as many columns as fit.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the responsive grid gallery demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses a grid instead of flexbox. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
display: grid turns the gallery into a grid container. Unlike flexbox, which lays out one row at a time, a grid manages rows and columns together.
This one line makes the gallery responsive. minmax(180px, 1fr) means each column is at least 180px wide and can grow to take an equal share of the free space. repeat(auto-fit, ...) creates as many such columns as fit in the container. A wide window may show 5 columns, a phone only 1, without any media query. With auto-fit, unused empty columns collapse, so the images stretch to fill the row.
gap: 15px sets the space between rows and between columns.
width: 100% makes each image as wide as its grid cell. As the columns change size, the images scale with them, and the height follows the ratio.
Rounded 10px corners and a thin gray border. box-sizing: border-box is important here: images use content-box by default, so width: 100% plus a 2px border on each side would make the image 4px wider than its grid cell and push it into the gap. With border-box, the border is counted inside the 100%, so the image fits its cell exactly.
box-shadow values: no horizontal offset, 3px down, 8px blur, black at 20% opacity. It gives the images a slight lift from the page.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the gallery container with six images.
The grid container. Each direct child, an image here, is placed in its own grid cell.
Six images with the same ratio keep the grid tidy. They fill the cells row by row, from left to right. Resize the window to see how many columns fit.
</div> closes the gallery.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Image Gallery Example
What this code does: Builds a titled, responsive grid gallery of six photos with shadows that grow stronger as each image enlarges on hover.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the complete gallery demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. background: #f5f9ff gives the preview a light blue color, so the shadowed photos stand out.
The gallery is a grid, so the images are arranged in rows and columns.
Each column is at least 200px and shares the free space equally. auto-fit creates as many columns as fit, so the layout adapts to any width. gap: 20px separates the images, and it also leaves room for the hover enlargement.
width: 100% makes each image fill its cell, and border-radius: 10px rounds the corners.
A soft shadow lifts the photo. transition: .3s animates both the size and the shadow change on hover.
On hover the image grows by 5% with scale(1.05), and the shadow becomes larger and darker (8px down, 18px blur, 35% black), as if the photo lifted off the page. The growth of about 10px fits inside the 20px gap, so neighbors stay uncovered.
</style> closes the CSS.
</head> closes the head. The visible content comes next.
<body> starts the visible content: a heading and the gallery.
A heading above the gallery. It is outside the grid container, so it is not placed in a grid cell.
The grid container. Its image children fill the grid cells.
All images share the demo-photo class, so one set of rules styles them all.
</div> closes the gallery.
</body> closes the body.
</html> ends the document.
Advantages of CSS Image Gallery
Advantage
Benefit
Description
Organized Layout
Neat Display
Images are displayed in a clean and structured way.
Responsive Design
Mobile Friendly
The gallery adapts automatically to different screen sizes.
Interactive Effects
Better Experience
Hover animations make galleries more engaging.
Professional Appearance
Modern Design
Borders, shadows, and spacing improve visual appeal.
Easy Maintenance
Reusable CSS
The same gallery styles can be applied to many images.
Common Mistakes
Beginners often use images with different sizes, forget responsive
layouts, apply excessive hover effects, or omit spacing between images,
making the gallery appear unorganized.
What this code does: Compares a width written without a unit (200, ignored by the browser) with the correct 200px, so you can see the size difference.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
/* Incorrect: missing unit, so the browser ignores it */
.demo-wrong .demo-photo {
width: 200;
border: 3px solid #dc3545;
}
/* Correct: includes the px unit */
.demo-correct .demo-photo {
width: 200px;
border: 3px solid #28a745;
}
.demo-label {
font-weight: bold;
}
</style>
</head>
<body>
<p class="demo-label">Incorrect (width:200) - shows at full size</p>
<div class="demo-wrong">
<img class="demo-photo" src="https://picsum.photos/400/300?7" alt="Gallery image">
</div>
<p class="demo-label">Correct (width:200px)</p>
<div class="demo-correct">
<img class="demo-photo" src="https://picsum.photos/400/300?7" alt="Gallery image">
</div>
</body>
</html>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the common mistake demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one incorrect rule and one correct rule. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A CSS comment, ignored by the browser and written for the reader. It labels the next rule as the wrong version.
The selector .demo-wrong .demo-photo matches a demo-photo that is inside an element with the class demo-wrong. width: 200 has no unit, and in CSS lengths other than 0 need one. With a DOCTYPE the browser drops this declaration without any error, so the image keeps its natural 400px width.
A red border marks the incorrect example. The border is valid, so it still works. Only the width is ignored.
Another comment, labeling the correct version.
200px includes the unit, so the image is resized to 200px wide.
A green border marks the correct example, so the two images are easy to compare.
font-weight: bold makes the captions above the images stand out.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: two captions, each followed by a wrapper div and an image.
Says that the image below uses width:200 and is shown at full size.
The wrapper has the class demo-wrong, which makes the descendant selector in the CSS match the image inside it. The image is the same photo in both examples.
Says that the image below uses width:200px.
The wrapper has demo-correct, so the valid width applies. Compared with the first image, this one is 200px wide.
</body> closes the body.
</html> is the final closing tag and ends the document.
Best Practices
Use images with similar dimensions for a consistent layout.
Use CSS Grid to create responsive image galleries.
Add spacing using the gap property instead of margins.
Use subtle hover effects to improve user experience.
Optimize images to reduce page loading time.
Include descriptive alt text for accessibility.
Use rounded corners and shadows sparingly for a modern design.
Test the gallery on desktop, tablet, and mobile devices.