使不知宽高的元素水平垂直居中的方法
2017-09-15 16:49
274 查看
本文主要介绍如何使元素居中显示的几种方法,当然方法有很多,现在记录的不过是笔者目前能够想到的几种:定位、table-cell、增加空span、弹性盒模型。
html样式如下:
类名为.box1的div宽高未定。
方法一(定位)
方法四(弹性盒模型)
html样式如下:
<div class="box"> <div class="box1"></div> </div>css样式如下:
.box{ width:600px; height:600px;}
类名为.box1的div宽高未定。
方法一(定位)
.box{ width:600px; height:600px; position:relative; } .box1{ position:absolute; left:50%; top:50%; transfrom:translate(-50%,-50%); }方法二(table-cell)
.box{ width:600px; height:600px; display:table-cell; text-align:center; vertical-align:middle; } .box1{ display:inline-block; vertical-align:top; }方法三(给box中增加一个空span)
<div class="box"> <span></span> <div class="box1"></div> </div>
.box{ width:600px; height:600px; } .box span{ display:inline-block; vertical-align:middle; height:100%; } .box1{ display:inline-block; vertical-align:middle; }
方法四(弹性盒模型)
.box{ width:600px; height:600px; display:flex; justify-content:center; align-items:center; }
相关文章推荐
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 实现绝对定位元素水平垂直居中的两种方法
- CSS中元素水平垂直居中4种方法介绍
- 元素居中的三种方法(包括垂直居中和水平居中)
- 元素垂直水平居中的几种方法
- css元素的垂直水平居中;flex 已知元素宽高 未知元素宽高
- 使元素水平垂直居中的实现方法
- 未知宽高元素水平垂直居中方法
- CSS实现父级元素属性display为block的元素垂直和水平居中的三种方法
- CSS实现元素水平/垂直居中的方法
- CSRF、AMD和水平垂直居中元素的方法
- CSS Transform让百分比宽高布局元素水平垂直居中
- css元素水平垂直居中的十种方法
- 使元素相对于窗口或父元素水平垂直居中的几种方法