How to achieve vertical and horizontal centering with CSS
This article mainly introduces how to achieve vertical and horizontal centering in CSS, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Preface
In the interview We are often asked how to use CSS to center an element vertically and horizontally. Especially when it comes to written test questions, this question appears more frequently. Of course, in our lives, there are often vertical Horizontal centering requirements.
Three ways to achieve vertical and horizontal centering with CSS
1. Element display in the container: inline/inline-block
In this case, it is easier, just set the container directly Text-align can achieve horizontal centering of content elements. To set vertical centering, you need to set the height of the container, and then set the easy line-height===height, as follows:
<p class="container"> <span>this is text</span> </p>
.container{ text-align: center; height: 50px; background: green; line-height: 50px; }
2. Inside the container The element display:block, and the width and height of the element are known
In this case, we use the position attribute combined with setting the offset to achieve this. First set the position of the container: relative, set the element position to absolute, and then set the offset of the element (.inner-box) top, left, margin-top, margin-left, where top and left are set to 50%, and margin The offset of -top/margin-left is half of the height/width of the element itself, which is a negative value.
The code is as follows:
<p class="container"> <p class="inner-box"></p> </p>
.container { height: 200px; width: 200px; background: pink; position: relative; } .inner-box { position: absolute; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; height: 100px; width: 100px; background: green; }
3. The element in the container is display:block, and the width and height of the element are unknown
This method is similar to method two, but the difference is that it cannot pass Set the margin-top/left offset to achieve the effect, because the width and height of the elements in the container are unknown. This time we set left/top/bottom/right:0, and then set margin:auto.
The code is as follows:
<p class="container"> <p class="inner-box"></p> </p>
.container { height: 200px; width: 200px; background: pink; position: relative; } .inner-box { position: absolute; height: 100px; width: 100px; top: 0; right: 0; left: 0; bottom: 0; margin: auto; background: green; }
Afterword
There are many ways to achieve vertical and horizontal centering. It is also possible to set translate or use flex layout, but the methods written above It has better compatibility. If there are any deficiencies, please feel free to point them out.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Use CSS to achieve various centering methods
The above is the detailed content of How to achieve vertical and horizontal centering with CSS. For more information, please follow other related articles on 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

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
