Detailed explanation of several horizontal and vertical centering methods in HTML (Basics)

Detailed explanation of several horizontal and vertical centering methods in HTML (Basics)

Preface

When we were writing the horse, I guess everyone didn't know much about the methods of horizontal and vertical centering. So I will summarize for you several commonly used methods of horizontal and vertical centering.

First method

<!--html box code-->
<!--Horizontal and vertical center-->

<div class="Centered1">
    <p>dThe first type</p>
</div>

<!-css style part-->
   .Centered1{
            background-color: #800070;
            width: 100%;
            height:500px;
            position: relative;
        }
    .Centered1 p{
            width: 200px;
            height: 200px;
            background-color: deeppink;
            line-height: 200px;
            text-align: center;
            position: absolute;
            left: 0;
            bottom: 0;
            right:0;
            top: 0;
            margin: auto;
        }

Second method

<!--html box code-->
<!--Horizontal and vertical center-->

<div class="Centered2">
    <p>dThe second type</p>
</div>

<!-css style part-->
/*The second method is horizontal and vertical centering*/
    .Centered2{
            background-color: #ef8518;
            width: 100%;
            height: 500px;
            position: relative;
        }
    .Centered2 p {
            position: absolute;
            width: 200px;
            height: 200px;
            background-color:red;
            line-height: 200px;
            text-align: center;
            left: 50%;
            top:50%;
            margin-left:-100px;
            margin-top: -100px;
        }

The third method

<!--html box code-->
<!--Horizontal and vertical center-->

<div class="Centered3">
    <p>dThe third type</p>
</div>

<!-css style part-->
/*The third method is horizontal and vertical centering*/
    .Centered3{
            background-color: dimgrey;
            width: 100%;
            height: 500px;
            position: relative;
        }
    .Centered3 p {
            position: absolute;
            width: 200px;
            height: 200px;
            background-color:darkorange;
            line-height: 200px;
            text-align: center;
            left: 50%;
            top: 50%;
            transform:translate(-50%,-50%);
        }    

The fourth method

<!--html box code-->
<!--Horizontal and vertical center-->

<div class="Centered4">
    <p>dThe fourth type</p>
</div>

<!-css style part-->
/*The fourth method is horizontal and vertical centering, old version flex layout*/
    .Centered4{
            background-color: #FF4444;
            width: 100%;
            height: 500px;
            display: -webkit-box;
            -webkit-box-pack:center;
            -webkit-box-align: center;
        }
    .Centered4 p {
            width: 200px;
            height: 200px;
            background-color:cadetblue;
            line-height: 200px;
            text-align: center;
        }

The fifth method

<!--html box code-->
<!--Horizontal and vertical center-->

<div class="Centered5">
    <p>d Fifth type</p>
</div>

<!-css style part-->
/*The fifth method is to center horizontally and vertically. The new version of flex is to center horizontally and vertically*/
    .Centered5{
            background-color: darkslateblue;
            width: 100%;
            height: 500px;
            display: flex;
            justify-content:center;
            align-items: center;
        }
    .Centered5 p {
            width: 200px;
            height: 200px;
            background-color:fuchsia;
            line-height: 200px;
            text-align: center;
        }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Analysis of the cause of docker error Exited (1) 4 minutes ago

>>:  In-depth explanation of Mysql deadlock viewing and deadlock removal

Recommend

Introduction to CSS BEM Naming Standard (Recommended)

1 What is BEM Naming Standard Bem is the abbrevia...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

37 Tips for a Good User Interface Design (with Pictures)

1. Try to use single column instead of multi-colum...

Detailed explanation of various methods of Vue component communication

Table of contents 1. From father to son 2. From s...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Using Zabbix to monitor the operation process of Oracle table space

0. Overview Zabbix is ​​an extremely powerful ope...

Vue realizes click flip effect

Use vue to simply implement a click flip effect f...

WeChat applet realizes the effect of swiping left to delete list items

This article shares the specific code for WeChat ...

Docker builds python Flask+ nginx+uwsgi container

Install Nginx First pull the centos image docker ...

Example of implementing dynamic verification code on a page using JavaScript

introduction: Nowadays, many dynamic verification...

Example of using the href attribute and onclick event of a tag

The a tag is mainly used to implement page jump, ...

Complete steps to install mysql5.7 on Mac (with pictures and text)

I recently used a Mac system and was preparing to...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

4 Scanning Tools for the Linux Desktop

While the paperless world has not yet emerged, mo...