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

JavaScript manual implementation of instanceof method

1. Usage of instanceof instanceof operator is use...

A brief discussion on the use of React.FC and React.Component

Table of contents 1. React.FC<> 2. class xx...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

Solution to MySQL Installer is running in Community mode

Today I found this prompt when I was running and ...

vue-pdf realizes online file preview

This article example shares the specific code of ...

How to configure anti-hotlinking for nginx website service (recommended)

1. Principle of Hotlinking 1.1 Web page preparati...

Detailed steps for deploying Microsoft Sql Server with Docker

Table of contents 1 Background 2 Create a contain...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

JavaScript to achieve product query function

This article example shares the specific code of ...

Installation method of MySQL 5.7.18 decompressed version under Win7x64

Related reading: Solve the problem that the servi...

Automatically build and deploy using Docker+Jenkins

This article introduces Docker+Jenkins automatic ...

Build a WebRTC video chat in 5 minutes

In the previous article, I introduced the detaile...