Home > Web Front-end > CSS Tutorial > How to Achieve a Masonry Layout Using Bootstrap 4's Flexbox Grid?

How to Achieve a Masonry Layout Using Bootstrap 4's Flexbox Grid?

Linda Hamilton
Release: 2024-12-19 13:13:09
Original
599 people have browsed it

How to Achieve a Masonry Layout Using Bootstrap 4's Flexbox Grid?

How to Create a Masonry Layout in Bootstrap 4 with Flexbox Grid

In Bootstrap 4, you can achieve a masonry column layout utilizing the flexbox grid by leveraging the Card columns feature.

Solution:

Wrap your card elements in a .card-columns container, as seen below:

<div class="container">
  <div class="card-columns">
    <div class="card">...</div>
    <div class="card">...</div>
    <div class="card">...</div>
  </div>
</div>
Copy after login

Explanation:

  • The .card-columns class uses CSS columns to arrange your cards from top to bottom and left to right.
  • The cards are displayed inline-block to prevent them from breaking across columns.
  • Because column-break-inside: avoid is not yet a bulletproof solution, you may still experience inconsistent column heights.

Example:

Consider the following HTML:

<div class="container">
  <div class="card-columns">
    <div class="card">
      <img src="card-image-1.jpg" alt="Card image">
      <div class="card-body">
        <h5 class="card-title">Card title</h5>
        <p class="card-text">...</p>
      </div>
    </div>
    <div class="card">
      <img src="card-image-2.jpg" alt="Card image">
      <div class="card-body">
        <h5 class="card-title">Another card title</h5>
        <p class="card-text">...</p>
      </div>
    </div>
    <div class="card">
      <img src="card-image-3.jpg" alt="Card image">
      <div class="card-body">
        <h5 class="card-title">Card with long title</h5>
        <p class="card-text">...</p>
      </div>
    </div>
    <div class="card">
      <img src="card-image-4.jpg" alt="Card image">
      <div class="card-body">
        <h5 class="card-title">Another card with even more text</h5>
        <p class="card-text">...</p>
      </div>
    </div>
  </div>
</div>
Copy after login

This will result in a masonry layout, with the cards arranging themselves based on their content height, creating a visually appealing and dynamic grid.

The above is the detailed content of How to Achieve a Masonry Layout Using Bootstrap 4's Flexbox Grid?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template