效果图:web
内部 div 要有宽度bash
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: #ccc;
display: flex;
display: -webkit-flex;
justify-content: center;
align-items: center;
}
.box .a{
width: 100px;
height: 100px;
background-color: blue;
}
</style>
HTML 代码:
<div class="box">
<div class="a"></div>
</div>复制代码
效果图:布局
CSS代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.box .a{
width: 100px;
height: 100px;
background-color: blue;
position: absolute;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
}
</style>
HTML 代码:
<div class="box">
<div class="a">love</div>
</div>
复制代码
margin: -50px 0 0 -50px;
替换为:
transform: translate(-50%,-50%);
效果图:flex
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.box .a{
background-color: blue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
复制代码
效果图:spa
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.box .a{
width: 100px;
height: 100px;
background-color: blue;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
HTML 代码:
<div class="box">
<div class="a">love</div>
</div>
复制代码
效果图:3d
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.box .a{
/* 若是不设置宽高,将铺满整个父级*/
background-color: pink;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
}
</style>
HTML:
<div class="box">
<div class="a">love</div>
</div>复制代码
table 实现垂直居中,子集元素能够是块元素,也能够不是块元素code
CSS:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
display: table-cell;
vertical-align: middle;
}
.box .a{
margin-left: 100px;
width: 100px;
height: 100px;
background-color: blue;
}
</style>
<div class="box">
<div class="a">love</div>
</div>
复制代码
行元素 text-align :center;orm
块元素 :margin :0 auto;cdn
text-align : center 给元素的父级加,能够使文本或者行级元素(例如:图片)水平居中
line-height : 值为元素的高度,能够使元素的文本内容垂直居中
margin: 0 auto 使用条件:父级元素宽度无关紧要,子级元素必须使块元素,并且要有宽度(不然继承父级)复制代码
display:table-cell
会使元素表现的相似一个表格中的单元格td,利用这个特性能够实现文字的垂直居中效果。同时它也会破坏一些 CSS 属性,使用 table-cell 时最好不要与 float 以及 position: absolute 一块儿使用,设置了 table-cell 的元素对高度和宽度高度敏感,对margin值无反应,能够响 padding 的设置,表现几乎相似一个 td 元素。blog
小结:
1. 不要与 float:left, position : absolute, 一块儿使用
2. 能够实现大小不固定元素的垂直居中
3. margin 设置无效,响应 padding 设置
4. 对高度和宽度高度敏感
5. 不要对 display:table-cell 使用百分比设置宽度和高度
效果图:
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
display: table-cell;
text-align: center;/*使元素水平居中 */
vertical-align: middle;/*使元素垂直居中 */
}
</style>
HTML 代码:
<div class="box">love</div>
复制代码
给父级设置 display : table,子集设置 display:tablecell ,子集会充满全屏
CSS 代码:
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
display: table;
}
.box .a{
display: table-cell;
vertical-align: middle;
text-align: center;
background-color: blue;
}
</style>
HTML :
<div class="box">
<div class="a">love</div>
</div>复制代码
效果图:
<style>
.box{
width: 300px;
height: 300px;
background-color: skyblue;
display: table-cell;
text-align: center;
vertical-align: middle;
}
img{
/* 设置成块元素后,text-align:center 就会失效 */
width: 100px;
height: 100px;
}
</style>
HTML:
<div class="box">
<img src="1.jpg" alt="">
</div>复制代码
中间的图片会随着外层容器的大小而自动水平垂直居中,其实原理和文字水平垂直居中如出一辙
CSS 代码:
<style>
*{
padding: 0;
margin: 0;
}
.box{
display: table;
width: 90%;
margin: 10px auto;
padding: 10px;
border: 1px solid green;
height: 100px;
}
.left,.right{
display: table-cell;
width: 20%;
border: 1px solid red;
}
.center{
/* padding-top: 10px; */
height: 100px;
background-color: green;
}
</style>
HTML:
<div class="box">
<div class="left">
<p>我是左边</p>
</div>
<div class="center">
<p>我是中间</p>
</div>
<div class="right">
<p>我是右边</p>
</div>
</div>
复制代码
效果:
其中 center 的顶部没有与左右两侧对齐,缘由是 left 中的 <p> 有一个 margin-top , 而表格布局中默认是文字顶部对齐的,因此 center 会向下移动到首行文字基线对齐,解决办法是为 center 添加与左右两侧中 margin-top 较大者等值的 padding-top 便可。