Bootstrap Cards
Bootstrap Cards are flexible content containers used to display related information such as text, images, buttons, products, courses, profiles and other content.
What is a Bootstrap Card?
A Bootstrap Card is a flexible and extensible content container. It is commonly used to group related content together in a visually attractive format.
Cards can contain different elements such as headings, paragraphs, images, links, buttons, lists and other Bootstrap components.
1. Basic Card
The basic Bootstrap Card is created using the
.card class.
The content inside the card is normally placed inside
the .card-body class.
.card creates the outer card,
while .card-body contains the content.
Syntax
<div class="card">
<div class="card-body">
Card Content
</div>
</div>
Example
<div class="card">
<div class="card-body">
<h5 class="card-title">
Bootstrap Card
</h5>
<p class="card-text">
This is a basic Bootstrap card.
</p>
</div>
</div>
Output:
Bootstrap Card
This is a basic Bootstrap card.
2. Card Title and Card Text
Bootstrap provides the
.card-title class for the title of a card
and the .card-text class for the description.
card-title is used for the main heading
of the card, while card-text is used for
the description or paragraph.
Example
<div class="card">
<div class="card-body">
<h5 class="card-title">
Full Stack Development
</h5>
<p class="card-text">
Learn frontend and backend development.
</p>
</div>
</div>
Output:
Full Stack Development
Learn frontend and backend development.
3. Card Header
The .card-header class creates a header
section at the top of a card.
It is useful when we want to separate the heading from the main card content.
Example
<div class="card">
<div class="card-header">
Course Information
</div>
<div class="card-body">
<h5 class="card-title">
.NET Full Stack
</h5>
<p class="card-text">
Learn .NET Full Stack Development.
</p>
</div>
</div>
Output:
.NET Full Stack
Learn .NET Full Stack Development.
4. Card Footer
The .card-footer class creates a footer
section at the bottom of a card.
Example
<div class="card">
<div class="card-body">
<h5 class="card-title">
Bootstrap Course
</h5>
<p class="card-text">
Complete Bootstrap training.
</p>
</div>
<div class="card-footer">
Updated Today
</div>
</div>
Output:
Bootstrap Course
Complete Bootstrap training.
5. Card with Image
Bootstrap allows us to add an image at the top of
a card using the .card-img-top class.
.card-img-top.
Syntax
<div class="card">
<img src="/Images/Boostrap/Image1.png"
class="card-img-top"
alt="Course Image">
<div class="card-body">
<h5 class="card-title">
Bootstrap Course
</h5>
<p class="card-text">
Learn Bootstrap step by step.
</p>
</div>
</div>
Output:
Bootstrap Course
Learn Bootstrap step by step.
6. Card Image Bottom
The .card-img-bottom class is used when
we want the image to appear at the bottom of the card.
.card-img-top puts the image at the top,
while .card-img-bottom puts the image
at the bottom.
Syntax
<div class="card">
<div class="card-body">
<h5 class="card-title">
Bootstrap
</h5>
<p class="card-text">
Bootstrap makes UI development easier.
</p>
</div>
<img src="/Images/Boostrap/Image2.png"
class="card-img-bottom"
alt="Bootstrap Image">
</div>
Output:
Bootstrap
Bootstrap makes UI development easier.
7. Card Links and Buttons
Cards can contain buttons and links to perform actions or navigate to another page.
Example
<div class="card">
<div class="card-body">
<h5 class="card-title">
Java Full Stack
</h5>
<p class="card-text">
Learn Java Full Stack Development.
</p>
<a href="#" class="btn btn-primary">
Learn More
</a>
</div>
</div>
Output:
8. Card with List Group
Bootstrap Cards can also contain a
list-group.
This is useful when a card needs to display a list of related items.
Example
<div class="card">
<div class="card-header">
Course Topics
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
HTML
</li>
<li class="list-group-item">
CSS
</li>
<li class="list-group-item">
Bootstrap
</li>
</ul>
</div>
Output:
- HTML
- CSS
- Bootstrap
9. Card Colors
Bootstrap provides contextual classes that can be used to change the background and text color of cards.
Common classes include
bg-primary,
bg-success,
bg-warning,
bg-danger and
bg-dark.
Example
<div class="card bg-primary text-white">
<div class="card-body">
Primary Card
</div>
</div>
Output:
10. Card Group
The .card-group class groups multiple
cards together.
Card groups are useful when we want to display related cards together in a single section.
Example
<div class="card-group">
<div class="card">
<div class="card-body">
Java
</div>
</div>
<div class="card">
<div class="card-body">
Python
</div>
</div>
<div class="card">
<div class="card-body">
.NET
</div>
</div>
</div>
Output:
Java
Java Full Stack Development
Python
Python Full Stack Development
.NET
.NET Full Stack Development
11. Cards Using Bootstrap Grid
Bootstrap Grid can be used to create responsive card layouts.
We can use row and col-md-4
to display three cards in a row on medium and larger
screens.
Example
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body">
Java
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
Python
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
.NET
</div>
</div>
</div>
</div>
Output:
Java
Java Full Stack Development
Python
Python Full Stack Development
.NET
.NET Full Stack Development
12. Real-World CIIT Training Institute Example
Cards are very useful for displaying courses on a training institute website.
Each course can be displayed inside a separate card containing the course name, description, duration and an action button.
Example
<div class="card">
<div class="card-body">
<h5 class="card-title">
.NET Full Stack Development
</h5>
<p class="card-text">
Learn frontend, backend, database,
API and cloud technologies.
</p>
<a href="#" class="btn btn-primary">
Enroll Now
</a>
</div>
</div>
Output:
.NET Full Stack Development
Learn frontend, backend, database, API and cloud technologies.
Enroll NowImportant Bootstrap Card Classes
| Class | Purpose |
|---|---|
.card |
Creates the main card container. |
.card-body |
Creates the main content area. |
.card-title |
Styles the card title. |
.card-text |
Styles the card description. |
.card-header |
Creates the card header. |
.card-footer |
Creates the card footer. |
.card-img-top |
Places an image at the top. |
.card-img-bottom |
Places an image at the bottom. |
.card-group |
Groups multiple cards together. |
.h-100 |
Makes cards use the full available height. |
Common Mistakes
-
Forgetting to place the main content inside
.card-body. - Using very large images without responsive classes.
-
Forgetting the
altattribute for images. - Creating cards without responsive Bootstrap columns.
- Using fixed widths that cause horizontal scrolling on mobile devices.
- Not keeping equal card heights when cards are displayed in a grid.
Summary
- Bootstrap Cards are flexible containers for displaying related content.
-
.cardcreates the main card container. -
.card-bodycontains the main card content. -
.card-titleis used for the card heading. -
.card-textis used for card descriptions. -
.card-headercreates a card header. -
.card-footercreates a card footer. -
.card-img-topplaces an image at the top. -
.card-img-bottomplaces an image at the bottom. - Cards can be combined with Bootstrap Grid to create responsive layouts.
- Cards are commonly used for courses, products, profiles, services and dashboard information.