During a recent interview, the interviewer asked about the horizontal and vertical centering layout method of CSS. As a newbie in front-end, I was undoubtedly confused. I quickly looked up the information when I was free. Let me analyze it and share it with you to avoid falling into pitfalls.
First of all, let’s explain html and some basic css styles, I won’t go into details below!
html
垂直水平居中
The public css code is as follows
These css styles will be included by default in the subsequent introduction, so I won’t go into details!
1. absolute and margin auto (commonly used)
Similarly, the width and height of the centered element must be fixed, and the width and height of the child elements need to be known
In this way, by setting the distance in all directions to 0, and then setting the margin to auto, you can center it in all directions
.div1{ position: relative; } .box{ position: absolute; top:0; left: 0; right: 0; bottom: 0; margin: auto; }
2. Absolute and margin (negative value)
Let’s briefly talk about the principle. Absolute positioning is used. The percentage of absolute positioning is relative to the width and height of the parent element, so we can center the element based on this principle. . But please note: absolute positioning is based on the upper left corner of the child element, but if you want the child element to be displayed in the center, you must solve this problem.
At this time, you can use the negative value of margin to achieve the effect. When the margin is negative, the element is positioned in the opposite direction. In this way, we can set the margin of the sub-element to half the width and height of the sub-element. . (PS: The disadvantage is that the width and height of the child element must be obtained)
.div1{ position: relative; } .box{ top: 50%; left: 50%; position: absolute; margin-top: -50px; margin-left: -50px; }
3. Absolute and calc
also require the width and height of the child element Fixed, and knowing width and height, css3 has computed properties.
The percentage of top is based on the upper left corner of the element minus half of the width (PS: depends on the compatibility of calc)
.div1{ position: relative; } .box{ position: absolute; top: calc(50% - 50px ); /* 减号前后没有空格,该样式不生效*/ left: calc(50% - 50px ); }
When I was writing this code, I discovered an interesting thing, calc (50%-50px) If there are no spaces before and after the minus sign, the style will not take effect. If you add spaces, it will take effect normally. I don’t know the specific reason emmmmm
below The method can be set without knowing the width and height of the child elements.
html is modified to:
水平垂直居中,不需要子元素固定宽高
The public css is as follows
.div1{ width: 300px; height: 300px; border: 1px solid red; } .box{ background: #00FFFF; }
4. Flex (commonly used)
flex is a modern layout solution that only requires a few lines of code to achieve the centering effect
.div1{ display: flex; justify-content: center; align-items: center; }
5. line-height
Using the centering attribute of inline elements can also achieve horizontal and vertical centering. Set the box as an inline element and use text-align to achieve horizontal centering or vertical-align (PS: This method requires resetting the text display to the desired effect in the child element)
.div1{ line-height: 300px; text-align: center; font-size: 0px; } .box{ font-size: 10px; display: inline-block; vertical-align: middle; line-height:initial; /* 修正文字 */ text-align: left; }
6. Absolute and transform
does not require fixed width and height of child elements, but depends on the compatibility of translate2d
.div1{ position: relative; } .box{ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); }
7. css-table
The new table attribute of css can change the display effect of ordinary elements to table elements, and can also achieve horizontal centering
.div1{ display:table-cell; text-align: center; vertical-align: middle; } .box{ display:inline-block; }
The above are some of the centered layout methods I have summarized. If there are any other methods, you are welcome to add them!
Personal feeling:I preferabsolute margin auto, flex, absolute and transform. It is best to use flex on the mobile terminal. I like absolute margin auto# on the PC terminal.
## Recommended related tutorials:CSS video tutorial,CSS3 video tutorial
The above is the detailed content of Let's talk about the method of realizing horizontal and vertical centered layout with CSS. For more information, please follow other related articles on the PHP Chinese website!