×

CSS Image Gallery

Image galleries are used to display and store collections of images.

Example

Let’s discuss an example.

CSS was used to create the following image gallery:

Code is as follows:

<html>
<head>
<style>
div.gallery {
  margin: 6px;
  border: 1px solid rgb (185, 153, 153);
  float: left;
  width: 175px;
}


div.gallery:hover {
  border: 1px solid rgb(141, 112, 112);
}


div.gallery img {
  width: 105%;
  height: auto;
}


div.desc {
  padding: 16px;
  text-align: center;
}
</style>
</head>
<body>


<div class="gallery">
  <a target="_blank" href="coastline_image.png">
    <img src="coastline_image.png" alt="coastline - Cinque Terre" width="550" height="50">
  </a>
  <div class="desc">Here is where you can describe the image</div>
</div>


<div class="gallery">
  <a target="_blank" href="rivermountain_image.png">
    <img src="rivermountain_image.png" alt="river-mountain" width="550" height="410">
  </a>
  <div class="desc">Here is where you can describe the image</div>
</div>


<div class="gallery">
  <a target="_blank" href="northlight_image.png">
    <img src="northlight_image.png" alt="Northern-Lights" width="550" height="410">
  </a>
  <div class="desc">Here is where you can describe the imagee</div>
</div>


<div class="gallery">
  <a target="_blank" href="forest_image.png">
    <img src="forest_image.png" alt="Forest.." width="550" height="410">
  </a>
  <div class="desc">Here is where you can describe the image</div>
</div>


</body>
</html>

The output for this above code is shown below:

CSS Image Gallery

Responsive Image Gallery

The following example essentially shows how to make a responsive Image Gallery using HTML and CSS.

The following steps will help you essentially create a responsive image gallery:

  • Step 1: Laying out the kind of basic structure of a gallery.
    There are some div sections in each gallery. Within each div section, an image and a description are displayed.
<div class="gallery">
  <div class="box"> 
    <div class="image"> The image is added here </div>
    <div class="text"> The text is added here </div>
  </div>
  • Step 2: A CSS style sheet can be used to style the file
    Here essentially are some guidelines for styling the gallery container:
    Select flex as the display property. It literally means that items within the gallery container will display in a flex context in a big way.
    To set the flex-flow property to row wrap, set it to true, or so they thought. This specifies both the direction of flexing and the style of wrapping.
.gallery {
  width:110%;
  display:flex;
  flex-flow: row wrap;
}

- Box styling is done below:

.box {
    flex-basis: 18%;
    width: 110%;
    padding: 11px;
    margin: 9px;
     // Set the blur shadow
    box-shadow: 1px 1px 14px 1px red; 
}

-  Step 3: By using @media query to make a responsive image gallery

@media only screen and (max-width: 310px) 
{ 
 .box {
    flex-basis: 105%;
}

Code is as follows:

<!DOCTYPE html>
<html>
      
<head>
    <style>
      
        /* style to set body of page */
        body {
            width:90%; 
            margin:auto;
        }
        .gallery {
            width:90%;
            display:flex;
            flex-flow: row wrap;
        }
        .box {
            flex-basis:18%;
            width:90%;
            padding:11px;
            margin:9px;
            box-shadow: 1px 1px 14px 1px red;
        }
        .text {
            text-align:center;
            margin-top:5px;
        }
        .image:hover {
            border: 3px solid red ;
        }
          
        /* media query used here */
        @media only screen and (max-width: 300px) { 
            .box {
                flex-basis: 100%;
            }
        }
          
        @media only screen and (max-width:300px) {
            .box {
                flex-basis: 40%;
            }
        }
    </style>
</head>
  
<body>
    <div class = "gallery">
        <div class = "box">
            <div class = "image">
                <a target="_blank" href=
"javatpoint logo_image.png">
                    <img src =
"javatpoint logo_image.png" 
                width = "300" height = "200">
                </a>
            </div>
              
            <div class = "text">
                Javatpoint Image
            </div>
        </div>
      
        <div class = "box">
            <div class = "image">
                <a target="_blank" href=
"logo image.png">
                    <img src =
"logo image.png"
                width = "300" height = "200">
                </a>
            </div>
              
            <div class = "text">
                Javatpoint Image
            </div>
        </div>
          
        <div class = "box">
            <div class = "image">
                <a target="_blank" href=
"Javatpoint img.png">
                    <img src = 
"Javatpoint img.png" 
                width = "300" height = "200">
                </a>
            </div>
              
            <div class = "text">
                Javatpoint Interview qns Image
            </div>
        </div>
</body>
</html>

The output for this above code is shown below:

(Responsive image gallery is shown in output)

CSS Image Gallery
  • Some more example for responsive image gallery:
  • Here are some CSS media queries that you can use to make a responsive image gallery that will look good on any device, including desktops, tablets, and smart phones.

Example

Code is as follows:

<!DOCTYPE html>
<html>
<head>
<style>
div.gallery {
  border: 1px solid rgb(233, 133, 133);
}


div.gallery:hover {
  border: 1px solid rgb(241, 121, 121);
}


div.gallery img {
  width: 90%;
  height: auto;
}


div.desc {
  padding: 14px;
  text-align: center;
}


* {
  box-sizing: border-box;
}


.responsive {
  padding: 0 6px;
  float: left;
  width: 23.99999%;
}


@media only screen and (max-width: 600px) {
  .responsive {
    width: 48.99999%;
    margin: 5px 0;
  }
}


@media only screen and (max-width: 460px) {
  .responsive {
    width: 90%;
  }
}


.clearfix:after {
  content: "";
  display: table;
  clear: both;
}
</style>
</head>
<body>


<h2> Responsive Image Gallery </h2>
<h4>The effect can be seen by resizing the browser window. </h4>
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="coastline_image.png">
      <img src="coastline_image.png" alt="coastline Cinque-Terre" width="500" height="300">
    </a>
    <div class="desc">Here is where you can describe the image</div>
  </div>
</div>


<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="rivermountain_image.png">
      <img src="rivermountain_image.png" alt="River - Mountain" width="500" height="300">
    </a>
    <div class="desc">Here is where you can describe the image</div>
  </div>
</div>


<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="northlight_image.png">
      <img src="northlight_image.png" alt="North lights.." width="500" height="300">
    </a>
    <div class="desc">Here is where you can describe the image</div>
  </div>
</div>


<div class="responsive">
  <div class="gallery">
    <a target="_blank" href ="forest_image.png">
      <img src="forest_image.png" alt="forest_image..." width="500" height="300">
    </a>
    <div class="desc">Here is where you can describe the image</div>
  </div>
</div>


<div class="clearfix"></div>


<div style="padding: 6px;">
  <p>Media queries and responsive web design will be covered in more detail in our CSS tutorial. </p>
</div>
</body>
</html>

In this example, we use media queries to change the arrangement of the images on different screen sizes: <br> for screens larger than 700px wide, it will show four images side by side, for screens smaller than 700px wide, <br> it will show two images side by side. When the screen size is less than 500px, the images stack vertically (100%).

The output for this above code is shown below:
(Responsive image gallery is shown in output)

CSS Image Gallery CSS Image Gallery

Related Topics

CSS Hover

CSS Hover: CSS :hover is used to select the various elements whenever we point the mouse on these elements. We can also use this selector on all the HTML elements,...

3 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made...

3 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Clip

CSS Clip The Clip property in CSS describes the element’s visible area. It applies over the elements which are absolutely positioned (position: absolute ;). Generally, it is used when an image...

2 minutes read.

Is CSS a programming language?

 A programming language is a set of grammatical rules and a vocabulary for commanding a machine to accomplish specified tasks. High-level languages like BASIC, C, C++, COBOL, Java, FORTRAN, Ada...

4 minutes read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

35 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 minutes read.