CSS solution for centering elements with variable width and height

CSS solution for centering elements with variable width and height

1. Horizontal center

Public code:

html:

<div class="parent">
    parent
    <br>
    <div class="child">
        child
    </div>
</div>

css:

html, body {
    margin: 0;
    width: 100%;
    height: 100%;

    .parent {
        width: 100%;
        height: 100%;
        background: #fac3fa;
        
        .child {
            width: 50%;
            height: 50%;
            background: #fe9d38;
        }
    }
}

Solution 1: text-align (parent) + inline-block (child)

Code:
css:

.parent {
    text-align: center;

    .child {
        display: inline-block;
    }
}

Solution 2: Block-level elements + margin: 0 auto;

.child {
    display: block; // Set margin: 0 auto for non-block-level elements;
}

Solution 3: absolute + transform

.parent {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

Option 4: flex

Note: Due to the use of flex, parent and <br> are removed here

.parent {
    display: flex;
    justify-content: center;
}

2. Vertical centering

Public code:
html:

<div class="parent">
    <div class="child"></div>
</div>

css:

html, body {
    margin: 0;
    width: 100%;
    height: 100%;
}
.parent {
    display: table-cell;
    width: 800px;
    height: 500px;
    background: #fac3fa;

    .child {
        width: 50%;
        height: 50%;
        background: #fe9d38;
    }
}

Solution 1: table-cell (parent) + vertical-align (child)

.parent {
    display: table-cell;
    vertical-align: middle;
}

Solution 2: absolute + transform

.parent {
    position: relative;
    
    .child {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }
}

Option 3: flex

.parent {
    display: flex;
    align-items: center;
}

3. Horizontal and vertical centering

Public code is the same as [vertical center]

Common solution 1: absolute + transform

.parent {
    position: relative;
    
    .child {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
}

Common solution 2: flex

.parent {
    display: flex;
    align-items: center;
    justify-content: 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.

<<:  Detailed explanation of XML syntax

>>:  Examples of using && and || operators in javascript

Recommend

Mysql sorting and paging (order by & limit) and existing pitfalls

Sorting query (order by) In e-commerce: We want t...

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...

Introduction to the usage of props in Vue

Preface: In Vue, props can be used to connect ori...

js canvas realizes circular water animation

This article example shares the specific code of ...

Let's learn about the MySQL storage engine

Table of contents Preface 1. MySQL main storage e...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

How to use jsonp in vue

Table of contents 1. Introduction 2. Installation...

Using HTML+CSS to track mouse movement

As users become more privacy-conscious and take m...

mysql-8.0.17-winx64 deployment method

1. Download mysql-8.0.17-winx64 from the official...

MySQL green version setting code and 1067 error details

MySQL green version setting code, and 1067 error ...

MySQL isolation level detailed explanation and examples

Table of contents 4 isolation levels of MySQL Cre...

XHTML: Frame structure tag

Frame structure tag <frameset></frameset...

Vue components dynamic components detailed explanation

Table of contents Summarize Summarize When the ar...

Detailed explanation of JavaScript BOM composition and common events

Table of contents 1. BOM 2. Composition of BOM 2....