Skip to content

Bootstrap Grid Medium

Bootstrap Grid Example: Targeting Medium Devices

Section titled “Bootstrap Grid Example: Targeting Medium Devices”

Bootstrap’s grid system allows you to define different column layouts for different screen sizes. This chapter focuses on how .col-md-* classes apply to medium devices and how they interact with classes for smaller or larger devices.

In Bootstrap 5, medium devices (md) are defined as having a screen width of 768 pixels or greater (≥768px).

Let’s consider an example where we want a 25%/75% split on small devices (.col-sm-*) and a 50%/50% split on medium devices (.col-md-*) and up.

For small devices (≥576px), we might use .col-sm-3 (25%) and .col-sm-9 (75%). For medium devices (≥768px) and larger, we want .col-md-6 (50%) and .col-md-6 (50%).

The combined classes on our columns would be:

<div class="col-sm-3 col-md-6">....</div>
<div class="col-sm-9 col-md-6">....</div>

Bootstrap’s mobile-first approach means that grid classes apply to their specified breakpoint and wider, unless overridden by a larger breakpoint class. So:

  • On extra-small screens (<576px), columns stack (default behavior).
  • On small screens (≥576px but <768px), .col-sm-3 and .col-sm-9 apply, resulting in a 25%/75% split.
  • On medium screens (≥768px) and larger (unless overridden by .col-lg-*, .col-xl-*, etc.), .col-md-6 and .col-md-6 apply, resulting in a 50%/50% split.

The following example demonstrates this behavior:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Grid Medium Example</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* Custom styles for visualization */
.content-area {
padding: 1rem;
margin-bottom: 1rem; /* For spacing if rows stack */
}
</style>
</head>
<body>
<div class="container-fluid mt-3">
<h1>Responsive Grid Demo</h1>
<p>Resize your browser window to see the layout change at different breakpoints.</p>
<div class="row">
<div class="col-sm-3 col-md-6">
<div class="content-area bg-info-subtle border">
<h5>Column 1 (sm-3, md-6)</h5>
<p>On small screens (sm), this column takes up 3 of 12 units (25%).</p>
<p>On medium screens (md) and up, it takes 6 of 12 units (50%).</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
<div class="col-sm-9 col-md-6">
<div class="content-area bg-warning-subtle border">
<h5>Column 2 (sm-9, md-6)</h5>
<p>On small screens (sm), this column takes up 9 of 12 units (75%).</p>
<p>On medium screens (md) and up, it takes 6 of 12 units (50%).</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem.</p>
</div>
</div>
</div>
</div>
<!-- Bootstrap JS Bundle (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<!--
Practical Application: This is useful for sidebars that become wider or main content areas that adjust their prominence on different screen sizes.
Common Obstacle: Understanding how breakpoint classes override each other. Remember: `xs` (default/no prefix) < `sm` < `md` < `lg` < `xl` < `xxl`. A larger breakpoint class will override a smaller one if both are present.
Resource: Bootstrap Grid Breakpoints: https://getbootstrap.com/docs/5.3/layout/breakpoints/
-->
Note: Ensure that the sum of column units for a specific breakpoint within a single .row makes sense for your intended layout, typically adding up to 12 for a full row. Bootstrap is flexible, but this helps in planning.

The next chapter might explore how to add different splits for large (.col-lg-*) or extra-large (.col-xl-*) devices, or introduce features like column ordering and offsetting.