When we were writing the horse, we probably didn’t know much about the method of horizontal and vertical centering. So I will give you a summary of several commonly used horizontal and vertical centering methods.
first method<!--html box code--><!--horizontally and vertically centered--><div class=Centered1> <p>d first type</p></div><!-css style part--> .Centered1{ background-color: #800070; width: 100%; height:500px; position: relative; } .Centered1 p{ width: 200px; height: 200px; background-color: deeppink; line-height: 200px; text-align: center; position: absolute; left: 0; bottom: 0; right:0; top: 0; margin: auto; }Second method
<!--html box code--><!--centered horizontally and vertically--><div class=Centered2> <p>dSecond type</p></div><!-css style part--> /*The second method is horizontally and vertically centered*/ .Centered2{ background-color: #ef8518; width: 100%; height: 500px; position: relative; } .Centered2 p { position: absolute; width: 200px; height: 200px; background-color:red; line-height: 200px; text-align: center; left: 50%; top:50%; margin-left:-100px; margin-top: -100px; }Third method
<!--html box code--><!--centered horizontally and vertically--><div class=Centered3> <p>d third type</p></div><!-css style part--> /*The third method is horizontal and vertical centering*/ .Centered3{ background-color: dimgrey; width: 100%; height: 500px; position: relative; } .Centered3 p { position: absolute; width: 200px; height: 200px; background-color:darkorange; line-height: 200px; text-align: center; left: 50%; top: 50%; transform:translate(-50%,-50%); }fourth method
<!--html box code--><!--horizontally and vertically centered--><div class=Centered4> <p>d fourth type</p></div><!-css style part--> /*The fourth method is horizontal and vertical centering, old version of flex layout*/ .Centered4{ background-color: #FF4444; width: 100%; height: 500px; display: -webkit-box; -webkit-box-pack:center; -webkit-box-align: center; } .Centered4 p { width: 200px; height: 200px; background-color:cadetblue; line-height: 200px; text-align: center; }The fifth method
<!--html box code--><!--horizontally and vertically centered--><div class=Centered5> <p>d fifth type</p></div><!-css style part--> /*The fifth method is horizontal and vertical centering, the new version of flex is horizontal and vertical centering*/ .Centered5{ background-color: darkslateblue; width: 100%; height: 500px; display: flex; justify-content:center; align-items: center; } .Centered5 p { width: 200px; height: 200px; background-color:fuchsia; line-height: 200px; text-align: center; }
The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope everyone will support VeVb Wulin Network.