第一种 借助inline-block的特点
#d1{
display:inline-block
width:500px
height:500px
border:1px solid red
text-align:center
}
#d1:after{
content:""
display:inline-block
height:100%
vertical-align:middle
background:#000
}
#d2{
display:inline-block
width:200px
height:200px
border:1px solid red
vertical-align:middle
}
<div id="d1">
<div id="d2"></div>
</div>
第二种 利用css的transform 好用但是兼容性不好,IE10+以及其他现代浏览器才支持(手机开发可忽略)
.box{width:300pxheight:300pxborder:1px solid redposition:relative}
.content{
position:absolute
width:100px
height:100px
border:1px solid red
margin:0 auto
top:50%left:50%
/* transform:translateY(-50%)仅垂直居中*/
/* transform:translateX(-50%)仅水平居中*/
transform: translate(-50%, -50%)
/*
若父容器下只有一个元素,且父元素设置了高度,则只需要使用相对定位即可
父元素{
height:xxx
}
.子元素 {
position: relative
top: 50%
transform: translateY(-50%)
}
*/
}
<div class="box">
<div class="content"></div>
</div>
第三种:绝对定位之后的偏移
.box{
border:1px solid red
width:300pxheight:300pxposition:relative
}
.content{
border:1px solid red
width: 200pxheight: 200px
position: absoluteleft: 50%top: 50%
margin-top: -100px /* 高度的一半 */
margin-left: -100px /* 宽度的一半 */
}
<div class="box">
<div class="content"></div>
</div>
第四种:定位之后的margin: auto
.box{
border:1px solid red
width:300pxheight:300pxposition:relative
}
.content{
width: 200px
height: 200px
position: absolute
left: 0
top: 0
right: 0
bottom: 0
margin: auto
border:1px solid red
}
<div class="box">
<div class="content"></div>
</div>
第五种 flex布局
<div style="display:flexdisplay: -webkit-flexjustify-content:centeralign-items:centerwidth: 300pxheight: 300pxborder:1px solid red">
<div style="width: 200pxheight: 200pxborder:1px solid red"></div>
</div>
第六种利用display:table-cell的vertical-align属性 子元素加上“display:inline-block”可水平居中
<div style="display:table-cellvertical-align:middletext-align:centerwidth:300pxheight:300pxborder:1px solid red">
<div style="border:1px solid redwidth:200pxheight:200pxdisplay:inline-block"></div>
</div>
第七种 使用css3中的display:-webkit-box的用法这种方法还没有得到浏览器的普遍支持,如有兴趣,自行学习
如果您觉得我的文章有用,欢迎点赞和关注,也欢迎光临我的个人博客 https://github.com/BokFang
在这里总结一下CSS水平居中、垂直居中的各种方式。应该说非常全了。
总览一下:
用法:在父级元素的样式中添加 text-align:center
效果图:
用法:在元素样式添加 margin:0 auto ,使其margin-left和margin-right平分块级元素那一行剩余的宽度。
效果图:
当然如果你的块级元素没有设置width,那么div就会占满一行,也就没有水平居中的说法了。
如果由多个块级元素,则可以使用 inline-block 配合 text-align:center ,将 inline-block 写在需要居中的元素样式上, text-align:center 写在父级元素上。
效果图:
使用flex也可以轻松做到多个块级元素水平居中
用法:在父级元素样式增加 display: flex justify-content: center
效果图与第三个一样。
当然,多个块级元素能用的居中方法,在单个块级元素上也同样可以使用。
使用 display:table 配合 margin:0 auto ,可以达到不定宽块级元素居中效果。
效果图:
使用绝对定位给元素一个left:50%,然后再加一个margin-lelt:-(宽度的一半)
效果图:
不过缺点很明显,就是你知道元素宽度而且得固定不变,所以是比较蠢的一种写法。
效果图:
效果图:
这个方法和水平居中的第6个方法一样,就不多说了。
效果图:
这个和水平居中的第7个方法一样,我也就不多说了。
效果图:
效果图:
效果图:
缺点也比较明显,需要计算。
效果图:
效果图:
效果图:
需要在html中加入 <table>标签,比较低效,我就不详写了,想了解的朋友可以Google搜索一下。
如果您觉得我的文章有用,欢迎点赞和关注,也欢迎光临我的个人博客 https://github.com/BokFang
例:居中.wrapper里的.content
一. 已经元素的宽高的前提下:
(1) left:50%top:50%
父元素设置相对定位,position: relative
子元素(要居中的元素)设置绝对定位,position: absolute left: 50% top: 50%
(2)设置margin: auto
父元素设置相对定位,position: relative
子元素(要居中的元素)设置绝对定位,position: absolutemargin: auto
(3) flex布局
父元素设置 display: flex justify-content: center align-items: center
二.未知元素宽高的情况下:
(1)四个方向设置值,把元素撑开
父元素设置相对定位,position: relative
子元素设置绝对定位,position: absolutetop与bottom设置一样的值,left与right设置一样的值,把容器撑开