Introduction
This article will demonstrate you, how you can use bootstrap 4 cards components. A cardis a flexible and extensible content container. It includes options for headers and footers, a wide variety of content, contextual background colors, and powerful display options. if you are familiar with Bootstrap 3, cards replace old panals,well and thumbnails. Cards are build with as little markup and styles as possible, but still manage to deliver a ton of control and customazation. Card build with flexbox, they offer easy alignment and mix well with other bootstrap components.
To use Bootstrap 4 card components your project, you need to have the following downloaded or cdn link scripts,
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <div class="card">
- <div class="card-body">Basic card</div>
- </div>
- </div>
- </body>
- </html>
Output
Example of Card with header and footer
The .card-header class adds a heading to the card and the .card-footer class adds a footer to the card.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Card</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <div class="card">
- <div class="card-header">
- <h5>header</h5>
- </div>
- <div class="card-body">
- <h5>bady</h5>
- </div>
- <div class="card-footer">
- <h5>footer</h5>
- </div>
- </div>
- </div>
- </body>
- </html>
Output
Example of Card Titles, Text, and Links
Use .card-title to add card titles to any heading element. The .card-text class is used to remove bottom margins for a p element if it is the last child (or the only one) in card-body. The .card-link class adds a blue color to any link, and a hover effect.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <div class="card">
- <div class="card-body">
- <div class="card-title">
- Card-title
- </div>
- <div class="card-text">
- Card text
- </div>
- <a href="#" class="card-link">link-1</a>
- <a href="#" class="card-link">link-2</a>
- <a href="#" class="card-link">link-3</a>
- </div>
- </div>
- </div>
- </body>
- </html>
Output
Example of with Card Images
Add .card-img-top or .card-img-bottom to an <img> to place the image at the top or at the bottom inside the card. Note that we have added the image outside of the .card-body to span the entire width.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <h3>Bootstrap 4 Card Image</h3>
- <h4>Card image top</h4>
- <div class="card" style="width:250px;">
- <img class="card-img-top" src="images/farhan.jpg" alt="farhan" />
- <div class="card-body">
- <h4 class="card-title">Farhan Ahmed</h4>
- <p class="card-text">Hi, I am full-stack developer.</p>
- <a href="#" class="btn btn-primary rounded-0">View Profile</a>
- </div>
- </div>
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <h3>Bootstrap 4 Card Image</h3>
- <h4>Card image bottom</h4>
- <div class="card" style="width:250px;">
- <div class="card-body">
- <h4 class="card-title">Farhan Ahmed</h4>
- <p class="card-text">Hi, I am full-stack developer.</p>
- <a href="#" class="btn btn-primary rounded-0">View Profile</a>
- </div>
- <img class="card-img-bottom" src="images/farhan.jpg" alt="farhan" />
- </div>
- </div>
- </body>
- </html>
Example of Card Image Overlay
Card image overlay turns an image into a card background and use .card-img-overlay to overlay the card's text.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <h3>Bootstrap 4 Card Image Overlay</h3>
- <div class="card img-fluid" style="width:250px;">
- <img class="card-img-top" src="images/farhan.jpg" alt="farhan" />
- <div class="card-img-overlay">
- <h4 class="card-title">Farhan Ahmed</h4>
- <p class="card-text">Hi, I am full-stack developer.</p>
- </div>
- </div>
- </div>
- </body>
- </html>
Example of Card Columns
The .card-columns class creates a masonry-like grid of cards. The layout will automatically adjust as you insert more cards.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <h4>Card Columns</h4>
- <div class="card-columns">
- <div class="card bg-primary">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card One</h5>
- <p class="card-text">Some text inside the first card</p>
- </div>
- </div>
- <div class="card bg-secondary">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Two</h5>
- <p class="card-text">Some text inside the Second card</p>
- </div>
- </div>
- <div class="card bg-danger">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Three</h5>
- <p class="card-text">Some text inside the third card</p>
- </div>
- </div>
- <div class="card bg-success">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Fourth</h5>
- <p class="card-text">Some text inside the Fourth card</p>
- </div>
- </div>
- <div class="card bg-primary">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Fifth</h5>
- <p class="card-text">Some text inside the fifth card</p>
- </div>
- </div>
- <div class="card bg-warning">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Sixth</h5>
- <p class="card-text">Some text inside the sixth card</p>
- </div>
- </div>
- <div class="card bg-info">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card Seventh</h5>
- <p class="card-text">Some text inside the Seventh card</p>
- </div>
- </div>
- <div class="card bg-dark">
- <div class="card-body text-center text-white">
- <h5 class="text-center">Card eighth</h5>
- <p class="card-text">Some text inside the eighth card</p>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Example of Card Deck
The .card-deck class creates a grid of cards that are of equal height and width. The layout will automatically adjust as you insert more cards.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Card Deck</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <div class="card-deck">
- <div class="card bg-primary rounded-0">
- <div class="card-body">
- <h4 class="text-center text-white">Card-1</h4>
- </div>
- </div>
- <div class="card bg-secondary rounded-0">
- <div class="card-body">
- <h4 class="text-center text-white">Card-2</h4>
- </div>
- </div>
- <div class="card bg-success rounded-0">
- <div class="card-body">
- <h4 class="text-center text-white">Card-3</h4>
- </div>
- </div>
- <div class="card bg-danger rounded-0">
- <div class="card-body">
- <h4 class="text-center text-white">Card-4</h4>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Example of Card Group
The .card-group class is similar to .card-deck. The only difference is that the .card-group class removes left and right margins between each card.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Card Group</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container py-5">
- <div class="card-group">
- <div class="card bg-primary">
- <div class="card-body">
- <h4 class="text-center text-white">Card-1</h4>
- </div>
- </div>
- <div class="card bg-secondary">
- <div class="card-body">
- <h4 class="text-center text-white">Card-2</h4>
- </div>
- </div>
- <div class="card bg-success">
- <div class="card-body">
- <h4 class="text-center text-white">Card-3</h4>
- </div>
- </div>
- <div class="card bg-danger">
- <div class="card-body">
- <h4 class="text-center text-white">Card-4</h4>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>