Example of horizontal and vertical centering of div sub-element using CSS

Example of horizontal and vertical centering of div sub-element using CSS

Div basic layout

<div class="main">
   <div class="center"></div>
  </div>

CSS Styles

1. Coordinate positioning and margin: auto

Add relative positioning to the parent element and absolute positioning to the child element

 .main{
    width: 300px;
    height: 300px;
    background-color: red;
    position: relative;
   }
   .center{
    width: 100px;
    height: 100px;
    background-color: skyblue;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
   }

2. Use flex layout to center the content horizontally and vertically.

 .main{
    width: 300px;
    height: 300px;
    background-color: red;
    display: flex;
    justify-content: center;
    align-items: center;
   }
   .center{
    width: 100px;
    height: 100px;
    background-color: greenyellow;
   } 

3. Use position:absolute and transform

: What needs to be remembered here is that when the percentage is used in the transform, it is relative to its own length and width, not the parent box.

 .main{
     width: 300px;
     height: 300px;
     background-color: red;
     position: relative;
    }
    .center{
     width: 100px;
     height: 100px;
     background-color: pink;
     position: absolute;
     left: 50%;
     top: 50%;
     transform: translateX(-50%) translateY(-50%);
    }

4. Positioning and negative margin

Only suitable for cases where the length and width of the subbox are fixed

 .main{
     width: 300px;
     height: 300px;
     background-color: red;
     position: relative;
    }
    .center{
     width: 100px;
     height: 100px;
     background-color: pink;
     position: absolute;
     left: 50%;
     top: 50%;
     margin-left: -50px;
     margin-top: -50px;
    } 

5.display:table-cell

display:table-cell; and vertical-align:middle are used to center the child box in the numerical direction.

margin:auto; centers the child box horizontally. If you only want to center the box in one direction, just remove the other one.

.main{
     width: 300px;
     height: 300px;
     background-color: red;
     display: table-cell;
     vertical-align: middle;
    }
    .center{
     width: 100px;
     height: 100px;
     background-color: #000;
     margin: auto;
    }

This is the end of this article about examples of how to use CSS to horizontally and vertically center sub-elements div. For more information about how to horizontally and vertically center sub-elements div with CSS, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  VMware implements the detailed process of PXE+kickstart unattended installation of Centos7 system

>>:  Implementation of MySQL scheduled database backup (full database backup)

Recommend

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

Discuss the application of mixin in Vue

Mixins provide a very flexible way to distribute ...

Teach you how to deploy Vue project with Docker

1.Write in front: As a lightweight virtualization...

How to implement Ajax concurrent request control based on JS

Table of contents Preface Ajax serial and paralle...

Comparing Node.js and Deno

Table of contents Preface What is Deno? Compariso...

Tutorial on installing Elasticsearch 7.6.2 in Docker

Install Docker You have to install Docker, no fur...

MySQL5.6.31 winx64.zip installation and configuration tutorial

#1. Download # #2. Unzip to local and modify nece...

Reflection and Proxy in Front-end JavaScript

Table of contents 1. What is reflection? 2. Refle...

Summary of Mysql update multi-table joint update method

Next, I will create two tables and execute a seri...

Detailed explanation of Vue two-way binding

Table of contents 1. Two-way binding 2. Will the ...

Several ways to generate unique IDs in JavaScript

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

Several ways to solve CSS style conflicts (summary)

1. Refine the selector By using combinators, the ...