跳到主要内容

传统布局以

一、 多列布局

两栏布局(左侧固定,右侧自适应):

1. 使用 Flex

.left {
width; 200px;
}
.right {
flex: 1;
}

2. 使用 Grid

container {
grid-template-columns: 200px 1fr;
}

二、 三栏布局(两侧固定,中间自适应)

  • 圣杯布局
  • 双飞翼布局(传统方案)

1. 使用 column-count

指定多列的列数即列宽
.container {
/* 指定列数 */
column-count: 3;
/* 指定列宽 */
column-width: 10rem;
/* 指定列间隙 */
column-gap: 20px;
/* 指定列间分割线(该分割线并不占用宽度。它置于 column-gap 创建的间隙内) */
column-rule: 4px dotted #0f6;
}

二、 水平垂直居中

1. 使用 Flex

container {
display: flex;
justify-content: center;
align-items: center;
}

2. 使用 Grid

container {
display: grid;
/* `place-items` 是
- `align-items`
- `justify-items`
的缩写 */
place-items: center;
}

3. 使用 Position + transform

child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}