Skip to content

CSS 布局技巧

1. 元素水平垂直居中布局

1.1 Flex 布局

css
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.item {
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.2 Grid 布局

css
.container {
  display: grid;
  place-items: center;
  height: 100vh;
}

.item {
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.3 Table 布局

css
.container {
  display: table;
  width: 100%;
  height: 100vh;
}

.wrapper {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.item {
  display: inline-block;
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.4 绝对定位 + transform

css
.container {
  position: relative;
  height: 100vh;
}

.item {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.5 绝对定位 + 负边距

css
.container {
  position: relative;
  height: 100vh;
}

.item {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -100px;
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.6 绝对定位 + Calc 函数

css
.container {
  position: relative;
  height: 100vh;
}

.item {
  position: absolute;
  top: calc(50% - 100px);
  left: calc(50% - 100px);
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

1.7 绝对定位 + margin auto

css
.container {
  position: relative;
  height: 100vh;
}

.item {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  width: 200px;
  height: 200px;
  background: #f0f0f0;
}

2. 九宫格布局

2.1 Flex 布局实现

css
.container {
  display: flex;
  flex-wrap: wrap;
  width: 300px;
  height: 300px;
  border: 1px solid #000;
}

.item {
  width: 33.33%;
  height: 33.33%;
  border: 1px solid #ccc;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
}

2.2 Grid 布局实现

css
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 300px;
  height: 300px;
  border: 1px solid #000;
}

.item {
  border: 1px solid #ccc;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
}

3. 左侧固定并且可收缩,右边自适应

css
.container {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 200px;
  background: #f0f0f0;
  transition: width 0.3s ease;
  overflow: hidden;
}

.sidebar.collapsed {
  width: 60px;
}

.main {
  flex: 1;
  background: #fff;
  padding: 20px;
}

4. 左右两边固定,中间自适应

4.1 Flex 布局实现

css
.container {
  display: flex;
  height: 100vh;
}

.left {
  width: 200px;
  background: #f0f0f0;
}

.center {
  flex: 1;
  background: #fff;
  padding: 20px;
}

.right {
  width: 200px;
  background: #f0f0f0;
}

4.2 Grid 布局实现

css
.container {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  height: 100vh;
}

.left {
  background: #f0f0f0;
}

.center {
  background: #fff;
  padding: 20px;
}

.right {
  background: #f0f0f0;
}

5. 弹性自适应布局

5.1 左侧固定右侧撑满剩余空间

css
.container {
  display: flex;
}

.left {
  width: 200px;
  background: #f0f0f0;
}

.right {
  flex: 1;
  background: #fff;
  padding: 20px;
}

5.2 多个元素自动平分宽度

css
.container {
  display: flex;
}

.item {
  flex: 1;
  background: #f0f0f0;
  margin: 0 10px;
  padding: 20px;
  text-align: center;
}

5.3 一行放不下自动换行

css
.container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.item {
  flex: 1 1 200px;
  background: #f0f0f0;
  padding: 20px;
  text-align: center;
}

5.4 两端对齐

css
.container {
  display: flex;
  justify-content: space-between;
}

.item {
  width: 100px;
  background: #f0f0f0;
  padding: 20px;
  text-align: center;
}

5.5 间距均分

css
.container {
  display: flex;
  justify-content: space-around;
}

.item {
  width: 100px;
  background: #f0f0f0;
  padding: 20px;
  text-align: center;
}

6. 等宽多列布局

6.1 Flex 布局实现

css
.container {
  display: flex;
}

.column {
  flex: 1;
  background: #f0f0f0;
  margin: 0 10px;
  padding: 20px;
  text-align: center;
}

6.2 Grid 布局实现

css
.container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.column {
  background: #f0f0f0;
  padding: 20px;
  text-align: center;
}

6.3 浮动实现

css
.container {
  overflow: hidden;
}

.column {
  float: left;
  width: 25%;
  box-sizing: border-box;
  background: #f0f0f0;
  padding: 20px;
  text-align: center;
}

基于 VitePress 的本地知识库