How to use grid for images in bootstrap card ? - GeeksforGeeks

29 Jan, 2020
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="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </script>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">    </script>    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">    </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="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">            </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= "https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">           <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">     </script>           <script src= "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">     </script>           <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">     </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="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">                <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200120152724/gfg_icon.png">                <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png">            </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:
Bootstrap-4
Bootstrap-Misc
Picked
Bootstrap
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Show Images on Click using HTML ?
How to set Bootstrap Timepicker using datetimepicker library ?
How to Use Bootstrap with React?
Create a Homepage for Restaurant using HTML , CSS and Bootstrap
Tailwind CSS vs Bootstrap
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?