Bootstrap Cards Example: HTML Code with Margins

Bootstrap, a popular CSS framework, simplifies responsive web development by providing pre-defined styles and components. One of these components is the card, which is versatile and widely used. Understanding how to manipulate margins in Bootstrap cards can help you create visually appealing and well-structured layouts. Let's dive into an HTML code example demonstrating this.

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Before we begin, ensure you have included the Bootstrap CSS and JavaScript libraries in your HTML file. You can use the following links to include them via CDN:

Bootstrap 4 Classes List Cheat Sheet
Bootstrap 4 Classes List Cheat Sheet

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>

Understanding Margins in Bootstrap Cards

In Bootstrap, margins are controlled using the CSS margin property. However, Bootstrap provides utility classes that make it easier to apply margins without writing custom CSS. These classes are based on the margin property and can be used to create space between cards or other elements.

an image of a computer screen with text
an image of a computer screen with text

Let's explore some of these utility classes and see how they can be applied to Bootstrap cards.

Using Margin Classes

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Bootstrap provides margin classes like `ms-auto` (margin-start: auto), `me-auto` (margin-end: auto), `mt-3` (margin-top: 1rem), `mb-3` (margin-bottom: 1rem), and so on. These classes can be applied to cards to create space around them.

Here's an example demonstrating the use of margin classes to create space between cards:

<div class="container">
  <div class="row">
    <div class="col-md-4 mb-3">
      <div class="card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
    <div class="col-md-4 mb-3">
      <div class="card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
    <div class="col-md-4">
      <div class="card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
  </div>
</div>

Creating Equal Columns with Auto Margins

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

To create equal columns with space between them, you can use the `mx-auto` class along with the `col` class. This will center the columns and create equal space on both sides.

Here's an example demonstrating this:

<div class="container">
  <div class="row">
    <div class="col mx-auto mb-3">
      <div class="card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
    <div class="col mx-auto mb-3">
      <div class="card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
  </div>
</div>

Customizing Margins with Custom CSS

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

While the utility classes provide a lot of flexibility, sometimes you might need to customize the margins further. In such cases, you can use custom CSS to override the Bootstrap styles.

Here's an example demonstrating how to customize the margin of a card using custom CSS:

css border
css border
two screens showing different types of text and numbers
two screens showing different types of text and numbers
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Bootstrap card design with hover effect
Bootstrap card design with hover effect
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
CSS Notes for Beginners (Easy + Quick Guide) 🎨
CSS Notes for Beginners (Easy + Quick Guide) 🎨
Tooltip Bootstrap 5 #shorts #coding #html
Tooltip Bootstrap 5 #shorts #coding #html
CSS Gradient Card Border Animation
CSS Gradient Card Border Animation
an image of a computer screen with many different types of screens and numbers on it
an image of a computer screen with many different types of screens and numbers on it
CSS border styles
CSS border styles
CSS Gradient Card Border Animation
CSS Gradient Card Border Animation
a paper with instructions on how to use the machine and padding in css
a paper with instructions on how to use the machine and padding in css
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Bootstrap 5.3 (2025) Cheat Sheet
Bootstrap 5.3 (2025) Cheat Sheet
Responsive CSS Cards Design with Hover Animation in HTML & CSS
Responsive CSS Cards Design with Hover Animation in HTML & CSS
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
CSS Cards
CSS Cards
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Complete CSS Notes for Web Development 💻
Complete CSS Notes for Web Development 💻

<style>
  .custom-card {
    margin: 2rem;
  }
</style>
<div class="container">
  <div class="row">
    <div class="col">
      <div class="card custom-card">
        <img src="..." class="card-img-top" alt="...">
        <div class="card-body">
          <h5 class="card-title">Card Title</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
        </div>
      </div>
    </div>
  </div>
</div>

In conclusion, understanding and utilizing margins in Bootstrap cards can greatly enhance the visual appeal and responsiveness of your web layouts. Whether you're using utility classes or custom CSS, margins provide a powerful tool for creating well-structured and visually appealing designs. So, go ahead and experiment with margins to create stunning Bootstrap card layouts!