Creating a Responsive Flex Gallery in CSS


A Flex Gallery is a popular layout used to display a collection of images or other media in a responsive manner. It's implemented using the Flexbox layout in CSS and can be easily customized to fit different design needs.

The Flexbox layout allows for easy manipulation of elements within a container, which makes it an ideal choice for creating a gallery layout. Using the display: flex property on the container element and various properties such as flex-direction, justify-content, align-items, and flex-wrap, the items within the gallery can be aligned, spaced, and ordered in a flexible and dynamic way.

In addition to the basic Flexbox layout, other techniques such as using media queries, transform, and transition can be employed to create a responsive and visually appealing gallery design. For example, using transform: scale() on hover can create an effect where the image grows slightly when the cursor hovers over it, and using transition can make the effect smooth and gradual.

CSS Grid can also be used to create a gallery layout, but Flexbox is generally easier to use for simpler designs that require a one-dimensional layout.

Overall, a Flex Gallery in CSS is a flexible and effective way to display images or other media in a visually appealing and responsive manner.

For Example

This code creates a basic image gallery using Flexbox layout in CSS.

Source Code :

<!DOCTYPE html>
<html lang="en">
  <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Gallery</title>
   <style>
    .container{
     display: flex;
     flex-wrap: wrap;
     justify-content: center;
    }
    .box {
     padding: 5px;
     opacity: 0.8;
    }
    .box:hover{
     opacity: 1;
     transition: .9s;
    }
   </style>
 </head>
 <body>
   <div class="container">
     <div class="box"><img src="images/1.jpg" alt=""></div>
     <div class="box"><img src="images/2.jpg" alt=""></div>
     <div class="box"><img src="images/3.jpg" alt=""></div>
     <div class="box"><img src="images/4.jpg" alt=""></div>
     <div class="box"><img src="images/5.jpg" alt=""></div>
     <div class="box"><img src="images/6.jpg" alt=""></div>
     <div class="box"><img src="images/7.jpg" alt=""></div>
     <div class="box"><img src="images/8.jpg" alt=""></div>
     <div class="box"><img src="images/9.jpg" alt=""></div>
   </div>
 </body>
</html>

The HTML markup consists of a container div element with a class of container and a number of div elements within it, each with a class of box. The img tag within each div element specifies the source image to be displayed in the gallery.

The CSS styling for the .container class specifies display: flex and flex-wrap: wrap to make the items wrap when the container width is too small, and justify-content: center to horizontally center the items within the container.

The .box class applies a padding of 5px and an opacity of 0.8 to each image. When the mouse hovers over the image, the opacity increases to 1 and the transition effect takes 0.9s to complete.

Overall, this code creates a basic image gallery with Flexbox layout that is responsive and has a simple hover effect. However, additional styling and design modifications can be applied to make the gallery more visually appealing and user-friendly.

Live Preview

CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects