You've successfully navigated through creating buttons, forms, modals, and accordions with Bootstrap. Now, let's expand your toolkit even further by exploring Bootstrap Cards. Cards are versatile and elegant components used to display content in a structured format, making your web pages more visually appealing and easier to navigate.
In this lesson, you'll master the creation and customization of Bootstrap Cards
. We'll focus on understanding how to structure a card, add images, text, and buttons to create a cohesive and attractive element. By the end of this lesson, you'll be able to design a card similar to this:
HTML, XML1<div class="card" style="width: 18rem;"> 2 <img src="https://codesignal-assets.s3.amazonaws.com/uploads/1714856323584/tiandl_adorable_corgi_puppy_natural_light_film_camera_shot_dayl_5f6f5500-4ed2-46f3-947d-c7717270d757.png" class="card-img-top" alt="Corgi puppy lounging in sunlight"> 3 <div class="card-body"> 4 <h5 class="card-title">Sunbathing Puppy</h5> 5 <p class="card-text">Enjoying the warmth and light, a corgi finds comfort in the rays of the sun.</p> 6 <a href="#" class="btn btn-primary">Learn More</a> 7 </div> 8</div>
Here are the key parts of the above code snippet:
- Card Container (
<div class="card">
): This is the Bootstrap class used to create the main container for the card. Thestyle="width: 18rem;"
sets the width of the card. - Card Image (
<img class="card-img-top">
): This Bootstrap class is used to display an image at the top of the card. Thealt
attribute provides alternative text for the image. - Card Body (
<div class="card-body">
): This section uses the Bootstrap classcard-body
to contain the main content of the card, including:- Card Title (
<h5 class="card-title">
): This Bootstrap class is used to display the title of the card. - Card Text (
<p class="card-text">
): This Bootstrap class provides additional information or description. - Card Button (
<a class="btn btn-primary">
): This Bootstrap class adds a button to the card, typically used to link to more information.
- Card Title (
Bootstrap Cards offer a versatile range of content options, making it easy to create visually appealing and functional cards. Let's explore some examples:
-
Images:
HTML, XML1<img src="image-url.jpg" class="card-img-top" alt="Description">
This uses the
card-img-top
class to place an image at the top of the card, providing a visually engaging start. -
Text and Titles:
HTML, XML1<h5 class="card-title">Card Title</h5> 2<p class="card-text">Quick example text to build on the card title.</p>
Use the
card-title
andcard-text
classes to add a catchy title and descriptive text, giving context and information to your card. -
Links:
HTML, XML1<a href="#" class="card-link">Card link</a> 2<a href="#" class="card-link">Another link</a>
The
card-link
class is used to include links for additional navigation or resources, making your card interactive. -
List Groups:
HTML, XML1<ul class="list-group list-group-flush"> 2 <li class="list-group-item">Cras justo odio</li> 3 <li class="list-group-item">Dapibus ac facilisis in</li> 4 <li class="list-group-item">Vestibulum at eros</li> 5</ul>
Add a list group using the
list-group
andlist-group-flush
classes to organize information neatly within the card, perfect for lists and itemized content.
Customizing the size of Bootstrap Cards allows you to tailor them to fit various layouts and design needs. Here are some ways to do this:
-
Fixed Width:
HTML, XML1<div class="card" style="width: 18rem;"> 2 <!-- Sets a fixed width of 18rem (approximately 288px) for the card --> 3</div>
Specify a fixed width to create uniformity and ensure your cards maintain a consistent size.
-
Using Utility Classes for Width:
HTML, XML1<div class="card w-50"> 2 <!-- Sets the width to 50% of its parent container using Bootstrap utility class w-50 --> 3</div>
Besides
w-50
, other width utility classes includew-25
for 25%,w-75
for 75%,w-100
for 100%, andw-auto
for automatic width based on content. These classes provide quick and easy ways to adjust element widths responsively. -
Using Grid for Width:
HTML, XML1<div class="col-sm-6"> 2 <!-- Sets the width to 50% of the parent container using Bootstrap grid system class col-sm-6 --> 3 <div class="card"></div> 4</div>
Utilize the grid system to control the width dynamically, ensuring your cards fit seamlessly within your layout. Other grid column classes such as
col-sm-4
for 33.33% andcol-sm-12
for 100% are also available for responsive design.
These examples highlight the adaptability of Bootstrap Cards, enabling you to create cards that not only look great but also function well within your design.
Understanding how to use Bootstrap Cards
is essential for modern web development. Cards provide a flexible and extensible content container, combining various elements like images, text, and links in a uniform style. They are perfect for organizing information, whether you're showcasing products, blog posts, or user profiles.
By mastering cards, you'll be able to create more interactive and visually engaging web pages. This not only enhances user experience but also makes your content more accessible and attractive.
Are you excited to see the impact of Bootstrap Cards
on your web pages? Let's dive into the practice section and get started!