1. 水平居中6种
.parent {
text-align: center;
}
.child {
display: inline-block;
}
.child {
margin: 0 auto;
width: 100px;
}
.child {
width: 100px;
position: relative;
left: 50%;
margin-left: -50px;
}
.parent {
position: relative;
}
.child {
position: absolute;
left: 0;
right: 0;
width: 300px;
margin: auto;
}
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.parent {
display: flex;
justify-content: center;
}
.child {
margin: auto;
}
.parent {
display: grid;
justify-items: center;
justify-content: center;
}
.child {
margin: auto;
}
2. 垂直居中6种
.parent {
height: 500px;
line-height: 500px;
}
.child {
display: inline-block;
vertical-align: middle;
}
.parent {
position: relative;
}
.child {
height: 100px;
position: absolute;
top: 50%;
margin-top: -50px;
}
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
width: 300px;
margin: auto;
}
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
transform: translateY(-50%);
}
.parent {
display: flex;
align-items: center;
}
.child {
margin: auto;
}
.parent {
display: grid;
align-items: center;
align-content: center;
}
.child {
margin: auto;
align-self: center;
}
3. 垂直水平居中7种(垂直水平居中方法一起用)
.parent {
height: 300px;
line-height: 300px;
text-align: center;
}
.child {
display: inline-block;
vertical-aligh: middle;
}
.parent {
position: relative;
}
.child {
width: 100px;
height: 100px;
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
.parent {
position: relative;
}
.child {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.parent {
position: relative;
}
.child {
postion: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.parent {
display: flex;
align-items: center;
justify-content: center;
}
.child {
margin: auto;
}
.parent {
display: grid;
place-items: center;
place-content: center;
}
.child {
margin: auto;
place-self: center;
}
4. 两列布局