I have encountered many centering problems recently, so I took some time to summarize them and put them here so that it will be easy to find them later. 1. Center text Copy code The code is as follows:<div class="wrap"> I'm in the middle... </div> .. height+line-height+text-center (can only center a single line) .wrap{ width:px; height:px; border:px solid red; text-align: center; line-height: px; } ps:text-align:center only centers the inline elements below the element 1.2display:table-cell (multiple rows with fixed height centered) Copy code The code is as follows:.wrap{ width:px; height:px; border:px solid red; text-align: center; display:table-cell; vertical-align: middle; } display:table-cell: does not work in ie67, it is best to use it with display:table; Under ie67: (I won't use it anymore, but I'll put it here) Method 1: (The em tag height is the same as the parent, so span and em centering is equivalent to span centering the parent) Copy code The code is as follows:<div class="wrap"> <span> I'm in the middle... I'm in the middle... I'm in the middle... </span> <em> </div> .wrap{ width:px; height:px; border:px solid red; text-align: center; } .wrap span{ vertical-align: middle; display:inline-block; width:px; } .wrap em{ height:%; vertical-align: middle; display:inline-block; } Method 2: (By adding an absolutely positioned parent tag to the child element, and then using relative positioning of the child element to center it horizontally and vertically) Copy code The code is as follows:<div class="wrap"> <span class="span"> <span class="span">I'm in the middle... I'm in the middle... I'm in the middle... I'm in the middle...</span> </span> </div> .wrap{ width:px; height:px; border:px solid red; display:table; position:relative; overflow: hidden; } .wrap .span{ display:table-cell; vertical-align: middle; text-align: center; *position:absolute; top:%; left:%; } .wrap .span{ *position:relative; top:-%; left:-%; } 1.3padding (internal padding, needless to say) Copy code The code is as follows:.wrap{ width:px; border:px solid red; padding:px; } 2. Centering Elements Copy code The code is as follows:<div class="wrap"> <span></span> </div> 2.1position:absolute+margin negative value (width and height are required to calculate margin) Copy code The code is as follows:.wrap{ width:px; height:px; position:absolute; top:%; left:%; margin-top:-px; margin-left:-px; border:px solid red; } .wrap span{ width:px; height:px; background:red; position: absolute; top:%; left:%; margin-top:-px; margin-left:-px; } ps: CSS realizes DIV horizontal centering and vertical centering Copy code The code is as follows:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Vertical centering online demonstration DIVCSS5</title> <style> #main { position: absolute; width:400px; height:200px; left:50%; top:50%; margin-left:-200px; margin-top:-100px; border:1px solid #00F } /*css comment: For the convenience of screenshots, wrap the CSS code*/ </style> </head> <body> <div id="main">DIV horizontal center and vertical center<a href="http://www.divcss5.com/">DIVCSS5</a></div> </body> </html> Introduction to the principles of horizontal and vertical centering <br />Absolute positioning position:absolute is used here, and left and top are used to set the object's distance from the top and left to 50%. However, if it is set to 50%, the box is actually not centered, so margin-left:-200px;margin-top:-100px; are set. Here is a trick: the value of margin-left is half of the width, and the value of margin-top is also half of the object height. At the same time, they are set to negative, so that horizontal and vertical centering is achieved. |
<<: CSS to achieve the image hovering mouse folding effect
>>: Docker installation and configuration steps for Redis image
Table of contents 1. Click on the menu to jump 1....
Table of contents Overview Button-level permissio...
Unzip the Maven package tar xf apache-maven-3.5.4...
1. Introduction to Macvlan Before the emergence o...
Before I start, let me emphasize that process.env...
Table of contents background: Nginx smooth upgrad...
I wonder if you have ever thought about this ques...
Table of contents 1.DB,DBMS,SQL 2. Characteristic...
This article example shares the specific code for...
This article example shares the specific code of ...
environment Linux 3.10.0-693.el7.x86_64 Docker ve...
Table of contents 1. Usage 2. Output results 1.id...
Preface A reverse proxy is a server that receives...
question: I have a form in Vue for uploading blog...
win10 + Ubuntu 20.04 LTS dual system installation...