7 ways to center containers horizontally and vertically with css
This article mainly introduces in detail the 7 ways of using CSS to center the container horizontally and vertically. It has certain reference value. Interested friends can refer to
This kind of CSS layout is usually used. There are quite a lot of them, and it is also a question that often comes up in interviews. You will find a lot of them after searching online, but I still want to summarize them myself.
There are many methods of this kind. This article only summarizes a few of them to deepen the impression.
The renderings are all like this:
Method 1: position plus margin
# #XML/HTML CodeCopy content to clipboard
- ##p class ="wrap"> ##
p>
p class="center">p>
##CSS Code
## /**css**/
.wrap {- width
- :
200px; height: 200px; background : yellow; position: relative; ##} .wrap .center {
width : - 100px
; height: 100px; background: green; margin: auto; position: absolute; left: 0; rightright: 0; top: 0; bottombottom: 0; }
Compatibility: supported by all major browsers, but not supported by IE6 - Method 2: diaplay:table-cell
XML/HTML Code
Copy content to clipboard
-
p
- class
="wrap">
p - class
= "center">p>
p - >
- CSS Code
Copy content to clipboard
/*&/ .wrap{
width- :
- 200px
; height: 200px; background: yellow ; display: table-cell; vertical-align: middle; text-align: center;
} .center{ display: inline-block; vertical-align: middle; width: 100px; height: 100px; background : green;
}
Compatibility: Due The reason for display:table-cell is that IE67 is not compatible
Method three: position plus transform
XML/HTML CodeCopy content to clipboard
#
- ##p class="wrap"> ##
- p
>
p class="center"> p>
##
Copy content to clipboard
##/* css */ .wrap {
position- :
- relative
; background: yellow; width: 200px; height: 200px;} .center { position: absolute; background: green; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width: 100px; height: 100px; }
- Compatibility: Transform is not supported under IE9 and below, and the mobile version performs better.
- Method 4: flex;align-items: center;justify-content: center
##XML/HTML Code
Copy content to clipboard#
- ##
-
> ## p class=
"center" - >
p> ## p>
-
##CSS Code
Copy content to clipboard -
/* css */ .wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify -content: center;
} .center { background: green; width: 100px; height: 100px;
}
Mobile preferred
Method five: display :flex;margin:auto
XML/HTML CodeCopy content to clipboard
##
- ##p class="wrap"> ##
p class="center">p> ;
## - p
>
p class
="wrap"CSS Code
/* css */
- .wrap {
- background
: yellow; width: 200px; height: 200px ; display: flex; } .
center { - background
: green; width: 100px; height: 100px; margin: auto; }
Preferred method for mobile terminal
Pure position
XML/HTML Code
##
-
##
p - class
="wrap"> ##p
class - =
"center">p > ##p
> CSS Code
Copy content to clipboard
/* css */ .wrap { background: yellow; width: 200px; height: 200px; position: relative;
} /**method one**/ .center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;
} /** Method Two**/ .center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;
-
}
Compatibility: Applicable to all browsers
Calculation formula for method one in method six As follows:
The left value calculation formula of the child element (conter): left=(width of the parent element - width of the child element) / 2=(200-100) / 2=50px;
Child element (conter) The top value calculation formula: top=(height of parent element - height of child element) / 2=(200-100) / 2=50px;
Method 2 calculation formula:
The left value is fixed at 50%;
The margin-left of the child element= -(width of the child element/2)=-100/2= -50px;
The top value is the same, fixed at 50%
The margin-top= of the child element -(Height of child element/2)=-100/2= -50px;
Method 7:Compatible with lower version browsers, no fixed width and height
XML/HTML CodeCopy content to clipboard
p class="table">
-
p class= "tableCell"> #=
"content" - >
No fixed width and height, adaptivep> ## p>
p>
##CSS Code
Copy content to clipboard
.table {
height
200px;/*The height value cannot be less*/ width
:- 200px
- ;
/ *The width value cannot be less*/ display: table; position: relative; float:left; background: yellow;
-
} .tableCell { display: table-cell; vertical-align: middle; text-align: center;
: 10px - ;
*top: 50%;
* left - : 50%;
} .
content - {
*
position - :
relative;
- *
top: -50%;
* left - : -50%;
background: green
; }
- To summarize the above seven methods for now, there are too many methods. In fact, as long as you are used to one or two of them, it will be enough. Summary
- If it is a mobile terminal, then it is more convenient to use methods four and five. It also supports non-fixed width and height, which is fast, accurate and ruthless That is, use flex; align-items: center; justify-content: center;
##XML/HTML Code
Copy content to clipboard
#
##
p- class
="wrap"
> ## p class="center">
p> ## p>
##CSS Code Copy content to clipboard
-
##/* css */
.wrap { background
yellow
;
: 200px; height:
200px- ;
- display
: flex; align- items: center; justify-content: center; ##} .center { background: green; width: 100px; height: 100px
; -
} Or display:flex;margin:auto;
XML/HTML Code - Copy content to clipboard
##
pclass="wrap"
>- p
class ="center">p
> p>
##/* css */ .wrap { background: yellow; width: 200px; height: 200px; display : flex;
##} .- center
{ background: green; width: 100px; height: 100px; margin: auto;
} - If it is a PC version, compatibility must be considered. Method six is good, that is, pure position.
-
-
p>
- .wrap {
background: yellow; width: 200px; height: 200px ; position: relative; ##}
/**method one**/ . - center
{ background: green; position: absolute; width: 100px ; height: 100px; left: 50px; top: 50px;
-
}
/**Method Two**/ . - center
{ background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:- 50px; margin-top:-50px; }
If the height of the middle element on the PC side is not fixed, then just use method seven, and the code will not be copied.
If we really want to sum up the vertical css elements, there should be more than ten or twenty types. However, it is not necessary to master them all, as long as you have a general understanding and there are no side effects when using them.
CSS CodeCopy content to clipboard
Copy content to clipboard
p class="wrap">
p class ="center">p>
CSS Code
Copy content to clipboard
- /* css */
The above is the detailed content of 7 ways to center containers horizontally and vertically with css. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version
