Table of Contents
Preface
Three ways to achieve vertical and horizontal centering with CSS
1. Element display in the container: inline/inline-block
2. Inside the container The element display:block, and the width and height of the element are known
3. The element in the container is display:block, and the width and height of the element are unknown
Afterword
Home Web Front-end CSS Tutorial How to achieve vertical and horizontal centering with CSS

How to achieve vertical and horizontal centering with CSS

Jun 12, 2018 pm 03:34 PM

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>
Copy after login
    .container{    
        text-align: center;        
        height: 50px;        
        background: green;        
        line-height: 50px;    
        }
Copy after login

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>
Copy after login
    .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;    
     }
Copy after login

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>
Copy after login
    .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;    
       }
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;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.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

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&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

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.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

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

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

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

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

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...

See all articles