Several commonly used methods for centering CSS boxes (summary)

Several commonly used methods for centering CSS boxes (summary)

The first one:

Using the CSS position property

<style type="text/css">
        .div1 {
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
            position: relative;
        }

        .div2 {
            width: 40px;
            height: 40px;
            background-color: red;
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }
    </style>

    <div class="div1">
        <div class="div2">
        </div>
    </div>

Effect picture:

Second type:

Use the new CSS3 attributes table-cell, vertical-align:middle;

<style type="text/css">
        .div1 {
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
            display: table-cell;
            vertical-align: middle;
        }

        .div2 {
            width: 40px;
            height: 40px;
            background-color: red;
            margin: auto;
        }

    </style>

    <div class="div1">
        <div class="div2">
        </div>
    </div>

Effect:

The third type:

Layout using flexbox

<style type="text/css">
    .div1 {
        width: 100px;
        height: 100px;
        border: 1px solid #000000;
        display: flex;
        /*!*flex-direction: column;*!Optional*/
        justify-content: center;
        align-items: center;
    }

    .div2 {
        height: 40px;
        width: 40px;
        background-color: red;
    }
</style>
<div class="div1">
    <div class="div2">
    </div>
</div>

Effect:

The fourth type:

Using the transform attribute (disadvantage: need to support Html5)

<style type="text/css">
    .div1 {
        width: 100px;
        height: 100px;
        border: 1px solid #000000;
        position: relative;
    }

    .div2 {
        height: 40px;
        width: 40px;
        background-color: red;
        position: absolute;
        top: 50%;
        left: 50%;
        -ms-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        -o-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
    }
</style>
<div class="div1">
    <div class="div2">
    </div>
</div>

Effect picture:

This concludes this article on several commonly used methods (summary) for centering a CSS box. For more information on centering a CSS box, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of HTML page header code example

>>:  Solution to high CPU usage of Tomcat process

Recommend

Solve the cross-domain problem of get and post requests of vue $http

Vue $http get and post request cross-domain probl...

Tutorial on setting up scheduled tasks to backup the Oracle database under Linux

1. Check the character set of the database The ch...

How to clean up the disk space occupied by Docker

Docker takes up a lot of space. Whenever we run c...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

Four ways to switch tab pages in VUE

Table of contents 1. Static implementation method...

mysql-canal-rabbitmq installation and deployment super detailed tutorial

Table of contents 1.1. Enable MySQL binlog 1.2. C...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

xtrabackup backup and restore MySQL database

Due to some of its own characteristics (locking t...

How to view Docker container application logs

docker attach command docker attach [options] 容器w...

Steps to create a WEBSERVER using NODE.JS

Table of contents What is nodejs Install NodeJS H...

How to start the spring-boot project using the built-in linux system in win10

1. Install the built-in Linux subsystem of win10 ...