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.
XML/HTML CodeCopy content to clipboard
- <body>
-
<div class="maxbox" >
-
<div class="minbox align -center">div>
-
div>
-
body>
-
Mainly use absolute positioning, and then use margin to adjust to the middle position.
CSS CodeCopy content to clipboard
- .maxbox{
-
position: relative;
-
width: 500px;
-
height: 500px;
-
margin: 5px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
-
CSS CodeCopy content to clipboard
- .minbox{
-
width: 200px;
-
height: 200px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
CSS CodeCopy content to clipboard
- .align-center{
-
position: absolute;
-
left: 50%;
-
top: 50%;
-
margin-left: -100px;
-
margin-top: -100px;
- }
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.
CSS CodeCopy content to clipboard
- .maxbox{
-
position: relative;
-
width: 500px;
-
height: 500px;
-
margin: 5px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
-
CSS CodeCopy content to clipboard
- .minbox{
-
width: 200px;
-
height: 200px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
CSS CodeCopy content to clipboard
- .align-center{
-
position: absolute;
-
left: 50%;
-
top: 50%;
- }
-
JavaScript CodeCopy content to clipboard
- $(function(){
-
$(".align-center").css(
-
-
"margin-left": ($(".align-center").width() /-2),
-
"margin-top": ($(".align-center").height() /-2)
- );
- });
-
-
Third type: CSS3 absolute positioning displacement
Using absolute positioning and CSS3 transform: translate can also achieve the same effect.
Parent element:
Copy content to clipboard
.maxbox{
-
position-
: relative;
width-
: 500px;
height-
: 500px;
margin-
: 5px;
box-shadow:
1px-
1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
}
-
-
Child elements:
Copy content to clipboard
- .minbox{
-
width: 200px;
-
height: 200px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
-
Horizontal and vertical center alignment:
CSS CodeCopy content to clipboard
- .align-center{
-
position: absolute;
-
top: 50%;
-
left: 50%;
- -webkit-transform: translate(-50%, -50%);
- -moz-transform: translate(-50%, -50%);
-
transform: translate(-50%, -50%);
- }
-
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
- <div class="maxbox align-center" >
-
<div class="minbox" >div>
-
div>
-
Parent element:
CSS CodeCopy content to clipboard
- .maxbox{
-
position: relative;
-
width: 500px;
-
height: 500px;
-
margin: 5px;
-
box-shadow: 1px 1px 1px rgba (0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
-
Child elements:
C# CodeCopy content to clipboard
- .minbox{
- width: 200px;
- height: 200px;
- box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8);
- }
Horizontal and vertical center alignment:
CSS CodeCopy content to clipboard
- .align-center{
-
display: flex;
-
display: -webkit-flex;
justify
--
content: center;
align-items: center
; -
}
-
Comparison of several methods:
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.