Achieve horizontal centering of div content
Implementation plan one: margin:0 auto;
div{ height:100px; width:100px; background:red; margin:0 auto; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>div水平居中</title> </head> <body> <div></div> </body> </html>
Solution 2 to achieve horizontal centering of div: position: absolute; absolute positioning
div{height:100px;width:100px;background:red;position:absolute; left:50%; right:50%; margin: auto; }
div{height:100px;width:100px;background:red;position:fixed;left:0;top:0;bottom:0;right:0;margin:auto; }
div{height:100px;width:100px;background:red;position:absolute;left:0;top:0;bottom:0;right:0;margin:auto; }
div{height:100px;width:100px;background:red;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%); }
-ms-transform:translate(-50%,-50%); /* IE 9 */-moz-transform:translate(-50%,-50%); /* Firefox */-webkit-transform:translate(-50%,-50%);/* Safari and Chrome */-o-transform:translate(-50%,-50%); /* Opera */
The above is the detailed content of Example code for horizontal centering of css+div. For more information, please follow other related articles on the PHP Chinese website!