You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Semantic UI中实现错落网格(Staggered Grid)

Implementing Staggered (Masonry) Grid with Semantic UI

Hey there! You’re right—Semantic UI doesn’t have a built-in staggered (masonry-style) grid component out of the box, but there are several practical ways to pull this off while keeping your Semantic UI workflow consistent. Here are my go-to approaches:

1. CSS Grid + Semantic UI Base Classes

CSS Grid’s grid-auto-flow: dense property lets you create a staggered layout without extra libraries, and you can pair it with Semantic’s existing container/row classes for consistency.

First, create a custom class for your staggered grid (instead of inline styles for maintainability):

.staggered-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-gap: 1rem; /* Matches Semantic's default spacing */
  grid-auto-flow: dense;
}

/* Optional: Add responsive column counts */
@media (max-width: 768px) {
  .staggered-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}

Then use it with Semantic UI cards inside a container/row:

<div class="ui container">
  <div class="ui row staggered-grid">
    <div class="ui card" style="grid-row: span 2;">
      <div class="image">
        <img src="tall-image.jpg">
      </div>
      <div class="content">
        <div class="header">Tall Card</div>
      </div>
    </div>
    <div class="ui card">
      <div class="image">
        <img src="short-image.jpg">
      </div>
      <div class="content">
        <div class="header">Short Card</div>
      </div>
    </div>
    <!-- Add more cards with varying `grid-row: span X` values -->
  </div>
</div>

2. Masonry Library + Semantic UI Cards

If you need more robust behavior (like handling dynamic content loading or better cross-browser support for older browsers), the Masonry library is a tried-and-true option. It plays nicely with Semantic’s card components:

First, include Masonry (you can download it or use a CDN), then set up your grid:

<div class="ui container">
  <div class="ui row masonry-grid">
    <div class="ui card">
      <!-- Card content -->
    </div>
    <div class="ui card">
      <!-- Card content -->
    </div>
    <!-- More cards -->
  </div>
</div>

<script>
  // Initialize Masonry once the DOM is ready
  document.addEventListener('DOMContentLoaded', function() {
    const grid = document.querySelector('.masonry-grid');
    new Masonry(grid, {
      itemSelector: '.card',
      gutter: 16, /* Align with Semantic's default spacing */
      columnWidth: 250,
      fitWidth: true /* Makes the grid center-align */
    });
  });
</script>

You can combine this with Semantic’s responsive classes to adjust column widths on different screen sizes.

3. Pure CSS Column Layout (Simplest Option)

For a no-JS solution that works for static content, use CSS columns. Note: This will arrange items top-to-bottom first, then left-to-right (unlike Masonry/Grid which go left-to-right row-by-row), but it’s super straightforward:

.staggered-columns {
  columns: 3;
  column-gap: 1rem;
}

.staggered-columns .card {
  break-inside: avoid; /* Prevents cards from splitting across columns */
  margin-bottom: 1rem;
}

@media (max-width: 768px) {
  .staggered-columns {
    columns: 2;
  }
}
<div class="ui container">
  <div class="ui row staggered-columns">
    <div class="ui card">...</div>
    <div class="ui card">...</div>
    <!-- More cards -->
  </div>
</div>

Quick Recommendation

  • Use CSS Grid if you need row-by-row ordering and modern browser support.
  • Use Masonry if you have dynamic content or need broader cross-browser compatibility.
  • Use CSS Columns for static content where ordering isn’t a priority (and you want zero JS).

内容的提问来源于stack exchange,提问作者Mayank Vaidya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:40:00