Bootstrap - Cards
Creating a Basic Card
Bootstrap cards are built with a basic structure that includes a header, body, and footer. To create a card, you start with a container element with the class card
. Inside the card, you can add these elements:
-
Card header:
- The card header is created using an element with the class
card-header
. - It usually contains a title or a brief description of the card's content.
- You can use heading elements (
<h1>
to<h6>
) or other appropriate elements within the card header.
- The card header is created using an element with the class
-
Card body:
- The card body is the main content area of the card and is created using an element with the class
card-body
. - It can contain various types of content, such as text, images, lists, or other HTML elements.
- The card body is where you place the primary information or content related to the card.
- The card body is the main content area of the card and is created using an element with the class
-
Card footer:
- The card footer is an optional section that appears at the bottom of the card.
- It is created using an element with the class
card-footer
. - The footer is often used to display additional information, links, or actions related to the card.
Example: Basic Bootstrap Card Structure
<div class="card">
<div class="card-header">
Header
</div>
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Card content goes here.</p>
</div>
<div class="card-footer">
Footer
</div>
</div>
The card consists of a header with the text "Header", a body with a title and some content, and a footer with the text "Footer".
You can further customize the content within each section of the card based on your specific needs. Bootstrap provides various classes and utilities to style and format the card's content, such as card-title
for the title, card-text
for paragraphs, and card-link
for links.
By using this basic card structure and adding your own content, you can create informative and visually appealing cards in your Bootstrap projects.
Card Content
Cards in Bootstrap give you a flexible container to display different types of content. Within the card body, you can include various types of content to share information. Let's look at some common types of content you can add to your Bootstrap cards.
Card Title
The card title is a main heading that describes the main subject or topic of the card. To create a card title, you can use the card-title
class on a heading element, such as <h5>
or <h6>
, within the card body. The card title is usually placed at the top of the card body to get the user's attention and give a clear overview of the card's content.
Card Text
The card text is the main text content of the card, where you can give detailed information, descriptions, or explanations related to the card's topic. To add card text, you can use the card-text
class on a <p>
element within the card body. You can include multiple paragraphs of text to share your message. The card-text
class applies styling to the text to make sure it is readable and matches the card's overall design.
Card Links
Bootstrap cards let you include links within the card content to give additional resources, call-to-action buttons, or navigation options. To create a card link, you can use the card-link
class on an <a>
element within the card body. Card links are typically placed at the bottom of the card body or in the card footer, depending on their purpose. By using the card-link
class, the link will inherit the styling to match the card's design and color scheme.
Card Images
Images can be a powerful addition to your Bootstrap cards, helping to visually support the card's content or provide visual context. To include an image in a card, you can use the <img>
element within the card body. Bootstrap provides several classes to control the positioning and sizing of the image within the card:
Class | Description |
---|---|
card-img-top |
Places the image at the top of the card, above the card body. |
card-img-bottom |
Places the image at the bottom of the card, below the card body. |
card-img |
Allows the image to span the full width of the card, without any additional spacing. |
By using these classes, you can easily add images into your cards and control their placement within the card layout.
Card Content Example
<div class="card">
<img src="image.jpg" class="card-img-top" alt="Card Image">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">This is the main content of the card, providing detailed information about the topic.</p>
<a href="#" class="card-link">Read More</a>
</div>
</div>
By using these different types of content within your Bootstrap cards, you can create informative and engaging card designs that communicate your message to your users.
Card Sizing
Bootstrap lets you size your cards to fit your layout and design needs. You can control the width and height of cards using Bootstrap's grid classes or by adding custom sizing styles. Here are the ways to size your cards.
Using Grid Classes
Bootstrap's grid system lets you make responsive layouts by dividing the page into rows and columns. You can use the grid classes to control the width of your cards within a row. By default, cards in Bootstrap take up the full width of their parent container. But, you can use the column classes (col-*
) to set the width of the card.
Example: Two Cards Side by Side
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Content for card 1 goes here.</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Content for card 2 goes here.</p>
</div>
</div>
</div>
</div>
By changing the column classes, you can create different card widths and layouts to fit your design needs.
Custom Sizing
Besides using grid classes, you can also add custom sizing to your cards using CSS styles. Bootstrap has utility classes for setting the width and height of elements, which you can use on your cards.
To set a width for a card, you can use the w-*
classes, where *
represents a percentage or a set value. For example, w-50
sets the width of the card to 50% of its parent container, while w-75
sets it to 75%.
You can also control the height of a card using the h-*
classes. For example, h-100
sets the height of the card to 100% of its parent container, while h-auto
lets the card adjust its height based on its content.
Example: Custom Sized Card
<div class="card w-50 h-100">
<div class="card-body">
<h5 class="card-title">Custom Sized Card</h5>
<p class="card-text">This card has a custom width of 50% and a height of 100%.</p>
</div>
</div>
You can combine custom sizing classes with grid classes to have even more control over the card's size and layout within a row or column.
Card Layouts
Bootstrap provides layout options for arranging cards in a responsive manner. You can display cards in a grid, stack them vertically, or create equal-height columns. Let's look at the different card layouts available.
Card Groups
Card groups let you render cards as a single, attached element with equal width and height. To create a card group, wrap a series of cards inside a container with the class card-group
. The cards within the group will have equal sizes and will be connected without gaps between them.
Card Group Example
<div class="card-group">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Content for card 1 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Content for card 2 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Content for card 3 goes here.</p>
</div>
</div>
</div>
Card groups are useful when you want to present related cards together as a unit, such as displaying a product comparison or showcasing a set of features.
Card Decks
Card decks are similar to card groups, but they provide equal-width cards with consistent spacing between them. To create a card deck, wrap a series of cards inside a container with the class card-deck
. The cards within the deck will have the same width and will be evenly spaced.
Card Deck Example
<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Content for card 1 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Content for card 2 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Content for card 3 goes here.</p>
</div>
</div>
</div>
Card decks are a good choice when you want to present a collection of cards with consistent spacing, such as showcasing a portfolio of projects or displaying a grid of articles.
Card Columns
Card columns provide a masonry-like layout where cards are organized into columns. The cards within each column will have equal width, but their height can vary based on the content. To create card columns, wrap a series of cards inside a container with the class card-columns
.
Card Columns Example
<div class="card-columns">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Content for card 1 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Content for card 2 goes here, which may be longer than other cards.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Content for card 3 goes here.</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 4</h5>
<p class="card-text">Content for card 4 goes here.</p>
</div>
</div>
</div>
Card columns are useful when you have a collection of cards with varying content lengths and want to present them in a masonry-style layout.
These card layout options let you arrange and display your cards in a way that suits your design and content needs. Bootstrap's card layouts are responsive and adapt to different screen sizes, making them a versatile choice for creating engaging and informative card-based interfaces.
Card Styles
Bootstrap provides styling options to change the look of your cards. You can change the background color, border color, and text color of your cards to match your design. Let's look at these styling options.
Background Colors
Bootstrap offers background colors that you can use on your cards. These background colors are based on Bootstrap's color classes and can show different meanings or highlight specific cards. To set the background color of a card, add one of the following classes to the card container:
Class | Description |
---|---|
bg-primary |
Applies a blue background color to the card. |
bg-secondary |
Applies a gray background color to the card. |
bg-success |
Applies a green background color to the card. |
bg-danger |
Applies a red background color to the card. |
bg-warning |
Applies a yellow background color to the card. |
bg-info |
Applies a teal background color to the card. |
bg-light |
Applies a light gray background color to the card. |
bg-dark |
Applies a dark gray background color to the card. |
bg-white |
Applies a white background color to the card. |
Background color example
<div class="card bg-primary">
<div class="card-body">
<h5 class="card-title">Primary Card</h5>
<p class="card-text">This card has a blue background color.</p>
</div>
</div>
Border Colors
Bootstrap lets you change the border color of your cards. By default, cards have a border color that matches the default gray color. However, you can change the border color to any of Bootstrap's colors using the following classes:
Class | Description |
---|---|
border-primary |
Applies a blue border color to the card. |
border-secondary |
Applies a gray border color to the card. |
border-success |
Applies a green border color to the card. |
border-danger |
Applies a red border color to the card. |
border-warning |
Applies a yellow border color to the card. |
border-info |
Applies a teal border color to the card. |
border-light |
Applies a light gray border color to the card. |
border-dark |
Applies a dark gray border color to the card. |
border-white |
Applies a white border color to the card. |
To set the border color, add the class to the card container.
Border color example
<div class="card border-success">
<div class="card-body">
<h5 class="card-title">Success Card</h5>
<p class="card-text">This card has a green border color.</p>
</div>
</div>
Text Colors
Bootstrap also provides classes to change the text color within your cards. You can use these classes to make the text stand out or match the color scheme of your card. The available text color classes are:
Class | Description |
---|---|
text-primary |
Applies a blue text color. |
text-secondary |
Applies a gray text color. |
text-success |
Applies a green text color. |
text-danger |
Applies a red text color. |
text-warning |
Applies a yellow text color. |
text-info |
Applies a teal text color. |
text-light |
Applies a light gray text color. |
text-dark |
Applies a dark gray text color. |
text-white |
Applies a white text color. |
To set the text color, add the class to the text elements within the card, such as the card title or card text.
Text color example
<div class="card">
<div class="card-body">
<h5 class="card-title text-primary">Primary Title</h5>
<p class="card-text text-secondary">This card has a blue title and gray text.</p>
</div>
</div>
You can mix background colors, border colors, and text colors to create custom cards that match your design needs. Bootstrap's color classes make it easy to apply styles to your cards without writing extra CSS.
Card Images
Bootstrap cards provide options for including images and improving the look of your cards. You can add images to your cards in different ways, such as image overlays and image caps. Let's look at these options.
Image Overlays
Image overlays let you put content on top of an image within a card. This method is useful when you want to show text, buttons, or other elements over an image background. To create an image overlay, you can use the card-img-overlay
class with the card-img
class.
Image Overlay Example
<div class="card">
<img src="image.jpg" class="card-img" alt="Card Image">
<div class="card-img-overlay">
<h5 class="card-title">Card Title</h5>
<p class="card-text">This is an example of an image overlay, where the card content is placed on top of the image.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
The card-img
class is used on the <img>
element to make it cover the full width and height of the card. The card-img-overlay
class is used on a <div>
element that holds the card content, such as the title, text, and buttons. The overlay content is placed on top of the image, allowing you to create engaging and interactive card designs.
When using image overlays, think about the readability of the content placed on top of the image. You may need to change the text color or add a semi-transparent background to make sure the content is easy to read.
Image Caps
Image caps refer to putting images at the top or bottom of a card, outside the card body. Bootstrap provides two classes for adding image caps: card-img-top
and card-img-bottom
.
To add an image cap to the top of a card, use the card-img-top
class on an <img>
element placed above the card body.
Top Image Cap Example
<div class="card">
<img src="image.jpg" class="card-img-top" alt="Card Image">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">This is an example of an image cap at the top of the card.</p>
</div>
</div>
Similarly, to add an image cap to the bottom of a card, use the card-img-bottom
class on an <img>
element placed below the card body.
Bottom Image Cap Example
<div class="card">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">This is an example of an image cap at the bottom of the card.</p>
</div>
<img src="image.jpg" class="card-img-bottom" alt="Card Image">
</div>
Image caps are a simple way to add visual interest to your cards without overlaying content on top of the image. They are commonly used for showing product images, user avatars, or any other relevant visual content.
When using image caps, make sure to choose appropriate image sizes and aspect ratios that fit well within the card layout. You can use Bootstrap's responsive image classes, such as img-fluid
, to make sure the images scale properly across different screen sizes.
Customizing Cards
Bootstrap lets you customize cards using custom CSS and utility classes. Let's look at these options.
Custom CSS
You can customize your Bootstrap cards by using custom CSS styles. You can target specific card elements using CSS selectors and override the default styles from Bootstrap. This gives you full control over how your cards look.
To use custom CSS on your cards, you can create a separate CSS file or use the <style>
tag in your HTML file. Then, use CSS selectors to target the card elements and apply your custom styles.
Example: Custom CSS
<style>
.custom-card {
background-color: #f8f9fa;
border: none;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.custom-card .card-title {
color: #007bff;
font-size: 20px;
font-weight: bold;
}
.custom-card .card-text {
color: #6c757d;
font-size: 16px;
}
</style>
<div class="card custom-card">
<div class="card-body">
<h5 class="card-title">Custom Card</h5>
<p class="card-text">This card has custom styles applied using CSS.</p>
</div>
</div>
The .custom-card
class is used to apply custom styles to the card. The CSS rules change the background color, remove the border, apply a border radius, and add a box shadow to the card. The .card-title
and .card-text
elements in the card are styled with custom colors and font sizes.
By using custom CSS, you can change any part of the card's appearance, such as colors, typography, spacing, and more.
Utility Classes
Bootstrap has utility classes that you can use to quickly change the appearance and behavior of your cards without writing custom CSS. These utility classes cover many properties, such as colors, spacing, text alignment, and more.
Here are some common utility classes for customizing cards:
Utility Class | Description |
---|---|
text-center |
Centers the text in the card. |
text-right |
Aligns the text to the right in the card. |
bg-primary , bg-success , etc. |
Applies background colors to the card. |
text-white , text-muted , etc. |
Changes the text color in the card. |
p-3 , p-4 , etc. |
Adds padding to the card. |
m-3 , m-4 , etc. |
Adds margin to the card. |
rounded |
Applies rounded corners to the card. |
shadow |
Adds a box shadow to the card. |
You can apply these utility classes directly to the card elements or create custom classes that combine multiple utility classes.
Example: Utility Classes
<div class="card text-center bg-primary text-white p-4 rounded shadow">
<div class="card-body">
<h5 class="card-title">Utility Classes Card</h5>
<p class="card-text">This card is customized using Bootstrap utility classes.</p>
</div>
</div>
The utility classes text-center
, bg-primary
, text-white
, p-4
, rounded
, and shadow
are applied directly to the card element. These classes center the text, set a primary background color, change the text color to white, add padding, apply rounded corners, and add a box shadow to the card.
By using Bootstrap's utility classes, you can quickly customize your cards without writing a lot of custom CSS. But for more complex or specific customizations, using custom CSS gives you more control and flexibility.
Choose the customization approach that works best for your project's needs and maintainability. Bootstrap gives you the tools and flexibility to create visually appealing and customized cards.
Card Responsiveness
Bootstrap cards are responsive and adapt to different screen sizes and device widths. But you may need to make changes to make sure your cards look and work well on various devices. Let's look at some ways to handle card responsiveness.
Adjusting Card Layouts
Bootstrap's grid system lets you create responsive card layouts by using the right column classes. By default, cards take up the full width of their parent container, but you can use column classes to control their width and arrangement.
To create a responsive card layout, you can wrap your cards inside grid columns and specify the column width for different screen sizes. Bootstrap has responsive column classes like col-sm-*
, col-md-*
, col-lg-*
, and col-xl-*
, where *
represents the number of columns (1-12) the card should occupy at each breakpoint.
Example: Adjusting Card Layouts
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">This card will occupy different column widths based on the screen size.</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">This card will occupy different column widths based on the screen size.</p>
</div>
</div>
</div>
<!-- More cards... -->
</div>
The cards are wrapped inside <div>
elements with the classes col-sm-6
, col-md-4
, and col-lg-3
. This means that on small screens (sm
), the cards will occupy 6 columns (50% width), on medium screens (md
), they will occupy 4 columns (33.33% width), and on large screens (lg
), they will occupy 3 columns (25% width).
By adjusting the column classes, you can create responsive card layouts that adapt to different screen sizes, making sure they look good and are easy to use.
Collapsing Card Columns
Sometimes, you may want to collapse card columns on smaller screens to make them easier to read and prevent overflow. Bootstrap has the card-columns
class, which creates a masonry-like layout for cards. But on smaller screens, the columns may become too narrow, making the content cramped.
To fix this, you can use responsive utility classes to collapse the card columns on specific screen sizes. For example, you can use the collapse
class with responsive display classes like d-none d-md-block
to hide the columns on small screens and show them on larger screens.
Example: Collapsing Card Columns
<div class="card-columns">
<div class="card d-none d-md-block">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">This card will be hidden on small screens and visible on medium and larger screens.</p>
</div>
</div>
<div class="card d-none d-md-block">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">This card will be hidden on small screens and visible on medium and larger screens.</p>
</div>
</div>
<!-- More cards... -->
</div>
The cards are wrapped inside a <div>
element with the card-columns
class. Each card also has the classes d-none
and d-md-block
, which hide the cards on small screens (d-none
) and show them on medium and larger screens (d-md-block
).
By collapsing card columns on smaller screens, you can prevent content overflow and make the user experience better. Users can still access the card content by scrolling or using other navigation methods that work well on small screens.
Test your card layouts on various devices and screen sizes to make sure they are responsive and provide a good user experience. Bootstrap's responsive classes and utilities give you the tools to create flexible and adaptive card designs that work well across different devices.
Card JavaScript Behavior
Bootstrap has JavaScript plugins that you can use to add interactive behavior to your cards. Two common examples are collapsing cards and toggling cards. Let's look at how to implement these behaviors.
Collapsing Cards
Collapsing cards let you toggle the visibility of card content by clicking on a trigger element, such as a button or a link. This is useful when you want to show or hide more information in a card.
To create a collapsible card, you need to use the collapse
class on the card content and provide a trigger element with the data-toggle="collapse"
attribute and a target selector pointing to the card content.
Example: Collapsing Cards
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="true" aria-controls="collapseExample">
Collapsible Card
</button>
</h5>
</div>
<div id="collapseExample" class="collapse show" aria-labelledby="headingExample">
<div class="card-body">
This is the card content that can be collapsed and expanded.
</div>
</div>
</div>
The card header contains a button that serves as the trigger for collapsing the card content. The button has the data-toggle="collapse"
attribute to indicate that it controls a collapsible element. The data-target="#collapseExample"
attribute specifies the target element to be collapsed, which is the <div>
with the ID collapseExample
.
Toggling Cards
Toggling cards is similar to collapsing cards but lets you toggle between different card elements or styles. You can use this behavior to switch between different views or states of a card.
To create a togglable card, you can use the data-toggle="card"
attribute on a trigger element, such as a button or a link, and specify the target card element using the data-target
attribute.
Example: Toggling Cards
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="card" data-target="#cardExample">
Toggle Card
</button>
</h5>
</div>
<div id="cardExample" class="card-body">
<p>This is the initial content of the card.</p>
<p class="d-none">This is the alternative content of the card.</p>
</div>
</div>
The card header contains a button that serves as the trigger for toggling the card content. The button has the data-toggle="card"
attribute to indicate that it toggles the card element. The data-target="#cardExample"
attribute specifies the target card element to be toggled, which is the <div>
with the ID cardExample
. Inside the card body, there are two <p>
elements. The first one is initially visible, while the second one has the d-none
class to hide it by default.
To use these JavaScript behaviors, make sure to include the Bootstrap JavaScript file in your project and initialize the plugins by calling the right methods or using data attributes.
Bootstrap's JavaScript plugins provide a simple and interactive way to enhance your cards and create engaging user experiences. Try collapsing and toggling cards to add dynamic behavior to your card designs.
Card Responsiveness
Bootstrap cards are responsive and adapt to different screen sizes and device widths. However, you may need to make adjustments to ensure your cards look and function well across various devices. Let's explore some techniques to handle card responsiveness.
Adjusting Card Layouts
Bootstrap's grid system allows you to create responsive card layouts by using the appropriate column classes. By default, cards span the full width of their parent container, but you can use column classes to control their width and arrangement.
To create a responsive card layout, you can wrap your cards inside grid columns and specify the column width for different screen sizes. Bootstrap provides responsive column classes such as col-sm-*
, col-md-*
, col-lg-*
, and col-xl-*
, where *
represents the number of columns (1-12) the card should occupy at each breakpoint.
Example: Responsive Card Layout
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">This card will occupy different column widths based on the screen size.</p>
</div>
</div>
</div>
<div class="col-sm-6 col-md-4 col-lg-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">This card will occupy different column widths based on the screen size.</p>
</div>
</div>
</div>
<!-- More cards... -->
</div>
The cards are wrapped inside <div>
elements with the classes col-sm-6
, col-md-4
, and col-lg-3
. This means that on small screens (sm
), the cards will occupy 6 columns (50% width), on medium screens (md
), they will occupy 4 columns (33.33% width), and on large screens (lg
), they will occupy 3 columns (25% width).
By adjusting the column classes, you can create responsive card layouts that adapt to different screen sizes, ensuring they look and function appropriately.
Collapsing Card Columns
In some cases, you may want to collapse card columns on smaller screens to improve readability and prevent content overflow. Bootstrap provides the card-columns
class, which creates a masonry-like layout for cards. However, on smaller screens, the columns may become too narrow, causing the content to appear cramped.
To address this, you can use responsive utility classes to collapse the card columns on specific screen sizes. You can use the collapse
class in combination with responsive display classes like d-none d-md-block
to hide the columns on small screens and display them on larger screens.
Example: Collapsing Card Columns
<div class="card-columns">
<div class="card d-none d-md-block">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">This card will be hidden on small screens and visible on medium and larger screens.</p>
</div>
</div>
<div class="card d-none d-md-block">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">This card will be hidden on small screens and visible on medium and larger screens.</p>
</div>
</div>
<!-- More cards... -->
</div>
The cards are wrapped inside a <div>
element with the card-columns
class. Each card also has the classes d-none
and d-md-block
, which hide the cards on small screens (d-none
) and display them on medium and larger screens (d-md-block
).
By collapsing card columns on smaller screens, you can prevent content overflow and improve the user experience. Users can still access the card content by scrolling or using alternative navigation methods suitable for small screens.
It's important to test your card layouts on various devices and screen sizes to ensure they are responsive and provide a good user experience. Bootstrap's responsive classes and utilities give you the flexibility to create adaptive card designs that work well across different devices.
Card JavaScript Behavior
Bootstrap provides JavaScript plugins that you can use to add interactive behavior to your cards. Two common examples are collapsing cards and toggling cards. Let's see how to implement these behaviors.
Collapsing Cards
Collapsing cards allows you to toggle the visibility of card content by clicking on a trigger element, such as a button or a link. This is useful when you want to show or hide additional information within a card.
To create a collapsible card, you need to use the collapse
class on the card content and provide a trigger element with the data-toggle="collapse"
attribute and a target selector pointing to the card content.
Collapsing Cards Example
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="true" aria-controls="collapseExample">
Collapsible Card
</button>
</h5>
</div>
<div id="collapseExample" class="collapse show" aria-labelledby="headingExample">
<div class="card-body">
This is the card content that can be collapsed and expanded.
</div>
</div>
</div>
The card header contains a button that serves as the trigger for collapsing the card content. The button has the data-toggle="collapse"
attribute to indicate that it controls a collapsible element. The data-target="#collapseExample"
attribute specifies the target element to be collapsed, which is the <div>
with the ID collapseExample
.
When the button is clicked, the card content will toggle between being collapsed and expanded.
Toggling Cards
Toggling cards is similar to collapsing cards but allows you to toggle between different card elements or styles. You can use this behavior to switch between different views or states of a card.
To create a togglable card, you can use the data-toggle="card"
attribute on a trigger element, such as a button or a link, and specify the target card element using the data-target
attribute.
Toggling Cards Example
<div class="card">
<div class="card-header">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="card" data-target="#cardExample">
Toggle Card
</button>
</h5>
</div>
<div id="cardExample" class="card-body">
<p>This is the initial content of the card.</p>
<p class="d-none">This is the alternative content of the card.</p>
</div>
</div>
The card header contains a button that serves as the trigger for toggling the card content. The button has the data-toggle="card"
attribute to indicate that it toggles the card element. The data-target="#cardExample"
attribute specifies the target card element to be toggled, which is the <div>
with the ID cardExample
.
Inside the card body, there are two <p>
elements. The first one is initially visible, while the second one has the d-none
class to hide it by default. When the button is clicked, the card content will toggle between the two paragraphs, showing one and hiding the other.
To use these JavaScript behaviors, make sure to include the Bootstrap JavaScript file in your project and initialize the plugins by calling the appropriate methods or using data attributes.
Bootstrap's JavaScript plugins provide a simple and interactive way to enhance your cards and create engaging user experiences. Experiment with collapsing and toggling cards to add dynamic behavior to your card designs.