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

CSS column-count布局疑问:如何让首个元素占满容器整宽度

Solution for Full-Width First Item in CSS Column-Based Masonry Layout

Got it, let's get this sorted! The issue you're facing is that column-span: all (the property that lets an element span all columns) doesn't play nicely with inline-block by default. Here's how to fix it step by step:

Working CSS Adjustment

Update your existing CSS to add a rule for the first .box-article element:

.container-post{ column-gap: 20px; margin: 0 auto; column-count: 2; }
.box-article { display: inline-block; background: #ffffff; padding: 20px; margin: 0 0 15px; width: 100%; box-sizing: border-box; border: solid 1px gray; }
/* Add this rule to target the first article */
.box-article:first-child {
  column-span: all;
  display: block; /* Switch to block-level to enable column-span */
}
.box-article img{ width: 100%; }
.box-article a{ color: #000000; }
.box-article h2{ color: #000000; text-align: center; }

Why This Works

  • column-span: all tells the first article to stretch across all columns in the container (which is set to 2 columns, so it takes full width).
  • We switch the first element to display: block because column-span only works on block-level elements—your original inline-block was preventing the property from taking effect, which is probably why your earlier attempts with :first-child failed.

What It Does

  • The first .box-article will now take up the full width of the .container-post
  • All subsequent articles will continue to flow into the 2-column masonry layout as before
  • The styling (padding, border, text alignment) for the first article stays consistent with the others, just like you wanted

Compatibility Note

column-span is supported in all modern browsers (Chrome, Firefox, Safari, Edge). If you need to support very old browsers like IE11 or earlier, this property won't work—for those cases, you could move the first article outside the .container-post and style it separately, but that's less clean than this solution.

Your existing HTML doesn't need any changes—keep it exactly as is!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 18:02:55