When I was working on a mobile project yesterday, I encountered a case like this, as shown in the picture. The image container stretches and expands freely with the width of the viewport. The image has a variable width and height, and is vertically centered:
When I was making this layout, I always thought of using display:table-cell through vertical-align:middle to achieve centering. When I use position:absolute on the same node, it will Causing this centering issue to fail.
The last solution I thought of, the case is as follows, vertical centering with variable width and height:
<!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8"> <title></title> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0"> <style type="text/css"> p{margin:0;padding:0} .wrap,.wrap1{ width:30%; padding-top:30%; background:#eee; position:relative; } .wrap p,.wrap1 p{ position:absolute; top:0; left:0; width:100%; height:100%; } .wrap p img{ max-width:100%; max-height:100%; position:absolute; top:50%; left:50%; -webkit-transform-origin:50% 50%; -webkit-transform:translate3d(-50%,-50%,0); } .wrap1 p{text-align:center;font-size:0;} .wrap1 p:after{ width:0; height:100%; display:inline-block; vertical-align:middle; content:""; } .wrap1 p img{ display:inline-block; max-width:100%; max-height:100%; vertical-align:middle; margin:0 auto; } </style></head><body> <h2>第一种方案</h2> <div class="wrap"> <p> <img src="img/download.jpg" alt="" /> </p> </div> <h1>111</h1> <div class="wrap"> <p> <img src="img/download1.jpg" alt="" /> </p> </div> <h2>第二种方案</h2> <div class="wrap1"> <p> <img src="img/download.jpg" alt="" /> </p> </div> <h1>111</h1> <div class="wrap1"> <p> <img src="img/download1.jpg" alt="" /> </p> </div> <div id="demo"> <p>水平垂直居中的随意内容</p></div> </body></html>
Below we will introduce three vertical centering solutions:
We have chewed on a few old knowledge points:
1. Horizontal centering knowledge points
text-align:center
There may not be much introduction to horizontal centering. All mainstream browsers support the text-align attribute, which only needs to be the value center That’s it;
2. Vertical-align knowledge points
vertical-align适用于 inline level, inline-block level 及 table-cells 元素上
All major browsers support the vertical-align attribute, so use this attribute to achieve vertical centering It’s a good idea;
1. Use display:table-cell;vertical-align:middle; to carry out variable height and vertical centering example demo
<div id="demo"> <p>水平垂直居中的随意内容</p></div>#demo{ display:table; width:500px; margin:10px auto; background:#eee;}#demo p{ display:table-cell; height:100px; vertical-align:middle;}
Since table can be implemented, it is natural to think of setting display to table system value. Of course, this solution has limitations, because browsers below IE8 do not support the table system value of display, so you can only see the effect in IE8 and above browsers and non-IE browsers;
2, use display:table-cell;vertical-align:middle; to perform indefinite height and vertical centering Example DEMO
<div id="demo"> <p>水平垂直居中的随意内容</p></div>#demo{ height:100px; text-align:center;}#demo:after{ display:inline-block; width:0; height:100%; vertical-align:middle; content:'';}#demo p{ display:inline-block; vertical-align:middle;}
There is one thing that needs special attention here: referring to the above code, if the width of our content container p is 100%, it will cause #demo:after to be squeezed out of one line. This is caused by the inline element spacing. You can set the font :0 elimination. If you want to maintain full compatibility, you can use the span tag instead of pseudo-classes.
Perfect solution: example demo
<div id="demo"> <p>这是一个终极实现的水平垂直居中实例</p> <!--[if lt IE 8]><span></span><![endif]--></div>#demo{ height:100px; text-align:center; font-size:0; //注意这里是去除文字间隔,放在内容宽度为100%时挤出换行}#demo:after,#demo span{ display:inline-block; *display:inline; *zoom:1; width:0; height:100%; vertical-align:middle;}#demo:after{ content:'';}#demo p{ display:inline-block; *display:inline; *zoom:1; vertical-align:middle; font-size:16px;}
Three, use translate3d(-50%,-50%,0) to achieve vertical Centering, only supports higher versions
<style type="text/css"> .main{width:600px;height:600px;position:relative} .box{position:absolute;top:50%;left:50%;-webkit-transform-origin:50% 50%;-webkit-transform:translate3d(-50%,-50%,0)} //原理有点跟 top:50%;left:50%;margin:-height/2 0 0 -width/2类似,这种是要知道高度,但是这个不需要 </style> <div class="main"> <div class="box">555666</div> </div>
Fourth, use two levels of nesting, twice left, top to achieve centering
<style type="text/css">html,body{width: 100%; height: 100%; margin: 0; padding: 0;}.wrp{position: fixed; width: 100%; height: 100%; left: 0; top: 0; background-color: rgba(0,0,0,.5);}.box{position: absolute; left: 50%; top: 50%;}.box2{position: relative; left: -50%; top: -50%; background-color: #f60000; color: #fff; padding: 10px;}</style> <div class="wrp"> <div class="box"> <div class="box2"> sssssfdsfd </div> </div></div>
Use two divs with variable width and height to embed each other. The peripheral divs are positioned with absolute, left:50%, top:50%. In this way, the upper left corner of the peripheral div is horizontally and vertically centered. The internal div is positioned relative, so that it can use its own width and height to expand the surrounding div, and then use left:-50%, top:-50%; (margin-left, margin-top can also be used, that is, using the value The principle is to refer to the width and height of the peripheral div) to achieve that the geometric center of the internal div coincides with the upper left corner of the peripheral div. This achieves vertical and horizontal play. This principle is a bit like using translate(-50%,-50%) to achieve horizontal and vertical centering, but I use css1.0 code, so it is very compatible with IE6.0~IE11 chrome and firefox
Data reference:
Unknown size elements are aligned horizontally and vertically http://demo.doyoe.com/css/alignment/