博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSS的7种常用的垂直居中的方法
阅读量:4539 次
发布时间:2019-06-08

本文共 1067 字,大约阅读时间需要 3 分钟。

1、绝对定位上下百分之五十然后上外边距做外边距都是他的宽高的一半

#child{     width: 200px;     height: 150px;     position: absolute;     left: 50%;     top:50%;     margin-top: -75px;     margin-left: -100px;}

 

2、根据上面的方法有一定的变化,不过也是根据绝对定位

.child{     position: absolute;     top: 0;     bottom: 0;     right: 0;     left: 0;     margin: auto;}

 

3、用于文本且单行,line-height要等于父元素高度

.div{    text-align:center;    line-height: 100px;    background-color:#fff;}

 

4、利用display:table;与display:table-cell;(注意只有.child框内元素会垂直居中)

.parent{     display: table;}.child{     display: table-cell;     vertical-align: middle;     text-align: center;    background-color: #fff;}

 

5、利用css3的transfrom

.child{   width: 10px;   height: 10px;   background-color: #fff;   position: relative;   top: 50%;   transform: translateY(-50%);   margin: 0 auto;}

 

6、绝对定位不需要知道宽高

.child{  position: absolute;  left: 50%;  top: 50%;  transform: translate(-50%, -50%);  /*水平垂直居中*/}

 

7、flex垂直居中方式

.child{  display: flex;  justify-content: center;  /*水平居中*/  align-items: center;      /*垂直居中*/}

 

转载于:https://www.cnblogs.com/huangqiming/p/6121849.html

你可能感兴趣的文章
Elasticsearch 2.3 java api
查看>>
golang写入csv
查看>>
基础2
查看>>
java基础篇---网络编程(UDP程序设计)
查看>>
Kafka Producer相关代码分析【转】
查看>>
麻省理工学院公开课-第四讲:快速排序 及 随机化 算法
查看>>
pycharm 的包路径设置export PYTHONPATH=$PYTHONPATH
查看>>
SQL语句创建函数
查看>>
解决mysql无法显示中文/MySQL中文乱码问号等问题
查看>>
CentOS 7.2 配置mysql5.7
查看>>
python输出转义字符
查看>>
计算一个整数二进制中1的个数
查看>>
netdom join 错误:指定的域不存在,或无法联系。
查看>>
Android中Dialog的使用
查看>>
Android Activity接收Service发送的广播
查看>>
[Leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印
查看>>
加速和监控国际网络
查看>>
【Flex】读取本地XML,然后XML数据转成JSON数据
查看>>
字符串循环右移-c语言
查看>>
解决从pl/sql查看oracle的number(19)类型数据为科学计数法的有关问题
查看>>