Bootstrap Cards
You may utilize the Bootstrap card components after watching this tutorial.
Using the Bootstrap Cards
The Bootstrap card is a versatile and adaptable container for content. It offers options for headers and footers, a wide range of content types, contextual background colors, and flexible display choices. The card component replaces the panel, well, and thumbnail components from the old Bootstrap 3 version.
Creating a Basic Card
The card markup is straightforward. The outer wrapper requires the base class .card
, and content can be placed inside the .card-body
element. The following example demonstrates how to create a card with a picture, mixed with some text content and a button.
Alice Liddel
Alice is a freelance web designer and developer based in London. She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.
View Profile
Note: Cards do not have a fixed width; they occupy the full width of their parent element. If you need a fixed-width card, you can apply the width property to the card yourself. Additionally, cards have no margin by default, but you can use spacing utility classes if needed.
Content Types for Card Component
The card component supports a wide variety of content, including images, text, list groups, links, navs, and more. Here are examples of what is supported by the card:
Body Only Card
You can simply use .card
with .card-body
inside whenever you need to create a padded box.
This text is enclosed in a box with padding.
Card with Titles, Text, and Links
Furthermore, you can include titles and links inside the card along with text, as shown below:
Taj Mahal
Agra, Uttar Pradesh, India.
Built: 1631–1653 Taj Mahal is one of the most iconic landmarks in the world
View pictures
Learn more
Card with Header and Footer
You can also add a header and footer within your cards using the .card-header
and .card-footer
class, respectively. The following example demonstrates this:
Featured
NASA Launched Solar Probe
In order to conduct studies of the outer solar corona, NASA deployed the Parker space probe in 2018. It is the first mission to ever "touch" the Sun.
Know more
Tip: You can use text-align
utility classes such as .text-center
and .text-end
to align the card's content to the center and right end, respectively. By default, the content is left-aligned.
Placing List Groups within Card
List groups can also be placed inside the card alongside other content types, as depicted here:
Mix and Match Multiple Content Types within Card
Feel free to mix and match multiple content types to create the card layout you desire. The following example creates a fixed-width card with an image, text, list group, and hyperlinks.
Card title
The content of the card is represented by the following sample text. You may always substitute your own text for it.
- Part 1
- Part 2
- Part 3
Adding Navigation to Cards
Additionally, you can add Bootstrap's nav components, such as tabs and pills, to the card header.
To add tabs navigation, simply place the tabs markup inside the card header and the tabs content inside the card body. You will also need to use the additional class
.card-header-tabs
on the .nav
element along with the class .nav-tabs
for proper alignment.
The example below illustrates an elegant tabbed navigation.
Home tab content
The contents of the tabs is represented by the following sample text. Anytime you want, substitute your own text.
Go somewhere
You can also incorporate pills nav into the card by adding the additional class .card-header-pills
along with the class .nav-pills
on the .nav
element, as demonstrated below:
Home tab content
The contents of the tabs is represented by the following sample text. Anytime you want, substitute your own text.
Go somewhere
Customizing the Card Styles
There are various options available for customizing the appearance of a card's backgrounds, borders, and colors.
Customizing Background and Color
You can easily change the card's appearance using background and color utility classes. The following example illustrates how it works:
First card title
To fill up the content of the cards, some fake text. It is always replaceable.
Customizing Border and Color
Similarly, you can customize the border and text color of any card using the text and border utility classes. Simply apply these classes to the .card
or its child elements, as shown here:
First card title
To fill up the card's content, some fictitious words. It can always be changed.
Card Layout Options
Aside from styling options, Bootstrap offers a few layout options for arranging a series of cards. However, these layouts are not yet responsive.
Creating the Card Groups
Card groups allow you to display cards as a single, attached element with equal-width and equal-height columns. However, on extra small devices (viewport width < 576px), cards inside a card group become horizontally stacked. Let's explore an example to see how it works:
Card title
To fill up the content of the cards, some fake text. It is always replaceable.
Creating the Card Grids
You can control the number of grid columns (wrapped around your cards) per row using the Bootstrap grid system and its .row-cols-*
classes. For instance, you can use .row-cols-1
to display one card per row or .row-cols-md-2
to show two cards per row from the medium breakpoint up (viewport width ≥ 768px).
Card title
To fill up the content of the cards, some fake text. It is always replaceable.
Creating Horizontal Cards
To create horizontal cards where image and text content are placed side-by-side, you can use a combination of grid and utility classes, as depicted in the following example:
Alice Liddel
London-based independent web designer and developer named Alice. She is skilled in JavaScript, HTML5, CSS3, and Bootstrap, among other things.
View Profile
Card Image Overlays
Furthermore, you can turn an image into a card background and place the card's text on top of it using the class .card-img-overlay
instead of .card-body.
Depending on the image, additional styles may be required for better adjustments:
Note: It's essential to ensure that the card content does not exceed the height of the image. If the content is larger, it will be displayed outside the image.
Text Alignment inside Card
To change the text alignment of a card, you can use the text alignment utility classes. For example, the class .text-center
and .text-end
can be used to align the card's text content to the center and right end, respectively.
Specifying Card Size
Cards have no fixed width; they are 100% wide by default. However, you can adjust the width as needed using custom CSS, grid classes, or sizing utility classes.
The example below demonstrates this:
Card with Stretched Link
To make the entire card clickable, you can add the class .stretched-link
to a link inside the card. Multiple links are not recommended with stretched links.
The following example shows how it works:
Alice Liddel
London-based independent web designer and developer named Alice. She is skilled in JavaScript, HTML5, CSS3, and Bootstrap, among other things.
View Profile
FAQ
What is a Bootstrap Card?
A Bootstrap Card is a flexible and lightweight content container within the Bootstrap framework. It's a versatile component used to organize and display various types of content such as images, text, links, and more. Cards are designed to be visually appealing and responsive, adapting to different screen sizes and devices.
How do you create a basic Bootstrap Card?
To create a basic Bootstrap Card, you can use the following HTML structure:
In this example, the card
class creates the card container, the card-img-top
class is used for the image at the top, and the card-body
class contains the title, text, and button elements.
How can you customize the appearance of a Bootstrap Card?
Bootstrap provides various classes that you can use to customize the appearance of a card. Some common classes include:
bg-*
: To set the background color of the card, replacing *
with a color name or contextual class like primary
, success
, etc.text-*
: To change the text color, replacing *
with a color name or contextual class.border-*
: To add borders, replacing *
with a color name or contextual class.
Can you include headers, footers, and other elements in a Bootstrap Card?
Yes, you can include headers, footers, and other elements in a Bootstrap Card. For instance, you can add a card header using the card-header
class and a card footer using the card-footer
class. These sections can contain additional content like titles, icons, buttons, etc.
Card Header
How can you create a card layout with multiple cards in a row?
You can use Bootstrap's grid system to create a card layout with multiple cards in a row. For instance, to create a row of three cards, you can use the following structure:
How can you make the cards responsive for different screen sizes?
Bootstrap Cards are responsive by default. The grid system ensures that cards adjust their width and layout based on the screen size. To achieve this responsiveness, you should place your cards within the appropriate grid columns using classes like col-sm-*
, col-md-*
, col-lg-*
, etc., to control the number of columns the cards should span across for different screen sizes.
How can you create a card with different styles, such as horizontal cards or card groups?
Bootstrap offers different card styles, including horizontal cards and card groups. To create a horizontal card, you can use the flex-row
class to align the card content horizontally:
Horizontal Card
This is a horizontal card layout.
For card groups, you can enclose multiple cards within a card-group
or card-deck
container:
How can you control the width of a Bootstrap Card?
You can control the width of a Bootstrap Card by adjusting the width of the containing column. For instance, if you want a card to occupy half the width of its column, you can use the col-md-6
class:
How can you use card overlays for additional content, such as text or buttons on images?
Card overlays can be used to display additional content, such as text or buttons, on top of card images. You can achieve this by using the card-img-overlay
class:
How can you add icons or badges to a Bootstrap Card?
You can add icons using Bootstrap's icon classes (e.g., bi bi-heart
) and badges using the badge
class. Here's an example of adding an icon and a badge to a card:
New
In this example, the card-icon
class is used to style the icon.
How can you use card decks and card columns to arrange multiple cards?
Card decks and card columns are useful for arranging multiple cards in different layouts. A card deck ensures equal height for all cards, while card columns allow for responsive columns. Here's how to use them:
For card decks:
For card columns:
How can you add hover effects or animations to Bootstrap Cards?
You can add hover effects and animations using Bootstrap's utility classes or CSS. For example, to add a subtle zoom effect on hover:
.card-zoom:hover {
transform: scale(1.05);
transition: transform 0.3s ease;
}
You can customize the CSS properties to achieve the desired effect.
How can you make a card's height adjust to its content?
By default, Bootstrap Cards adjust their height based on their content. If you want to ensure that the card height is the same for multiple cards in a row, you can use the align-items-stretch
class on the parent container to make them equal in height.
Can you include forms or input elements inside a Bootstrap Card?
Absolutely! You can include forms, input elements, and other interactive components within a Bootstrap Card. Here's an example of a card containing a form:
Contact Us
How can you create card layouts with equal heights regardless of content?
If you want to ensure that cards have equal heights within a row, you can use the align-items-stretch
class on the parent container:
This ensures that all cards within the row have the same height, regardless of the content.
Can you use Bootstrap Cards for displaying images in a gallery format?
Yes, you can use Bootstrap Cards to create an image gallery. Each card can represent an image, and you can use a grid layout to organize them. For example:
Is it possible to create customized content using HTML within a Bootstrap Card?
Yes, you can create customized content using HTML within a Bootstrap Card. You have full control over the card's content, allowing you to combine various elements:
In this example, we're using Bootstrap's badge
class and an icon from Bootstrap Icons.
How can you create card layouts with images and captions?
To create card layouts with images and captions, you can use the card-img
class for the image and include the caption within the card-img-overlay
class. Here's an example:
How can you incorporate media content like videos or audio within a Bootstrap Card?
You can embed media content like videos or audio within a Bootstrap Card using standard HTML video
and audio
tags. Here's an example of embedding a video:
Video Card
Check out this video:
How can you use badges and labels within a Bootstrap Card?
You can incorporate badges and labels within a Bootstrap Card to highlight additional information. Use the badge
class for badges and customize their style using contextual classes:
Product Name
This product has a New label.
How can you include lists and other structured content within a Bootstrap Card?
You can easily include lists and structured content within a Bootstrap Card. Here's an example with an unordered list:
List Card
- Item 1
- Item 2
- Item 3
How can you create cards with varying background colors?
You can customize the background color of cards using Bootstrap's contextual classes, such as bg-primary
, bg-success
, etc. Here's an example:
Colored Card
This card has a primary background color.
How can you create cards with custom content alignment and layout?
You can use Bootstrap's utility classes to adjust content alignment and layout within a card. For example, to center-align content:
Centered Content
This content is centered within the card body.
What is a "Body Only" card in Bootstrap?
A "Body Only" card in Bootstrap refers to a card component that contains only the main content body without any additional headers, footers, or images. It's a minimalist way to present information in a clean and straightforward manner.
How can you create a "Body Only" card in Bootstrap?
To create a "Body Only" card, you can omit the card header, footer, and image elements, focusing solely on the card's content body:
In this example, the card-body
class is used to enclose the main content of the card.
Can you customize the appearance of a "Body Only" card?
Absolutely. You can customize the appearance of a "Body Only" card using various Bootstrap utility classes. For instance, you can apply background colors, text colors, and border styles to achieve the desired look.
Styled "Body Only" Card
This card has a green background and white text.
How can you create "Body Only" cards that expand to show more content?
You can create "Body Only" cards that expand to show more content using Bootstrap's Collapse component. Here's an example:
Expandable Card
Click the button to see more content:
Additional content revealed here.
Toggle Content
How can you make the entire card clickable using a link?
To make the entire card clickable using a link, you can wrap the entire card content within an anchor (<a>
) tag:
Clickable Card
This entire card is a clickable link.
How can you include icons within headers and footers of a Bootstrap Card?
You can include icons within headers and footers of a Bootstrap Card using Bootstrap's icon classes. Here's an example of adding icons to the header and footer:
Icon Header
Date: August 30, 2023
This is the card content.
In this example, the <i>
elements with the bi bi-calendar
and bi bi-link
classes add a calendar and link icon, respectively.
How can you make the footer of a Bootstrap Card stick to the bottom of the card, regardless of content length?
You can make the footer of a Bootstrap Card stick to the bottom of the card by utilizing the flexbox utility classes. Here's an example:
Card Title
This is the card content.
In this example, the d-flex
class makes the card a flex container, and mt-auto
pushes the footer to the bottom of the container.
How can you customize the appearance of List Group items within a Bootstrap Card?
You can customize the appearance of List Group items within a Bootstrap Card using various Bootstrap utility classes. For example, you can use list-group-item-primary
, list-group-item-success
, etc., to apply different colors:
- Primary Item
- Success Item
- Danger Item
How can you make List Group items within a Bootstrap Card interactive?
You can make List Group items within a Bootstrap Card interactive by adding appropriate CSS and JavaScript for handling events like clicks. For example:
- Item 1
- Item 2
function handleItemClick(itemId) {
alert('Clicked on Item ' + itemId);
}
How can you create a horizontally-oriented List Group within a Bootstrap Card?
To create a horizontally-oriented List Group within a Bootstrap Card, you can use the list-group-horizontal
class. Here's an example:
- Item 1
- Item 2
- Item 3
Can you use List Group items as navigation links within a Bootstrap Card?
Yes, you can use List Group items as navigation links within a Bootstrap Card. By adding href
attributes to the List Group items, you can create navigation links:
How can you create a List Group with flush (borderless) styling within a Bootstrap Card?
To create a List Group with flush (borderless) styling within a Bootstrap Card, you can add the list-group-flush
class to the list-group
element:
Flush List Group Card
- Item 1
- Item 2
- Item 3
How can you create a complex card layout with a List Group and additional content?
You can create a complex card layout with a List Group and additional content by combining the List Group with other card components. Here's an example:
Complex Card Layout
This card includes a List Group and additional content.
- Item 1
- Item 2
- Item 3
Can you add contextual styling to List Group items within a Bootstrap Card?
Yes, you can add contextual styling to List Group items within a Bootstrap Card using Bootstrap's contextual classes. These classes allow you to apply different background colors to the List Group items based on their purpose or status:
- Primary Item
- Success Item
- Danger Item
Can you mix horizontal and vertical content alignment within a Bootstrap Card?
Yes, you can mix horizontal and vertical content alignment within a Bootstrap Card using utility classes like d-flex
, justify-content-between
, and align-items-center
:
Aligned Card
New
How can you open navigation links in a new tab when clicked?
To open navigation links in a new tab when clicked, you can use the target="_blank"
attribute on the anchor (<a>
) tag:
Open Link in New Tab
This card's link will open in a new tab.
How can you create navigation cards that act as tabs to switch between content sections?
You can create navigation cards that act as tabs using the Bootstrap Tabs component. Each tab can contain a card with its own content. Here's an example:
Section 1 Content
This is the content of section 1.
Section 2 Content
This is the content of section 2.
How can I change the font style and size of text within Bootstrap cards?
To modify the font style and size of text within Bootstrap cards, you can target the relevant card elements using CSS. Here's how:
.custom-card {
font-family: "Arial", sans-serif; /* Change font family */
}
.card-title {
font-size: 24px; /* Change title font size */
}
.card-text {
font-size: 16px; /* Change content font size */
}
In this example, the .custom-card
class sets the font family for the entire card. The .card-title
and .card-text
classes control the font sizes of the card's title and content, respectively.
How can I create a hover effect that changes the background color of the Bootstrap card?
You can achieve a hover effect that changes the background color of a Bootstrap card using CSS. Here's an example:
.custom-card:hover {
background-color: #f0f0f0; /* Change background color on hover */
}
In this case, the .custom-card
class changes the background color of the card when it's hovered over by the user.
Can I change the padding and margin of Bootstrap cards to adjust spacing?
Yes, you can adjust the padding and margin of Bootstrap cards to control spacing around and within the card. Here's how:
.custom-card {
padding: 20px; /* Adjust internal spacing */
margin: 10px; /* Adjust spacing around the card */
}
The padding
property controls the internal spacing within the card's content, and the margin
property adjusts the spacing around the entire card.
How can I create a card with a transparent background?
To create a card with a transparent background, you can override the default card background color with a transparent value. Here's how:
.transparent-card {
background-color: transparent; /* Set background color to transparent */
border: 1px solid #ccc; /* Add a border for visibility */
}
In this example, the .transparent-card
class sets the card's background color to transparent. You can add a border for visibility or adjust other styling as needed.
How can I center-align text within a Bootstrap card's content?
To center-align text within a Bootstrap card's content, you can use CSS to adjust the text alignment. Here's an example:
.center-aligned-card {
text-align: center; /* Center-align text within the card */
}
Apply the .center-aligned-card
class to your card's HTML div element.
How can I create a Bootstrap card with a colored border and background?
You can create a Bootstrap card with a colored border and background by combining background color and border color properties. Here's an example using custom CSS:
.custom-card {
background-color: #F5B041; /* Background color */
border: 2px solid #E74C3C; /* Border color */
}
In this example, the .custom-card
class sets the background color to #F5B041
and the border color to #E74C3C
.
How can I apply different border radius to Bootstrap cards to create rounded corners?
Applying different border radius to Bootstrap cards can be done using CSS. You can control the corner roundness using the border-radius
property. Here's an example:
.rounded-card {
border-radius: 10px; /* Apply rounded corners */
}
In this case, the .rounded-card
class applies a border radius of 10px
to all corners of the card. You can also target specific corners using the border-radius
property's individual values, like border-top-left-radius
, border-top-right-radius
, etc.
How can I create a card group with cards displayed as a vertical list?
You can create a card group with cards displayed as a vertical list using the .card-group
class. Cards within a .card-group
stack vertically.
How can I create a responsive grid of cards that adapt to different screen sizes?
Bootstrap's grid system allows you to create responsive card grids by using different column classes for various screen sizes. Here's an example:
2 columns on small screens, 3 columns on medium screens, and 4 columns on large screens.
What are horizontal cards in Bootstrap?
Horizontal cards in Bootstrap are cards that are oriented horizontally instead of the traditional vertical layout. They are often used when you want to display content side by side within a card, such as an image on one side and text on the other.
How can I create a basic horizontal card in Bootstrap?
To create a basic horizontal card in Bootstrap, you can use the .flex-row
class along with utility classes for alignment. Here's an example:
Card Title
Card content goes here.
In this example, the .flex-row
class creates a horizontal layout, and the .card-img-left
class positions the image on the left side.
How can I create a horizontal card with a card header and footer?
You can create a horizontal card with a card header and footer by placing the header and footer elements within the card's body. Here's an example:
Card Title
Card content goes here.
Card Header
In this example, the card header and footer are placed inside the card's body, and the .flex-row
class creates the horizontal layout.
How can I create a horizontal card with an image on the right side?
To create a horizontal card with an image on the right side, you can use the .flex-row-reverse
class. Here's an example:
Card Title
Card content goes here.
In this example, the .flex-row-reverse
class creates a horizontal layout with the image on the right side.
How can I create a horizontal card with a custom width for the image and content?
To create a horizontal card with a custom width for the image and content, you can use utility classes for sizing. Here's an example:
Card Title
Card content goes here.
In this example, the .img-fluid
class ensures the image is responsive, and custom widths are applied to both the image and the content.
How can I align the content vertically within a horizontal card?
You can align the content vertically within a horizontal card using flex utilities. Here's an example:
Card Title
Card content goes here.
In this example, the .align-items-center
class vertically centers the content within the card.
How can I create a horizontal card with a button on the right side?
You can create a horizontal card with a button on the right side by using flex utilities for alignment. Here's an example:
Card Title
Card content goes here.
In this example, the .d-flex
, .flex-column
, and .justify-content-between
classes create a vertical layout with the button aligned to the bottom.
How can I create a responsive horizontal card grid?
To create a responsive horizontal card grid, you can use the grid system along with horizontal cards. Here's an example:
In this example, the col-md-6
class creates a responsive grid, and the flex-md-row
class makes the cards horizontal on medium screens and larger.
How can I create a horizontal card with different content alignment for each side?
You can create a horizontal card with different content alignment for each side by using utility classes. Here's an example:
Card Title
Card content goes here.
In this example, the align-self-center
class centers the image vertically, and the text-right
class aligns the content on the right side.
How can I create a horizontal card with different content on different screen sizes?
You can use Bootstrap's responsive classes to display different content on different screen sizes. Here's an example:
Card Title (Desktop)
Card Title (Mobile)
Card content goes here.
In this example, the .d-none
and .d-sm-block
classes hide the desktop title on small screens and show the mobile title, while the .d-block
and .d-sm-none
classes do the opposite.
How can I make the overlay content in a card image overlay vertically centered?
You can make the overlay content in a card image overlay vertically centered by using flex utilities for alignment. Here's an example:
In this example, the .d-flex
, .align-items-center
, and .justify-content-center
classes vertically and horizontally center the overlay content.
How can I make the image overlay text have a transparent background?
You can make the image overlay text have a transparent background by using custom CSS to adjust the background color and opacity. Here's an example:
In this example, the .transparent-overlay
class sets a semi-transparent white background for the overlay text.
Can I create a card image overlay with a video instead of an image?
Yes, you can create a card image overlay with a video by embedding the video within the card and applying the overlay to it. Here's an example:
In this example, the video is embedded using the .embed-responsive
class, and the overlay is added as usual.
How can I apply different text alignments to different parts of the card body?
You can apply different text alignments to different parts of the card body by using separate classes for each section. Here's an example:
Centered Title
Right-aligned content.
Left-aligned content.
In this example, each element inside the card body has its own text alignment class.
How can I center align the card header and footer text?
To center align the card header and footer text, you can use the .card-header
and .card-footer
classes along with the .text-center
class. Here's an example:
Centered Header
Card Title
Card content goes here.
In this example, the .text-center
class aligns the text inside both the header and footer to the center.
How can I center align the text vertically and horizontally within a card body?
You can center align the text both vertically and horizontally within a card body by using the .d-flex
, .align-items-center
, and .justify-content-center
classes. Here's an example:
Centered Text
Content is centered both vertically and horizontally.
In this example, the .d-flex
class creates a flex container, and both .align-items-center
and .justify-content-center
classes center the content both vertically and horizontally.
Conclusion
Bootstrap Cards epitomize a versatile and powerful component for web development, characterized by a flexible grid system and responsive
design. The grid-based approach enables the creation of visually appealing and responsive cards, ensuring a seamless and adaptable layout across devices.
From Panel Components and Information Tiles to Boxed Elements and Grid-Based Cards, the diverse range of card
types allows for creativity and customization. These cards serve as effective Informational Tiles and Grid-Style Display Panels, providing developers
with options to present content in visually appealing ways.
The concept of Responsive Card Design further enhances user experiences, providing a dynamic and user-friendly interface for presenting content. The inclusion of
Dynamic Information Panels, Customizable Boxed Layout, and Panelized Information Blocks empowers developers to craft engaging and
personalized displays, catering to specific design needs.
The versatility of Sectioned Card Elements introduces a higher level of customization, allowing developers to create header/footer content,
titled blocks, and linked information panels. The integration of Aligned Text Cards, Horizontal Content Blocks,
and Side-by-Side Card Design provides precise control over text positioning, facilitating the creation of stylish and flexible horizontal layouts.
Whether through Image-Focused Data Tiles or Link-Integrated Cards, Bootstrap Cards offer a comprehensive toolkit for presenting diverse content
types. The flexibility and adaptability of cards extend to customizable sections, dynamic headers and footers, and a range of styling
options, ensuring developers have the tools needed to create visually appealing and user-friendly web interfaces.
In essence, Bootstrap Cards emerge as an integral part of web design, offering a balance between flexibility and responsiveness, facilitating the creation of diverse and engaging content layouts.