Academic Block

CSS Image Gallery Get Certified

What is a CSS Image Gallery?

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-gallery {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.demo-photo {
    width: 150px;
    border: 2px solid #007BFF;
}
</style>
</head>
<body>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/300/200?1" alt="Gallery image 1">
<img class="demo-photo" src="https://picsum.photos/300/200?2" alt="Gallery image 2">
<img class="demo-photo" src="https://picsum.photos/300/200?3" alt="Gallery image 3">
</div>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-gallery {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}
.demo-photo {
    width: 180px;
    border: 2px solid #ddd;
    border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/360/240?1" alt="Gallery image 1">
<img class="demo-photo" src="https://picsum.photos/360/240?2" alt="Gallery image 2">
<img class="demo-photo" src="https://picsum.photos/360/240?3" alt="Gallery image 3">
</div>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-gallery {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}
.demo-photo {
    width: 180px;
    border: 2px solid #ddd;
    transition: .3s;
    border-radius: 8px;
}
.demo-photo:hover {
    transform: scale(1.1);
    border-color: #007BFF;
}
</style>
</head>
<body>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/360/240?1" alt="Gallery image 1">
<img class="demo-photo" src="https://picsum.photos/360/240?2" alt="Gallery image 2">
<img class="demo-photo" src="https://picsum.photos/360/240?3" alt="Gallery image 3">
</div>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 15px;
}
.demo-photo {
    width: 100%;
    border-radius: 10px;
    border: 2px solid #ddd;
    box-sizing: border-box;
    box-shadow: 0 3px 8px rgba(0,0,0,.2);
}
</style>
</head>
<body>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/300/200?1" alt="Gallery image 1">
<img class="demo-photo" src="https://picsum.photos/300/200?2" alt="Gallery image 2">
<img class="demo-photo" src="https://picsum.photos/300/200?3" alt="Gallery image 3">
<img class="demo-photo" src="https://picsum.photos/300/200?4" alt="Gallery image 4">
<img class="demo-photo" src="https://picsum.photos/300/200?5" alt="Gallery image 5">
<img class="demo-photo" src="https://picsum.photos/300/200?6" alt="Gallery image 6">
</div>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    background: #f5f9ff;
}
.demo-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}
.demo-photo {
    width: 100%;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0,0,0,.2);
    transition: .3s;
}
.demo-photo:hover {
    transform: scale(1.05);
    box-shadow: 0 8px 18px rgba(0,0,0,.35);
}
</style>
</head>
<body>
<h2>Nature Gallery</h2>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/400/300?1" alt="Gallery image 1">
<img class="demo-photo" src="https://picsum.photos/400/300?2" alt="Gallery image 2">
<img class="demo-photo" src="https://picsum.photos/400/300?3" alt="Gallery image 3">
<img class="demo-photo" src="https://picsum.photos/400/300?4" alt="Gallery image 4">
<img class="demo-photo" src="https://picsum.photos/400/300?5" alt="Gallery image 5">
<img class="demo-photo" src="https://picsum.photos/400/300?6" alt="Gallery image 6">
</div>
</body>
</html>

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>

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.

Web Resources on CSS Image Gallery

1. MDN Web Docs : Image gallery
2. Light Gallery JS.com : Responsive Image Gallery
3. Dev.to : Creating an Interactive Image Gallery with HTML and CSS.

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.