css实现纵向分列,中间间距相等

方法一:使用网格布局(Grid Layout)

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      height: 100px;
      display: grid;
      grid-template-columns: 2fr 1fr 3fr; /* 自定义每一列的比例 */

      /* 将三个行都设置为平均分配剩余空间 */
      /* grid-template-rows: 1fr 1fr 1fr; */
      /* 设置 3 列,并使每列占据相同的比例 */
      /*grid-template-columns: repeat(3, 1fr); */
      gap: 20px; /* 设置列之间的间距 */
    }
    
    .item {
      background-color: #f1f1f1;
      padding: 20px;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="item">项目1</div>
    <div class="item">项目2</div>
    <div class="item">项目3</div>
  </div>
</body>
</html>

方法二:使用多列布局(Multi-column Layout)

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      column-count: 3; /* 设置列数 */
      column-gap: 20px; /* 设置列之间的间距 */
    }
    
    .item {
      background-color: #f1f1f1;
      padding: 20px;
      -webkit-column-break-inside: avoid; /* 防止内容中断到不同列(Webkit 内核浏览器)*/
      page-break-inside: avoid; /* 防止内容中断到不同列(非 Webkit 内核浏览器)*/
      break-inside: avoid; /* 防止内容中断到不同列 */
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="item">项目1</div>
    <div class="item">项目2</div>
    <div class="item">项目3</div>
  </div>
</body>
</html>
相关推荐
Csvn3 小时前
CSS :has() 选择器实战:没有它之前我们写了多少冗余 JS
前端·css
用户059540174461 天前
大模型长上下文遗忘排查实录:用 Playwright 自动化测试,揪出了 90% 的存储序列化 bug
前端·css
天蓝色的鱼鱼2 天前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
用户059540174462 天前
向量库静默丢数据踩坑实录:Playwright 端到端测试让我排查了72小时
前端·css
ZhengEnCi3 天前
Q06-导航按钮高级拟态玻璃效果构建完全指南
前端·css
用户059540174463 天前
Redis持久化踩坑实录:这个数据丢失Bug让我排查了6小时
前端·css
用户059540174464 天前
Redis记忆存储故障恢复测试踩坑实录:手动测试让我漏掉了2个一致性Bug
前端·css
用户059540174464 天前
用了3年Mock,才发现Redis记忆存储的测试一直漏掉了60%的边界场景
前端·css
用户059540174465 天前
用了6个月LangChain,才发现AI Agent的记忆存储一直有坑——写了23个Pytest用例才彻底修好
前端·css
用户059540174465 天前
把LLM记忆测试从手工脚本换成Pytest参数化,回归时间从2小时降到10分钟
前端·css