This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August
The level of
- The inline element is horizontally centered and utilized
text-align: center
Inline elements inside block-level elements can be horizontally centered. This method works with inline elementsinline
Inline blocks,inline-block
, inline tableinline-table
.inline-flex
Horizontally centered elements are valid.
.center-text {
text-align: center;
}
Copy the code
- Block-level elements are horizontally centered by placing the block-level elements of a fixed width
margin-left
andmargin-right
Set auto to center the block-level elements horizontally.
.center-block {
margin: 0 auto;
}
Copy the code
3 Multiple block-level elements are horizontally centered
- using
inline-block
If there are two or more block-level elements in a row, set the display type of the block-level elements toinline-block
And the parent containertext-align
Property to center multiple block-level elements horizontally.
.container {
text-align: center;
}
.inline-block {
display: inline-block;
}
Copy the code
- using
display: flex
, to achieve horizontal center, wherejustify-content
Used to set the alignment of the elastic box elements along the main (horizontal) axis, in this case to set the child elements to be horizontally centered.
.flex-center {
display: flex;
justify-content: center;
}
Copy the code
vertical
- A single line of inline elements is vertically centered
Center an inline element vertically by setting the height of the inline element equal to the line height.
#v-box {
height: 120px;
line-height: 120px;/ / vertical
}
Copy the code
- Multiple row elements are vertically centered
- Using table layouts
table
thevertical-align: middle
Vertical centering of child elements is possible.
.center-table {
display: table;
}
.v-cell {
display: table-cell;
vertical-align: middle;
}
Copy the code
- Using Flex layout
flex
Achieve horizontal vertical center, whereflex-direction: column
Define the principal axis as vertical. Because the Flex layout is defined in CSS3, there are compatibility issues with older browsers.
/ / vertical
.center-flex {
display: flex;
flex-direction: column;
justify-content: center;
}
Copy the code
- using
Ghost Element
The technique achieves vertical centering by placing a pseudo-element of 100% height in the parent container so that the text and the pseudo-element are aligned vertically.
.ghost-center {
position: relative;
}
.ghost-center::before {
content: "";
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
width: 20rem;
}
Copy the code
- Block-level elements are vertically centered
- Fixed height of block level element by absolutely positioning element 50% away from top, and set
margin-top
Offset the element up by half the height to achieve vertical centering.
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px;
}
Copy the code
- For block-level elements of unknown height, we can use CSS3
transform
The method with a 50% reverse shift of the property to the Y axis achieves vertical centering. However, some browsers have compatibility issues.
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Copy the code