Discussion on horizontal and vertical centering of elements in HTML

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center the DIV, and center it horizontally and vertically relative to the page window, such as centering the login window.

So far, many methods have been explored.

HTML:

XML/HTML CodeCopy content to clipboard
  1. < body >   
  2.      < div   class = "maxbox" >   
  3.          < div   class = "minbox align-center" > </ div >   
  4.      </ div >   
  5. </ body >   
  6.   

Effect diagram (the following methods have the same effect diagram):

The first one: CSS absolute positioning

Mainly use absolute positioning, and then use margin to adjust to the middle position.

Parent element:

CSS CodeCopy content to clipboard
  1. .maxbox{
  2.      position : relative ;
  3.      width : 500px ;
  4.      height : 500px ;
  5.      margin : 5px ;
  6. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  7. }
  8.   

Child Elements:

CSS CodeCopy content to clipboard
  1. .minbox{
  2.      width : 200px ;
  3.      height : 200px ;
  4. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  5. }

Horizontal and vertical center alignment:

CSS CodeCopy content to clipboard
  1. .align- center {
  2.      position : absolute ;
  3.      left : 50%;
  4.      top : 50%;
  5.      margin-left : - 100px ; /*width/-2*/   
  6.      margin-top : - 100px ; /*height/-2*/   
  7. }

The second method: CSS absolute positioning + Javascript/JQuery

Mainly use absolute positioning, and then use Javascript/JQuery to adjust it to the middle position. Compared with the first method, this method improves the flexibility of the class.

Parent element:

CSS CodeCopy content to clipboard
  1. .maxbox{
  2.      position : relative ;
  3.      width : 500px ;
  4.      height : 500px ;
  5.      margin : 5px ;
  6. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  7. }
  8.   

Child Elements:

CSS CodeCopy content to clipboard
  1. .minbox{
  2.      width : 200px ;
  3.      height : 200px ;
  4. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  5. }

Horizontal and vertical center alignment:

CSS CodeCopy content to clipboard
  1. .align- center {
  2.      position : absolute ;
  3.      left : 50%;
  4.      top : 50%;
  5. }
  6.   

JQuery:

JavaScript CodeCopy content to clipboard
  1. $( function () {
  2. $( ".align-center" ).css(
  3. {
  4.              "margin-left" : ($( ".align-center" ).width()/-2),
  5.              "margin-top" : ($( ".align-center" ).height()/-2)
  6. }
  7. );
  8. });
  9.   

The third type: CSS3 absolute positioning + displacement

The same effect can be achieved by using absolute positioning and CSS3's transform: translate.

Parent element:

CSS CodeCopy content to clipboard
  1. .maxbox{
  2.      position : relative ;
  3.      width : 500px ;
  4.      height : 500px ;
  5.      margin : 5px ;
  6. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  7. }
  8.   

Child Elements:

CSS CodeCopy content to clipboard
  1. .minbox{
  2.      width : 200px ;
  3.      height : 200px ;
  4. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  5. }
  6.   

Horizontal and vertical center alignment:

CSS CodeCopy content to clipboard
  1. .align- center {
  2.      position : absolute ;
  3.      top : 50%;
  4.      left : 50%;
  5. -webkit-transform: translate(-50%, -50%);
  6. -moz-transform: translate(-50%, -50%);
  7. transform: translate(-50%, -50%); /*Move left and up*/   
  8. }
  9.   

Fourth: Flexbox: [Flexible Layout Box Model]

It's too easy to make elements horizontal and vertical with the Flexbox model.

Here we need to change the HTML:

XML/HTML CodeCopy content to clipboard
  1. < div   class = "maxbox align-center" >   
  2.      < div   class = "minbox" > </ div >   
  3. </ div >   
  4.   

Parent element:

CSS CodeCopy content to clipboard
  1. .maxbox{
  2.      position : relative ;
  3.      width : 500px ;
  4.      height : 500px ;
  5.      margin : 5px ;
  6. box-shadow: 1px   1px   1px rgba(0, 0, 0, 0.8), - 1px - 1px   1px rgba(0, 0, 0, 0.8);
  7. }
  8.   

Child Elements:

C# Code to copy content to the clipboard
  1. .minbox{
  2. width: 200px;
  3. height: 200px;
  4. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
  5. }

Horizontal and vertical center alignment:

CSS CodeCopy content to clipboard
  1. .align- center {
  2.      display : flex;
  3.      display : -webkit-flex; /*Compatibility issue*/   
  4.      justify - content : center ;
  5. align-items: center ;
  6. }

Comparison of several methods:

The first type of CSS absolute positioning margin adjustment has good compatibility but lacks flexibility. If there are many boxes that need to be centered horizontally and vertically, different .align-center should be written because of their different widths and heights.
The second one uses scripting language, which has good compatibility and makes up for the shortcomings of the first one. The horizontal and vertical centering effects are not affected by changes in width and height.
The third one uses some new properties of CSS3 and is compatible with IE10, Chrome, Firefox, and Opera. The compatibility is not very good, and the horizontal and vertical centering effects are not affected by changes in width and height.
Using the Flexbox model, it is compatible with Firefox, Opera and Chrome, but IE is completely defeated. The horizontal and vertical centering effects are not affected by changes in width and height.

The above is the full content of this article. I hope it will be helpful for everyone’s study.

<<:  CSS delivery address parallelogram line style example code

>>:  MySQL query statement grouped by time

Recommend

Pure CSS3 to achieve pet chicken example code

I have read a lot of knowledge and articles about...

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

How to use JavaScript strategy pattern to validate forms

Table of contents Overview Form validation withou...

Vue uses custom instructions to add watermarks to the bottom of the page

Project Scenario Add a custom watermark to the en...

How does the composite index of MySQL take effect?

Table of contents background Understanding compos...

Using MySQL in Windows: Implementing Automatic Scheduled Backups

1. Write a backup script rem auther:www.yumi-info...

Details on macrotasks and microtasks in JavaScript

Table of contents 1. What are microtasks? 2. What...

Solution to the gap between divs

When you use HTML div blocks and the middle of th...

Analysis of Linux kernel scheduler source code initialization

Table of contents 1. Introduction 2. Basic Concep...

Use Vue3+Vant component to implement App search history function (sample code)

I am currently developing a new app project. This...