How to achieve centered layout in CSS layout

How to achieve centered layout in CSS layout

1. Set the parent container to a table and the child to an inline element.

Suitable for sub-content to display text.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: table-cell; /* Transform into a table */
        text-align: center; /* horizontal */
        vertical-align: middle; /* vertical */
    }
    #child {
        background-color: blue;
        color: white;

        display: inline; /* Child elements are set to inline or inline block*/
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child">Content</div>
</div>

2. Set relative positioning for the parent container and absolute positioning for the child container, and then center it using margins.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative; /* Set relative positioning */
    }
    #child {
        height: 50px;
        width: 50px;
        color: white;
        background-color: blue;

        /* Absolute positioning, after setting the 4 directions to 0, the margin is set to auto */
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

3. The parent container is set to a flexible box and the child sets the margins.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: flex; /* Convert the parent element to a flexible box*/
        display: -webkit-flex; /* Safari */
    }
    #child {
        height: 50px;
        width: 50px;
        background-color: blue;

        margin: auto;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

4. Set relative positioning for the parent container, absolute positioning for the child, and negative half width for the left and top margins.

Suitable for situations where the width and height of the child are fixed.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative; /* Set relative positioning */
    }
    #child { /* When the child element knows its own width and height*/
        background-color: blue;

        width: 50px;
        height: 50px;
        margin-top: -25px;
        margin-left: -25px;
        position: absolute;
        left: 50%;
        top: 50%;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

5. Set relative positioning for the parent container and absolute positioning for the child container, and use the transformation attribute to set the horizontal and vertical directions to negative half.

Suitable for situations where the width and height of the child are not fixed.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative; /* Set relative positioning */
    }
    #child { /* Child element does not know its own width and height, use transform's translate */
        border: 1px solid blue;

        position: absolute;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%,-50%);
        -ms-transform: translate(-50%,-50%);
        -o-transform: translate(-50%,-50%);
        transform: translate(-50%,-50%);
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child">
        <div id="content">
            Content 1
            <br/>
            Content 2
        </div>
    </div>
</div>

6. Set the parent to a flexible box and set the alignment properties.

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: flex; /* Convert the parent element to a flexible box*/
        display: -webkit-flex; /* Safari */
        justify-content: center;/* horizontal */
        align-items: center; /* vertical */
    }
    #child {
        height: 50px;
        width: 50px;
        background-color: blue;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

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.

<<:  3 simple ways to achieve carousel effects with JS

>>:  Tips on HTML formatting and long files for web design

Recommend

How to completely uninstall mysql under CentOS

This article records the complete uninstallation ...

Example of how to implement local fuzzy search function in front-end JavaScript

Table of contents 1. Project Prospects 2. Knowled...

Installation and use of Apache stress testing tools

1. Download Go to the Apache official website htt...

Example code for implementing 3D Rubik's Cube with CSS

Let's make a simple 3D Rubik's Cube today...

...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...

MySQL 8.0.16 installation and configuration tutorial under Windows 10

This article shares with you the graphic tutorial...

Vue echarts realizes dynamic display of bar chart

This article shares the specific code of vue echa...

Several ways to generate unique IDs in JavaScript

Possible solutions 1. Math.random generates rando...

Detailed process of installing Presto and connecting Hive in Docker

1. Introduction Presto is an open source distribu...