-
-
Notifications
You must be signed in to change notification settings - Fork 11
CSS Challenge: Responsive Image Gallery with Card Hover Effects #2965
Description
This challenge focuses on creating a responsive image gallery using CSS Grid or Flexbox and enhancing it with subtle hover effects on each image card. We'll achieve a clean, modern look suitable for showcasing a portfolio or product lineup. This solution uses CSS Grid for layout and pure CSS for the hover effects, avoiding the need for JavaScript.
Description of the Styling
The gallery will consist of image cards arranged in a grid. The number of columns will adapt to the screen size, ensuring responsiveness. Each card will contain an image and an optional caption. On hover, the image will subtly scale up, and a semi-transparent overlay with the caption will appear.
Full Code (CSS Only):
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* Responsive grid */
grid-gap: 20px;
padding: 20px;
}
.gallery-item {
position: relative; /* Needed for absolute positioning of overlay */
overflow: hidden; /* Prevents image overflow on scale */
}
.gallery-item img {
transition: transform 0.3s ease; /* Smooth transition on hover */
width: 100%;
height: auto;
display: block; /* Prevents extra space below image */
}
.gallery-item:hover img {
transform: scale(1.05); /* Subtle scale-up on hover */
}
.gallery-item .overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent overlay */
opacity: 0;
transition: opacity 0.3s ease; /* Smooth transition on hover */
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.gallery-item:hover .overlay {
opacity: 1; /* Show overlay on hover */
}
.gallery-item .caption {
padding: 10px;
text-align: center;
}
/* Optional: Responsive adjustments for smaller screens */
@media (max-width: 768px) {
.gallery {
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
}HTML Structure (Example):
<div class="gallery">
<div class="gallery-item">
<img src="image1.jpg" alt="Image 1">
<div class="overlay">
<div class="caption">Image 1 Caption</div>
</div>
</div>
<div class="gallery-item">
<img src="image2.jpg" alt="Image 2">
<div class="overlay">
<div class="caption">Image 2 Caption</div>
</div>
</div>
<!-- Add more gallery items as needed -->
</div>Explanation
- CSS Grid:
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));creates a responsive grid.auto-fitensures columns adjust based on screen size,minmax(250px, 1fr)sets a minimum column width of 250px and allows columns to expand to fill available space. - Hover Effects:
transitionproperty creates smooth animations for the image scaling and overlay opacity changes. - Overlay: The
.overlayclass usesrgbafor semi-transparent background.position: absoluteoverlays it on the image. - Responsiveness: The
@mediaquery adjusts the grid for smaller screens.
Links to Resources to Learn More
- CSS Grid Layout: MDN Web Docs - CSS Grid Layout
- CSS Transitions: MDN Web Docs - CSS Transitions
- CSS Flexbox: (Alternative layout option) MDN Web Docs - CSS Flexible Box Layout
Copyrights (c) OpenRockets Open-source Network. Free to use, copy, share, edit or publish.