How to use grid for images in bootstrap card ?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.