IMAGINE'S BLOG IMAGINE'S BLOG
首页
  • 原生JS

    • JavaScript
  • 前端框架扩展

    • Vue
    • React
    • UI组件库
  • HTML
  • CSS
  • 浏览器
  • 分类
  • 标签
  • 归档
  • 技术文档
  • GitHub相关
  • Nodejs
关于
  • 网站
  • 友情链接
GitHub (opens new window)

peng

平平无奇的web前端开发一枚
首页
  • 原生JS

    • JavaScript
  • 前端框架扩展

    • Vue
    • React
    • UI组件库
  • HTML
  • CSS
  • 浏览器
  • 分类
  • 标签
  • 归档
  • 技术文档
  • GitHub相关
  • Nodejs
关于
  • 网站
  • 友情链接
GitHub (opens new window)
  • HTML

  • CSS

    • CSS教程和技巧收藏
    • flex布局语法
    • flex布局案例-基础
    • flex布局案例-骰子
    • flex布局案例-圣杯布局
    • flex布局案例-网格布局
    • flex布局案例-输入框布局
    • CSS3之transition过渡
    • CSS3之animation动画
    • 「布局技巧」图片未加载前自动撑开元素高度
    • 文字在一行或多行时超出显示省略号
    • 从box-sizing属性入手,了解盒子模型
    • css水平垂直居中
      • absolute + margin auto
      • absolute + 负 margin
      • absolute + calc
      • lineheight
      • flex
      • absolute + transform
      • grid
      • table-cell
    • 如何根据系统主题自动响应CSS深色模式
    • 「css技巧」使用hover和attr()定制悬浮提示
    • CSS给table的tbody添加滚动条
    • 常用的一些CSS样式
  • 页面杂谈
  • CSS
peng
2020-03-13
目录

css水平垂直居中

这是一个很常见的样式布局,网上也有很多的实现方式,本文主要是针对一些比较常用到的方法做一下总结,记录一下。

现有实现方式大致的思路归纳为以下几种:

  • 固定宽高居中
    • absolute + margin auto
    • absolute + 负 margin
    • absolute + calc
  • 不定宽高居中
    • ​ lineheight
    • ​flex
    • absolute + transform
    • ​grid
    • table-cell

公共代码(后面代码都基于这里)

<div class="out">
  <div class="inner">水平垂直居中</div>
</div>
1
2
3

实现的效果:


# absolute + margin auto

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  position: relative;
}

.inner {
  background-color: blueviolet;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# absolute + 负 margin

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  position: relative;
}

.inner {
  background-color: blueviolet;
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -150px;
  margin-top: -150px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# absolute + calc

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  position: relative;
}

.inner {
  background-color: blueviolet;
  width: 300px;
  height: 300px;
  position: absolute;
  top: calc(50% - 150px);
  left: calc(50% - 150px);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# lineheight

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  line-height: 500px;
  text-align: center;
  font-size: 0px;
}

.inner {
  font-size: 16px;
  background-color: blueviolet;
  display: inline-block;
  vertical-align: middle;
  line-height: initial;
  /* 若需要设置元素的宽高时 */
  /* width: 300px;
    height: 300px; */
  text-align: center;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# flex

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.inner {
  background-color: blueviolet;
}
1
2
3
4
5
6
7
8
9
10
11
12

# absolute + transform

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  position: relative;
}

.inner {
  background-color: blueviolet;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# grid

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  display: grid;
}

.inner {
  background-color: blueviolet;
  align-self: center;
  justify-self: center;
}
1
2
3
4
5
6
7
8
9
10
11
12

# table-cell

.out {
  width: 500px;
  height: 500px;
  border: 1px solid #000;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

.inner {
  background-color: blueviolet;
  display: inline-block;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

好了,基本上就这些了;还有些不怎么用到的就没去整理了。

  • PC 端:考虑兼容性的话推荐使用 absolute+负 margin 和​table-cell; 否则直接 flex。
  • 移动端:首推​flex。
#CSS
上次更新: 2022/09/06, 10:30:04
从box-sizing属性入手,了解盒子模型
如何根据系统主题自动响应CSS深色模式

← 从box-sizing属性入手,了解盒子模型 如何根据系统主题自动响应CSS深色模式→

最近更新
01
Axios 封装
09-06
02
MySQL数据库常用操作
09-06
03
解决element表格数据量过大导致页面渲染缓慢、卡顿问题
09-06
更多文章>
Theme by Vdoing | Copyright © 2020-2024 peng | IMAGINE
image | imgloc.com
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式