This article first in the public number: xiao Yao students, synchronous update personal blog: story film, reprint please sign. Code is constantly updated!!
The Demo preview is here at !!!!
Code Preview Demo
1. Use line-height and vertical-align
html
<div class="box"<span> Test text </span> </div>Copy the code
css
.box{
width: 200px;
height: 200px;
overflow: hidden;
background: #ccc;
text-align: center;
}
.box span{
vertical-align: middle;
line-height: 200px;
}
Copy the code
2. Use display:table-cell to center horizontal and vertical display
html
<div class="table">
<span class="cell"</span> </div>Copy the code
css
.table{
display: table;
}
.cell{
display: table-cell;
vertical-align: middle;
text-align: center;
}
Copy the code
3. Position + Transform is used to realize horizontal and vertical centered display
html
<div class="box"</span> </div>Copy the code
css
.box{
position: relative;
}
.box span{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Copy the code
4. Use display: Flex
html
<div class="flex"</span> </div>Copy the code
css
display:flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
Copy the code
5. Use display: box
html
<div class="box"</span> </div>Copy the code
css
display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;
Copy the code