Two Methods for Equalizing Row Height in Bootstrap 4: Utility Classes and Flexbox

Dec 01, 2025 · Programming · 13 views · 7.8

Keywords: Bootstrap 4 | row height equalization | Flexbox layout

Abstract: This article explores two core technical solutions for achieving equal row height in the Bootstrap 4 framework. By analyzing a common layout issue—how to make green and red rows within nested columns share the same height—it details the use of Bootstrap's built-in utility classes (e.g., h-50, h-100) and Flexbox-based approaches. The content includes code examples, underlying principles, and application scenarios, aiming to help developers address diverse layout needs flexibly.

In responsive web design, precise control over row height is a common challenge. Bootstrap 4, as a widely-used front-end framework, offers various tools to meet this demand. This article delves into two methods for equalizing row height through a specific case study.

Problem Context and Initial Code Analysis

The user's goal is to create a layout with a main column and a nested column. The nested column is divided into upper and lower sections: the upper part consists of two side-by-side green blocks, and the lower part is a red block. The key requirement is that the green and red rows must have equal heights, even with dynamic content. The initial code uses Bootstrap's grid system but lacks explicit height settings, resulting in inconsistent row heights.

Method 1: Using Bootstrap Utility Classes

Bootstrap 4 introduces a series of height utility classes, such as h-50 (height 50%) and h-100 (height 100%). These classes are based on CSS percentage units, allowing developers to quickly set element heights. In the solution, the nested row is first given the h-100 class to fill the parent container's height. Then, columns for the green blocks are assigned h-50 and pb-3 (padding-bottom), while the column for the red block uses h-50. Card elements within each block also employ h-100 to inherit the column's height.

<div class="row h-100">
    <div class="col-md-6 col-lg-6 B h-50 pb-3">
        <div class="card card-inverse card-success h-100"></div>
    </div>
    <div class="col-md-6 col-lg-6 B h-50 pb-3">
        <div class="card card-inverse bg-success h-100"></div>
    </div>
    <div class="col-md-12 h-50">
        <div class="card card-inverse bg-danger h-100"></div>
    </div>
</div>

This method is straightforward and suitable for scenarios with a known number of child elements. However, note that percentage heights rely on explicit height settings in parent elements; otherwise, they may not take effect.

Method 2: Combining with Flexbox Layout

For dynamic or unknown numbers of child elements, Flexbox offers a more flexible solution. Bootstrap 4 has built-in Flexbox support, and utility classes like d-flex (display: flex) and flex-column (flex-direction: column) can easily create vertical layouts. By adding d-flex flex-column h-100 to the nested row, it becomes a Flex container with a column direction. Child columns then use h-100 to fill the available height.

<div class="row d-flex flex-column h-100">
    <div class="col-md-6 col-lg-6 B h-100">
        <div class="card bg-success h-100"></div>
    </div>
    <div class="col-md-6 col-lg-6 B h-100">
        <div class="card bg-success h-100"></div>
    </div>
    <div class="col-md-12 h-100">
        <div class="card bg-danger h-100"></div>
    </div>
</div>

Flexbox automatically distributes space, ensuring child elements evenly fill the container without manual percentage calculations. This makes it more advantageous for handling responsive layouts or dynamic content.

Core Principles and Best Practices

Both methods depend on CSS height inheritance and Bootstrap's grid system. The utility class approach is based on the traditional box model, ideal for simple static layouts, while the Flexbox method leverages modern layout techniques for better scalability. In practice, choose based on project needs: utility classes are quicker for fixed layouts with known child counts, whereas Flexbox is superior for dynamic content or complex nesting. Ensuring parent containers have explicit heights (e.g., via chained h-100 settings) is crucial to avoid height collapse issues.

Conclusion and Extensions

Through this case study, we have demonstrated two effective ways to equalize row height in Bootstrap 4. Developers should grasp the core concepts of these tools, such as percentage heights and Flexbox properties, and practice with Bootstrap documentation. As new technologies like CSS Grid become more prevalent, layout options will diversify, but understanding foundational principles remains key to building robust interfaces.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.