Home >Web Front-end >CSS Tutorial >How to Avoid Empty Vertical Space Between Columns in Bootstrap 4?

How to Avoid Empty Vertical Space Between Columns in Bootstrap 4?

Susan Sarandon
Susan SarandonOriginal
2024-11-14 13:06:02210browse

How to Avoid Empty Vertical Space Between Columns in Bootstrap 4?

Empty Vertical Space Between Columns in Bootstrap 4

Bootstrap 4 introduces flexbox, resulting in columns sharing the same height as the tallest column. This creates an undesirable empty space between columns in certain scenarios.

Flexbox Alignment and Empty Space

Consider this:

  • Column A: Description (Tall)
  • Column B: Sidebar (Short)
  • Column C: Comments (Tall)

Flexbox aligns columns vertically, resulting in:

------------------  ---------
|                  ||         |
|                  ||         |
------------------ |         |
------------------ |         |
|                  ||         |
------------------  ---------

But we desire:

------------------  ---------
|                  |
 ------------------  
|                  |
 ------------------ |         |
|                  ||         |
 -------------------  ---------

Floats as a Workaround

Bootstrap 3 used floats, which allows columns to float and take up only the space they need. To achieve this in Bootstrap 4:

<div>

The d-lg-block class forces display:block on the row, allowing columns to float on large screens.

Nesting and Order Preservation

Nesting columns as follows may seem like a solution:

<div>

However, this compromises column order. In the above example, the sidebar will appear last on small screens due to the automatic layout of nested rows.

The above is the detailed content of How to Avoid Empty Vertical Space Between Columns in Bootstrap 4?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn