


Variable width and height vertical centering analysis_html/css_WEB-ITnose
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/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati
