让一个元素垂直水平居中的三种方法
2017-07-09 11:02
537 查看
第一种方法:
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、左都为50%-->
left:50%;
top:50%;
<!--设置元素的左外边距、上外边距为宽高的负1/2-->
margin-left:-100px;
margin-top:-200px;
}
*兼容性好;缺点:必须知道元素的宽高
-------------
第二种方法:
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、左都为50%-->
left:50%;
top:50%;
<!--设置元素的相对于自身的偏移度为负50%(也就是元素自身尺寸的一半)-->
transform:translate(-50%,-50%);
}
*这是css3里的样式;缺点:兼容性不好,只支持IE9+的浏览器
---------------
第三种方法
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、下、左、右都为0-->
left:0;
right:0;
top:0;
bottom:0;
<!--设置元素的margin样式值为 auto-->
margin:auto;
}
转载自:http://www.cnblogs.com/linsinan/p/6132241.html
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、左都为50%-->
left:50%;
top:50%;
<!--设置元素的左外边距、上外边距为宽高的负1/2-->
margin-left:-100px;
margin-top:-200px;
}
*兼容性好;缺点:必须知道元素的宽高
-------------
第二种方法:
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、左都为50%-->
left:50%;
top:50%;
<!--设置元素的相对于自身的偏移度为负50%(也就是元素自身尺寸的一半)-->
transform:translate(-50%,-50%);
}
*这是css3里的样式;缺点:兼容性不好,只支持IE9+的浏览器
---------------
第三种方法
div.box{
weight:200px;
height:400px;
<!--把元素变成定位元素-->
position:absolute;
<!--设置元素的定位位置,距离上、下、左、右都为0-->
left:0;
right:0;
top:0;
bottom:0;
<!--设置元素的margin样式值为 auto-->
margin:auto;
}
转载自:http://www.cnblogs.com/linsinan/p/6132241.html
相关文章推荐
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 元素居中的三种方法(包括垂直居中和水平居中)
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 元素居中的三种方法(包括垂直居中和水平居中)
- CSS实现父级元素属性display为block的元素垂直和水平居中的三种方法
- 让一个元素垂直水平居中的四种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 元素的垂直水平居中的三种方法
- 如何让DIV水平和垂直居中三种方法
- css 实现元素水平垂直居中总结5中方法
- CSS让一个元素水平垂直居中
- css系列(布局):实现一个元素在浏览器中水平、垂直居中的几个方案
- 关于元素浮动后垂直居中和水平居中的方法
- 实现绝对定位元素水平垂直居中的两种方法
- css水平垂直居中三种实现方法
- 一个垂直居中显示元素的方法