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>
相关推荐
inksci2 天前
用飞帆做了个小网页
前端·javascript·css·html
星云_byto2 天前
CSS面试题
前端·css
科研邪修法2 天前
PDF新增页面文字与文本批注怎么区分?以双版本培训讲义为例
前端·css·pdf
狮子座的男孩3 天前
html+css基础:31、响应式布局(媒体类型,媒体特性,运算符,常用阈值)
前端·css·经验分享·html·响应式布局·媒体类型/特性·常用阈值
狮子座的男孩3 天前
html+css基础:32、BFC(父子元素防止 margin 塌陷,父子元素清除浮动,兄弟元素两栏布局)
前端·css·经验分享·html·bfc·margin塌陷·清除浮动
吴长建先生4 天前
Flutter IM 即时聊天应用开发:从 0 到 1 实
css
htzyl2065 天前
前端样式-弹窗淡入淡出,为什么你的 `transition` 一直不生效
前端·css·html
Brookty5 天前
CSS基础知识(二):盒模型、内外边距、外边距折叠与父子元素的布局关系
前端·css
Mh5 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
Brookty6 天前
CSS基础知识(一):选择器、优先级、层叠与继承、边框属性及盒模型
前端·css