Open In App

How to use grid for images in bootstrap card ?

Improve
Improve
Like Article
Like
Save
Share
Report

Images can be added to the Bootstrap card by simply using an img tag. But we cannot use the same method directly to place an Image Grid into the Bootstrap Card as it will be lead to a misaligned design. Therefore, to get a per-flow to place image Grid into Bootstrap Card. The perfectly aligned grid we need to add some CSS to our HTML code.

Approach: First set the value display: grid; of the div wrapping all the images to transform it into to a grid layout. Then set the value grid-template-columns: auto; of the grid container to specify the number of columns in the grid layout. Now set the value width: 100%; of the image to get a perfect grid.

Below examples illustrate the above approach:

Example 1: Image grid with 2 columns i.e. 2×2 Image grid.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>Bootstrap Card</title>
    <meta charset="utf-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1">
    <link rel="stylesheet"
        href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
      
    <style>
        .card {
            width: 20rem;
            margin: 2rem;
        }
          
        .image-grid-container {
            display: grid;
      
            /* For 2 columns */
            grid-template-columns: auto auto;
        }
        img {
            width: 100%;
        }
    </style>
</body>
  
<body>
    <div class="container">
        <div class="card">
            <div class="image-grid-container">
                <img src=
                <img src=
                <img src=
                <img src=
            </div>
              
            <div class="card-body">
                <h4 class="card-title">Developer Guy</h4>
                  
                <p class="card-text">
                    Developer Guy love to develop
                    front-end and back-end
                </p>
                  
                <a href="#" class="btn btn-primary">
                    See Profile
                </a>
            </div>
        </div>
    </div>
</body>
  
</html>


Output:

Example 2: Image grid with 3 columns i.e. 3×3 Image grid.




<!DOCTYPE html> 
<html lang="en"
  
<head
    <title>Bootstrap Cards</title
      
    <meta charset="utf-8"
    <meta name="viewport" content="width=device-width, initial-scale=1"
  
    <link rel="stylesheet" href
      
    <script src
    </script
      
    <script src
    </script
      
    <script src
    </script
    <style>
        .card {
            width: 20rem;
            margin: 2rem;
        }
        .image-grid-container {
            display: grid;
              
            /* For 3 columns */
            grid-template-columns: auto auto auto;
        }
        img {
            width: 100%;
        }
    </style>
</head
  
<body>
    <div class="container"
        <div class="card"
            <div class="image-grid-container">
                <img src=
                <img src=
                <img src=
                <img src=
                <img src=
                <img src=
                <img src=
                <img src=
                <img src=
            </div
            <div class="card-body"
                <h4 class="card-title">
                    Developer Guy
                </h4
                  
                <p class="card-text">
                    Developer Guy love to develop
                    front-end and back-end 
                </p
                <a href="#" class="btn btn-primary">
                    See Profile
                </a
            </div>
        </div>
    </div>
</body>
  
</html>


Output:



Last Updated : 29 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads