Simonzhangs' blog Simonzhangs' blog
首页
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • JS设计模式总结
  • 《Vue》
  • 《React》
  • 《TypeScript 从零实现 axios》
  • TypeScript
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • apple music
  • extension
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Simonzhangs

前端学习探索者
首页
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • JS设计模式总结
  • 《Vue》
  • 《React》
  • 《TypeScript 从零实现 axios》
  • TypeScript
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • apple music
  • extension
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

    • CSS教程和技巧收藏
    • flex布局语法
    • flex布局案例-基础
    • flex布局案例-骰子
    • flex布局案例-圣杯布局
    • flex布局案例-网格布局
    • flex布局案例-输入框布局
    • CSS3之transition过渡
    • CSS3之animation动画
    • 「布局技巧」图片未加载前自动撑开元素高度
    • 文字在一行或多行时超出显示省略号
    • 从box-sizing属性入手,了解盒子模型
    • 水平垂直居中的几种方式-案例
    • 如何根据系统主题自动响应CSS深色模式
    • 「css技巧」使用hover和attr()定制悬浮提示
    • CSS-function汇总
    • CSS基础

      • CSS实现垂直居中
        • 1. line-height 适用于文本居中方法
        • 2. 设置伪元素 ::before
        • 3. absolute + translate(-50%,-50%)
        • 4. absolute + calc()
        • 5. absolute + margin:auto
        • 6. display: flex
        • 7. display:table-cell
      • position种类以及对应效果和用法
      • display常见属性
      • CSS选择器的优先级
      • 总结
  • JavaScript文章

  • 学习笔记

  • 前端
  • CSS
  • CSS基础
simonzhangs
2022-03-26
目录

CSS实现垂直居中

# CSS实现垂直居中

用CSS实现垂直居中的方法有很多种:

# 1. line-height 适用于文本居中方法

子元素的line-height跟父元素的height设置一样的高度

<div class="parent">
    <span class="child">你好</span>
</div>
1
2
3
.parent{
    width:200px;
    height:200px;
    border:1px solid red;
}
.child{
    width:200px;
    line-height: 200px;   
    text-align: center; //水平居中
    display: inline-block;
}
1
2
3
4
5
6
7
8
9
10
11

# 2. 设置伪元素 ::before

.parent{
    width:200px;
    height: 200px;
    border:1px solid red;
    
}
.parent::before{
    content:'';
    display: inline-block;
    height:100%;
    vertical-align: middle;  
  
}
.child{
    width:80px;
    height: 100px;
    background:red;
    vertical-align: middle;
    display: inline-block;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

给父元素添加伪元素::before,让伪元素高度为100%,设置为行内块元素,同时子元素也设置为行内块元素,将vertical-align属性设置为middle;

# 3. absolute + translate(-50%,-50%)

.parent{
    width: 200px;
    height: 200px;
    background:#ddd;
    position: relative;
    
}
.child{
    width: 100px;
    height: 50px;
    background:green;
    position: absolute;
    top: 50%;
    left:50%;
    transform: translate(-50% , -50%);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

父元素设置相对定位position:relative,子元素设置绝对定位position:absolute,top和left相对父元素的50%,同时设置transform: translate(-50% , -50%);使得水平和垂直方向上居中。

translate()函数是css3的新特性.在不知道自身宽高的情况下,可以利用它来进行水平垂直居中。 当使用:top: 50%;left: 50%;, 是以左上角为原点,故不处于中心位置 translate(-50%,-50%) 作用是,往上(x轴),左(y轴)移动自身长宽的 50%,以使其居于中心位置。

与负margin-left和margin-top实现居中不同的是,margin-left必须知道自身的宽高,而translate可以在不知道宽高的情况下进行居中,tranlate()函数中的百分比是相对于自身宽高的百分比,所以能进行居中。

# 4. absolute + calc()

.parent{
  width: 200px;
  height: 200px;
  border:1px solid black;
  position: relative;
}
.child{
  width: 100px;
  height: 50px;
  position: absolute;
  background:red;
  top: calc(50% - 25px); /*垂直居中 */
  left:calc(50% - 50px); /*水平居中 */
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

原理同3类似,calc()中需要通过手动计算子元素宽高来实现

# 5. absolute + margin:auto

.parent{
    width:200px;
    height: 200px;
    position: relative;
    border:1px solid red;   
}
.child{
  width:50px;
    height: 50px;
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    margin:auto;
    background:red;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

父元素使用 relative 相对定位,子元素使用absolute绝对定位,同时设置上下左右数值均设置为0,同时设置margin:auto;

# 6. display: flex

 .parent{
    width: 200px;
    height: 200px;
    background: grey;
    display: flex;
    justify-content: center;
    align-items: center;
}
 .child{
    width: 100px;
    height: 50px;
    background:green;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

父元素设置为flex布局,设置justify-content: center;水平居中,设置align-items: center;垂直居中。

# 7. display:table-cell

 .parent{
    width:200px;
    height:200px;
    border:1px solid red;
    display: table;
}
 .child{
    background:red;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
1
2
3
4
5
6
7
8
9
10
11
12

父元素设置display:table,子元素设置display:table-cell,子元素会自动撑满父元素,设置vertical-align: middle;text-align: center;实现垂直和水平居中。

编辑 (opens new window)
#HTML#CSS
上次更新: 2022/05/17, 21:50:47
CSS-function汇总
position种类以及对应效果和用法

← CSS-function汇总 position种类以及对应效果和用法→

最近更新
01
一些有意思的类比
06-16
02
the-super-tiny-compiler解析
06-06
03
计算机编译原理总概
06-06
更多文章>
Theme by Vdoing | Copyright © 2021-2022
蜀ICP备2021023197号-2
Simonzhans | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜