Four methods vertically centered CSS

1.margin: auto method:

html:

<div>
<img src="mm.jpg">
</div>

css:

div{
width:400px;
height:400px;
position:relative;
border:1px solid #465468;
}
img{
position:absolute;
margin:auto;
top:0;
left:0;
right:0;
bottom:0;
}

Up and down is positioned 0, margin: 0 may be implemented centrally from the document stream

2.margin negative law

.container{
width:500px;
height:500px;
border: 2px solid #379;
position:relative;
}

,inner{
width:480px;
height:480px;
background-color:#746;
position:absolute;
top:50%;
left:50%;
margin-top:-190px;
margin-left:-240px;
}

May be replaced by a negative value

transform:translateX(-50%)
transform:translateY(-50%)

3.table-cell (without departing from the document flow)

4. Use flex

Published 158 original articles · won praise 44 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43277404/article/details/103949648
Recommended