×

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 Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

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 Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 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.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 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.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 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.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 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.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 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.