Article Directory
Preface
Earlier I summarized an article about several horizontal and vertical centering methods. It can be known that the methods to achieve horizontal and vertical centering are really countless, but each has its own advantages and disadvantages. I want to apply them in practice. In order to use these methods correctly, it is necessary to rely on usual accumulation, and to be familiar with a few of them can deal with most of the centering problems. Recently, I have learned several magical methods, so let’s share them.
text
Horizontal centering method
text-align
+display:inline-block
Use with attributes.table
+margin
Use with attributesabsolute
+transform
Use with attributes
Method one, text-align
+display:inline-block
#parent{
text-align: center;}
#child{
display: inline-block;}
We first create a normal style
<style type="text/css">
#parent{
width: 100%;
height: 200px;
background: pink;
}
#child{
width: 200px;
height: 200px;
background: #987;
}
</style>
<div id="parent">
<div id="child">
水平居中
</div>
</div>
The effect displayed at this time is like this.
Let’s add two attributes to the style.
#parent{
width: 100%;
height: 200px;
background: pink;
text-align: center;
}
#child{
width: 200px;
height: 200px;
background: #987;
display: inline-block;
}
<div id="parent">
<div id="child">
水平居中
</div>
</div>
At this time, the level is centered.
Let's analyze, why use these two attributes? Why use inline-block
it instead of inline
it?
text-align
Attribute: is to set the alignment for the text content
left
: left alignment
center
: center alignment
right
: right alignment
Then you will have questions at this time? The initial knowledge of this attribute works on the text, so why does itdiv
also work on the elements at this time ? The key is that thedisplay
attribute and value are set for the child elementinline-block
.
display
Attributes have several common attributes::
block
block-level elements
inline
: inline elements
width
and height
attributes will be invalid
inline-block
: inline block-level elements (block-level + inline)
width
and height
attributes are valid
When the child element is an inline element, it text-align
will work.
inline-block
Using the characteristics of inline elements,
why not set the display property of the child element to inline
it? Because inline elements have their own problems, when we inline an element's inline element, chlid
the width
sum height
attribute will be invalid. You can try inline
the effect.
So we use inline-block
it to make child
the properties width
and height
properties effective, in order to achieve the effect we want.
Advantages and disadvantages
- Advantages: The browser compatibility is relatively good, for IE browser version 6-9,
text-align
andinline-block
both are supported. - Disadvantages: The
text-align
attributes are inherited, which causes the text of the child elements to be displayed in the center. (Solved, restoretext-align
the attributes in the child element )
If the text of the child element does not need to be centered, it must be processed. text-align
The value set in the child element left
can override the text-align
attribute value of the parent element .
Method two, use with table
+ margin
attributes
.child{
dispaly: table;
margin: 0 auto;
}
Add key css content in the normal style of the first method
#parent{
width: 100%;
height: 200px;
background: pink;
}
#child{
width: 200px;
height: 200px;
background: #987;
display: table;
margin: 0 auto;
}
<div id="parent">
<div id="child">
水平居中
</div>
</div>
The same effect
analysis is achieved
margin
:
- Set to a value, which means that the upper right, lower left are the same.
- Set to two values, the first means up and down, and the second means left and right.
- Set to three values, the first represents up, the second represents left and right, and the third represents down.
- Set to four values, upper right, lower left.
Provided margin: 0 auto;
where auto
indicated is automatically assigned according to the browser. So the browser will divide the left and right margins equally
display: table;
:
The same effect can be obtained by display
setting it to block
, but the inline style inline
will child
invalidate the width and height. Therefore, it display
can be set to two values: table
, block
.
Advantages and disadvantages
- Advantages: only need to set the child elements to realize the horizontal display in the center
- Disadvantage:
If the child element leaves the document flow, the value of the margin attribute becomes invalid.
The so-called element is out of the document flow, the possible situation is that the child element is set to the following three situations, which will invalidate the value of the margin attributeposition: absolute;
position: fixed;
float: left/right
Method three, use with absolute
+ transform
attributes
#parent{
position: relative;
}
#child{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
Use normal style plus the above attributes
#parent{
width: 100%;
height: 200px;
background: pink;
position: relative;
}
#child{
width: 200px;
height: 200px;
background: #987;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
<div id="parent">
<div id="child">
水平居中
</div>
</div>
Analysis After the
position: absolute;
current element is set to absolute positioning:
- If the parent element has no defined positioning, the current element is positioned relative to the page
- If positioning is enabled for the parent element (under non-positioning
static
), the current element is positioned relative to the parent element.
left: 50%;
The left distance of the child element relative to the parent element is 50%;
transform: translateX(-50%);
The child element moves half the width of the child element to the left. transform means translation, translateX() means horizontal translation.
Advantages and disadvantages
- Advantages: Whether the parent element is out of the document flow, does not affect the horizontal centering effect of the child element.
- Disadvantages: The transform attribute is a new attribute in CSS3, and the browser support is not good.
Vertical centering method
After talking about the horizontal centering method, here are two common vertical centering schemes. There are two common ways.
table-cell
+vertical-algin
attributeabsolute
+transform
Use with attributes
Method one, table-cell
+ vertical-algin
use together
Key code in method one
#parent{
display: table-cell;
vertical-align: middle;
}
The general style is modified into the following form:
#parent{
width: 200px;
height: 300px;
background: pink;
}
#child{
width: 200px;
height: 200px;
background: #987;
}
Add the above key code.
#parent{
width: 200px;
height: 300px;
background: pink;
display: table-cell;
vertical-align: middle;
}
#child{
width: 200px;
height: 200px;
background: #987;
}
The effect is as follows:
Analysis is
vertical-align: middle;
used to set the vertical alignment of the text content, and vertical-align
the values are as follows:
- top: align at the top
- middle: center alignment
- bottom: the bottom of the
display: table-cell;
table-cell alignment , whichtable
is to set the current element as an<table>
element,table-cell
is to set the current element as an<td>
element (cell)
Integrating the two core elements together can achieve the centering effect. When we set the display
attribute of the parent element to table-cell
be equivalent to setting the parent element to a cell in the table, the content of the cell can be aligned in two ways Yes, one is horizontal alignment, and the other is vertical alignment. As a child element, div
it is equivalent to the content in the cell, and finally shows the effect of vertical centering.
Advantages and disadvantages
- Advantages: The compatibility of the browser is better (mainly for the old version of the browser, because the content used
table-cell
and thevertical-algin
attributes belong to the CSS2 version, and the compatibility of the CSS2 to the old version of the browser is better) - Disadvantages:
vertical-algin
Attributes are inherited, causing the text of the parent element to be displayed in the center.
Add text content to the parent element, and this content will also be displayed in the center.
Method two, use with absolute
+ transform
attributes
Key code
#parent{
position: relative;
}
#child{
position: absolute;
top: 50%;
transform: translateY(-50%);
}
achieve
#parent{
width: 200px;
height: 300px;
background: pink;
position: relative;
}
#child{
width: 200px;
height: 200px;
background: #987;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
<div id="parent">
<div id="child">
垂直居中
</div>
</div>
This part is similar to the third type of file, which is centered horizontally. You can refer to the above here.
Centered layout
In fact, this is the integration of vertical and horizontal methods.
table
+margin
Achieve horizontal centering,table-cell
+vertical-algin
Achieve vertical centering.absolute
+transform
Realize the centered display in the horizontal and vertical directions.
Method 1: table
+ margin
realize horizontal centering, table-cell
+ vertical-algin
realize vertical centering
#parent{
width: 400px;
height: 400px;
background: pink;
display: table-cell;
vertical-align: middle;
}
#child{
width: 200px;
height: 200px;
background: #987;
display: table;
margin: 0 auto;
}
<div id="parent">
<div id="child">
居中布局
</div>
</div>
Advantages and
disadvantages of the first solution Disadvantages:
We can know that the parent
middle has table-cell
actually <td>
become a parent element, and child
the table
value of middle <table>
has become a child element, so here needs to be improved. Modify
the display
attribute value of the child element to block
achieve the same effect, which also solves the problem of language chunks.
#child{
width: 200px;
height: 200px;
background: #987;
display: block;
margin: 0 auto;
}
Advantages: browser compatibility is better.
Method 2: absolute
+ transform
Realize the centered display in the horizontal and vertical directions
Key code
#parent {
position: relative;
}
#child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% , -50%);
}
It’s easy to understand that it combines horizontal and vertical centering methods and centering through positioning properties.
Implementation code
#parent {
width: 400px;
height: 400px;
background: pink;
position: relative;
}
#child {
width: 200px;
height: 200px;
background: #987;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% , -50%);
}
Advantages and disadvantages
Disadvantages: relatively poor compatibility
Advantages: can be applied to more scenarios, and the method is good
to sum up
No solution is optimal. We need to achieve the same effect according to actual application scenarios.