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

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

Will Update in a Mysql transaction lock the table?

Two cases: 1. With index 2. Without index Prerequ...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

Nodejs plug-in and usage summary

The operating environment of this tutorial: Windo...

Detailed explanation of Angular component projection

Table of contents Overview 1. Simple Example 1. U...

Solve the problem of blocking positioning DDL in MySQL 5.7

In the previous article "MySQL table structu...

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

Ubuntu 15.04 opens mysql remote port 3306

Ubuntu 15.04 opens MySQL remote port 3306. All th...

Vue2 cube-ui time selector detailed explanation

Table of contents Preface 1. Demand and Effect ne...

How to completely uninstall iis7 web and ftp services in win7

After I set up the PHP development environment on...

A complete tutorial on using axios encapsulation in vue

Preface Nowadays, in projects, the Axios library ...

js handles account logout when closing the browser

Table of contents Classic approach question Furth...

Solution to the problem of large font size on iPhone devices in wap pages

If you don't want to use javascript control, t...

5 Easy Ways to Free Up Space on Ubuntu

Preface Most people will probably perform this op...

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...