Discussion on horizontal and vertical centering of elements in HTML_HTML/Xhtml_Webpage Production

WBOY
Release: 2016-05-16 16:36:04
Original
2087 people have browsed it

When we design the page, we often need to center the DIV, and display 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>

Rendering (the following methods have the same renderings):

The first type: 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. }

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. }

Second type: CSS absolute positioning Javascript/JQuery

Mainly use absolute positioning, and then use Javascript/JQuery to adjust 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. }

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. }

JQuery:

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

Using absolute positioning and CSS3 transform: translate can also achieve the same effect. Parent element:

CSS Code

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

CSS Code

Copy 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. top: 50%;
  4. left: 50%;
  5. -webkit-transform: translate(-50%, -50%);
  6. -moz-transform: translate(-50%, -50%);
  7. transform: translate(-50%, -50%); /*Shift left and up*/
  8. }

Fourth type: Flexbox: [Telescopic layout box model]

Making elements horizontal and vertical is too easy with the Flexbox model.

You need to change the HTML here:

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

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. }

Child elements:

C# 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. display: flex;
  3. display: -webkit-flex;  justify
  4. -
  5. content: center; align-items: center
  6. ;
  7. }
  8. Comparison of several methods:
  9. The first 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 needs to be written because of their different width and height.
The second one uses scripting language, which has good compatibility and makes up for the shortcomings of the first one. The effect of horizontal and vertical centering will not be affected by changes in width and height.
The third type 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 effect will not be affected by changes in width and height.
Using the Flexbox model, compatible with Firefox, Opera and Chrome, IE is completely wiped out. It also does not affect the horizontal and vertical centering effect due to changes in width and height.

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!