元素水平垂直居中
本文对元素的水平垂直居中通过两方面来呈现,每一方面又分为元素宽高已知和未知,以上代码均通过测试,可直接使用,如有缺陷欢迎指正!
相对于父元素(宽高已知)
用右侧目录点击观看体验更佳
1.1 margin硬挤
<style>
.a1;
border: 1px solid red;{
width: 600px;
height: 600px solid red;
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
margin-left: 250px;
margin-top: 250px;
}
</style>
<body>
<div class="a1">
<div class="a2"></div>
</div>
</body>
1.2 text-align+vertical-align
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
line-height: 600px;
text-align: center;
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
vertical-align: middle;
display: inline-block;
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.3.1 position+margin
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
position: relative;
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
top: 0px;
right: 0px;
left: 0px;
bottom: 0px;
margin: auto;
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.3.2 positon+calc函数
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
position: relative;
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
top: calc(50% - 100px / 2);
/* right: 0px; */
left: calc(50% - 100px / 2);
/* bottom: 0px; */
/* margin: auto; */
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.3.3 position+margin-top
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
position: relative;
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
margin-top: -50px;
top: 50%;
margin-left: -50px;
left: 50%;
/* top: calc(50% - 100px / 2); */
/* right: 0px; */
/* left: calc(50% - 100px / 2); */
/* bottom: 0px; */
/* margin: auto; */
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.3.4 position+translate
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
position: relative;
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
transform: translate(-50%, -50%);
/* margin-top: -50px; */
top: 50%;
/* margin-left: -50px; */
left: 50%;
/* top: calc(50% - 100px / 2); */
/* right: 0px; */
/* left: calc(50% - 100px / 2); */
/* bottom: 0px; */
/* margin: auto; */
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.4.2 flex-box
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
/* position: relative; */
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
/* position: absolute; */
/* transform: translate(-50%, -50%); */
/* margin-top: -50px; */
/* top: 50%; */
/* margin-left: -50px; */
/* left: 50%; */
/* top: calc(50% - 100px / 2); */
/* right: 0px; */
/* left: calc(50% - 100px / 2); */
/* bottom: 0px; */
/* margin: auto; */
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.4.3 flex-box+margin
<style>
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
/* position: relative; */
display: flex;
/* line-height: 600px; */
/* text-align: center; */
}
.a2 {
width: 100px;
height: 100px;
background-color: yellow;
/* position: absolute; */
/* transform: translate(-50%, -50%); */
/* margin-top: -50px; */
/* top: 50%; */
/* margin-left: -50px; */
/* left: 50%; */
/* top: calc(50% - 100px / 2); */
/* right: 0px; */
/* left: calc(50% - 100px / 2); */
/* bottom: 0px; */
margin: auto;
/* vertical-align: middle; */
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}
</style>
1.5 table-cell
.a1 {
width: 600px;
height: 600px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
/* justify-content: center; */
/* align-items: center; */
/* position: relative; */
/* line-height: 600px; */
text-align: center;
}
.a2 {
width: 100px;
height: 100px;
display: inline-block;
background-color: yellow;
/* position: absolute; */
/* transform: translate(-50%, -50%); */
/* margin-top: -50px; */
/* top: 50%; */
/* margin-left: -50px; */
/* left: 50%; */
/* top: calc(50% - 100px / 2); */
/* right: 0px; */
/* left: calc(50% - 100px / 2); */
/* bottom: 0px; */
/* margin: auto; */
vertical-align: middle;
/* display: inline-block; */
/* margin-left: 250px; */
/* margin-top: 250px; */
}