CSS Vertical and Horizontal Full Centering Manual
Centering has always been a typical complaint in CSS. Why is it so hard to implement? So someone was laughed at. I think the problem is not that there is no way to do it, it just depends on the situation. There are many different ways, but it is difficult to figure out which method should be used.
So I wrote this article, hoping to make it easier for him.
Horizontal centering
Center the inline element (inline or inline-*)?
You can center it relative to the parent block-level element
.center-children { text-align: center; }
Block level element (block level) is centered?
You can center it by setting margin-left and margin-right to auto (and also set width, otherwise it will fill the entire container and cannot be seen) out of the centering effect), such as.
.center-me { margin: 0 auto; }
What if there are many block-level elements?
If you have very even block-level elements that need to be centered horizontally in a row, you're better off using a different display type. Here is an example using inline-block and flex.
<main class="inline-block-center"> <div> I'm an element that is block-like with my siblings and we're centered in a row. </div> <div> I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do. </div> <div> I'm an element that is block-like with my siblings and we're centered in a row. </div> </main> <main class="flex-center"> <div> I'm an element that is block-like with my siblings and we're centered in a row. </div> <div> I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do. </div> <div> I'm an element that is block-like with my siblings and we're centered in a row. </div> </main>
body { background: #f06d06; font-size: 80%; } main { background: white; margin: 20px 0; padding: 10px; } main div { background: black; color: white; padding: 15px; max-width: 125px; margin: 5px; } .inline-block-center { text-align: center; } .inline-block-center div { display: inline-block; text-align: left; } .flex-center { display: flex; justify-content: center; }
Vertical centering
Vertical centering is a bit tricky in CSS
Centering inline elements (inline or inline-*), like text and links?
Is it one line?
Sometimes elements can appear to be vertically centered just because they have equal top and bottom padding
.link { padding-top: 30px; padding-bottom: 30px; }
If padding doesn't work for some reason and the text doesn't wrap, you can Use line-height and make it equal to height to align the text.
.center-text-trick { height: 100px; line-height: 100px; white-space: nowrap; }
Is it multi-line?
Padding methods such as top and bottom can also center multiple lines, but if this method doesn’t work, you can make the containers of these texts display in table cell mode, and then set the text The vertical-align attribute aligns, just like talbe
<table> <tr> <td> I'm vertically centered multiple lines of text in a real table cell. </td> </tr> </table> <div class="center-table"> <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p> </div>
body { background: #f06d06; font-size: 80%; } table { background: white; width: 240px; border-collapse: separate; margin: 20px; height: 250px; } table td { background: black; color: white; padding: 20px; border: 10px solid white; /* default is vertical-align: middle; */ } .center-table { display: table; height: 250px; background: white; width: 240px; margin: 20px; } .center-table p { display: table-cell; margin: 0; background: black; color: white; padding: 20px; border: 10px solid white; vertical-align: middle; }
Block level elements (block level) are vertically centered?
Do you know the height of the element?
It is quite common not to know the height of a web page layout for many reasons.
But if your layout has a fixed height, you can center it vertically like this:
.parent { position: relative; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */ }
The height of the element is unknown
Although unknown, it is still possible for you to push the width up by 50%
.parent { position: relative; } .child { position: absolute; top: 50%; transform: translateY(-50%); }
Can you use flexbox?
This is not surprising, using flexbox is much easier
<main> <div> I'm a block-level element with an unknown height, centered vertically within my parent. </div> </main>
body { background: #f06d06; font-size: 80%; } main { background: white; height: 300px; width: 200px; padding: 20px; margin: 20px; display: flex; flex-direction: column; justify-content: center; resize: vertical; overflow: auto; } main div { background: black; color: white; padding: 20px; resize: vertical; overflow: auto; }
Center horizontally and vertically simultaneously
Elements have fixed width and height
If the width and height of the element are fixed, you need to center it absolutely first, then move it up and to the left by 50% The width is sufficient, and this solution has excellent cross-browser support.
.parent { position: relative; } .child { width: 300px; height: 100px; padding: 20px; position: absolute; top: 50%; left: 50%; margin: -70px 0 0 -170px; }
The width and height of the element are unknown
#If you don't know the height and width (variable), you can use the transofrm attribute in both directions Panning minus 50%
.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
For more CSS vertical and horizontal completely centered manual related articles, please pay attention to the PHP Chinese website!

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

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

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
