CSS Image Gallery
In this page:
.gallery img {
width: length;
height: auto;
}
.gallery {
display: flex | grid;
gap: length;
}
The Gallery Grid Concept
An image gallery organizes visual cards into clean, structured grids. By defining a fluid layout structure and wrapping your images inside styled cards, you can keep your photos aligned cleanly on any screen size.
Example: The Gallery Grid Concept
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
}
img {
width: 100%;
height: 120px;
}
</style>
<div class="gallery">
<img src="https://placehold.co/150x120" alt="Photo">
<img src="https://placehold.co/150x120" alt="Photo">
</div>
Styling Image Cards with Captions
To make your gallery interactive, you can pair your images with styled captions or description texts. Wrapping the image and description in a styled card box makes the information clear to your visitors.
Example: Styling Image Cards with Captions
<style>
figure {
margin: 0;
}
figcaption {
padding: 8px;
}
</style>
<figure>
<img src="https://placehold.co/200x120" alt="Photo">
<figcaption>A brief caption</figcaption>
</figure>
Interactive Hover Zoom Effects
You can add custom hover animations (like a soft zoom effect) to make your image gallery interactive and engaging. By scaling the image slightly and clipping the overflow inside the card container, you create a polished, modern design.
Example: Interactive Hover Zoom Effects
<style>
.card {
overflow: hidden;
}
.card img {
transition: transform 0.3s;
}
.card:hover img {
transform: scale(1.1);
}
</style>
<div class="card">
<img src="https://placehold.co/200x150" alt="Zooms on hover">
</div>
Modern Galleries with Flexbox
Using Flexbox is the modern, standard way to build responsive image galleries. It allows your image cards to align horizontally and wrap onto new lines automatically, adapting perfectly to any screen size.
Example: Modern Galleries with Flexbox
<style>
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
img {
width: 150px;
}
</style>
<div class="gallery">
<img src="https://placehold.co/150x100" alt="Photo">
<img src="https://placehold.co/150x100" alt="Photo">
</div>
Using CSS Object-Fit on Gallery Images
If your gallery images have different aspect ratios, they can look misaligned in a grid. You can use the CSS object-fit: cover property to crop and scale your images automatically to fill their containers without distorting.
- Always specify a defined height on your image elements when using object-fit: cover.
- Accepted values:
- fill — stretches to fit, may distort (initial value)
- contain — fits inside, keeping the aspect ratio
- cover — fills the box, keeping the aspect ratio and cropping
- none — natural size
- scale-down — smaller of none and contain
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Using CSS Object-Fit on Gallery Images
<style>
img {
width: 150px;
height: 150px;
object-fit: cover;
}
</style>
<img src="https://placehold.co/300x150" alt="Cropped to fit without distortion">
- Using fixed pixel widths on your gallery columns, causing layout breakages on mobile screens.
- Forgetting to set overflow: hidden on the parent container when using hover zoom effects, letting the image spill out.
- Omitting width and height dimensions on image elements, which causes slow-loading layout shifts.
- Align image cards into fluid, responsive grids using Flexbox displays.
- Add context to your gallery cards using semantic figure and figcaption elements for captions.
- Use object-fit: cover to crop and scale images automatically without distortion, and apply transition hover zoom effects for responsive animations.
Standard image galleries and the object-fit property are natively supported by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline