SnippetUI

Layout Masonry

Bố cục lưới không đều đẹp mắt theo phong cách Pinterest sử dụng Cột CSS

HTMLCSS

Sử dụng CSS column-countbreak-inside: avoid để tạo các bố cục lưới vô định hình đẹp mắt (bố cục khối xây) như Pinterest. Nó có thể được triển khai chỉ bằng CSS mà không cần sử dụng JavaScript.

Tính năng

  • Được triển khai chỉ bằng CSS: Hiệu suất tuyệt vời mà không cần tính toán JavaScript.
  • Hỗ trợ đáp ứng: Số lượng cột tự động thay đổi tùy theo độ rộng màn hình (3 cột → 2 cột → 1 cột).
  • Ngăn chặn hiện tượng gãy phần tử: break-inside: avoid ngăn nội dung bị đứt gãy một cách bất thường giữa các cột.

Cách sử dụng

Chỉ cần áp dụng lớp này cho vùng chứa cha mẹ HTML và sắp xếp các phần tử con để tạo ra bố cục khối xây đẹp mắt.

Nature

Forest Serenity

A beautiful walk through the foggy woods.

City

Urban Exploration

Discovering hidden alleys.

Mountains

Mountain Peaks

Snowy tops against a clear blue sky.

Ocean

Ocean Waves

Crashing waves on the rocky shore.

Desert

Desert Dunes

Endless sand under the scorching sun.

Bridge

Suspension Bridge

Connecting two sides of the canyon.

<div class="masonry-layout">
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1472214103451-9374bd1c798e?w=800&auto=format&fit=crop&q=60" alt="Nature" />
    <div class="item-content">
      <h3>Forest Serenity</h3>
      <p>A beautiful walk through the foggy woods.</p>
    </div>
  </div>
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1449844908441-8829872d2607?w=800&auto=format&fit=crop&q=60" alt="City" />
    <div class="item-content">
      <h3>Urban Exploration</h3>
      <p>Discovering hidden alleys.</p>
    </div>
  </div>
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1454496522488-7a8e488e8606?w=600&h=800&auto=format&fit=crop&q=60" alt="Mountains" />
    <div class="item-content">
      <h3>Mountain Peaks</h3>
      <p>Snowy tops against a clear blue sky.</p>
    </div>
  </div>
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1439405326854-014607f694d7?w=800&h=500&auto=format&fit=crop&q=60" alt="Ocean" />
    <div class="item-content">
      <h3>Ocean Waves</h3>
      <p>Crashing waves on the rocky shore.</p>
    </div>
  </div>
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1473580044384-7ba9967e16a0?w=800&auto=format&fit=crop&q=60" alt="Desert" />
    <div class="item-content">
      <h3>Desert Dunes</h3>
      <p>Endless sand under the scorching sun.</p>
    </div>
  </div>
  <div class="masonry-item">
    <img src="https://images.unsplash.com/photo-1513635269975-59663e0ac1ad?w=600&h=700&auto=format&fit=crop&q=60" alt="Bridge" />
    <div class="item-content">
      <h3>Suspension Bridge</h3>
      <p>Connecting two sides of the canyon.</p>
    </div>
  </div>
</div>
.masonry-layout {
  /* Chỉ định số lượng cột */
  column-count: 3;
  /* Xác định khoảng cách giữa các cột */
  column-gap: 1.5rem;
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.masonry-item {
  /* Ngăn chặn việc chia các mục thành các cột */
  break-inside: avoid;
  /* Dự phòng cho các trình duyệt cũ */
  page-break-inside: avoid;
  
  display: inline-block;
  width: 100%;
  margin-bottom: 1.5rem;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.masonry-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.15);
}

.masonry-item img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
  margin: 0 !important;
}

.item-content {
  padding: 1.25rem;
}

.item-content h3 {
  margin: 0 0 0.5rem 0 !important;
  font-size: 1.25rem;
  color: #333;
}

.item-content p {
  margin: 0 !important;
  color: #666;
  font-size: 0.95rem;
  line-height: 1.5;
}

/* Responsive design */
@media (max-width: 900px) {
  .masonry-layout {
    column-count: 2;
  }
}

@media (max-width: 600px) {
  .masonry-layout {
    column-count: 1;
  }
}