This article mainly introduces to you methods to achieve vertical centering of div.
Various centered layouts of divs in web design are the most basic knowledge and skills that HTML/CSS beginners need to master. div horizontal centering effect We have introduced it to you in previous articles, and I believe that novice friends have already mastered it.
Now we will continue to use simple examples to introduce to you the implementation method of vertically centering div content.
Recommended reference study: "html tutorial"
div vertical centering codeThe example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Div垂直居中</title> </head> <body> <div style="line-height: 500px;height: 500px;width: 500px; background: red;"> PHP中文网 </div> </body> </html>
The front desk effect is as shown below:
As shown in the picture, the text in the div is vertically centered. The main attribute is line-height.
line-height The property sets the distance between lines (line height is the vertical direction).
The line-height property affects the layout of the line box. When applied to a block-level element, it defines the minimum distance between baselines in that element rather than the maximum distance.
The calculated difference between line-height and font-size (known as "line spacing" in CSS) is divided into two halves and added to the top and bottom of a line of text content. The smallest box that can contain this content is a line box. The raw numeric value specifies a scaling factor, and descendant elements inherit this scaling factor rather than the calculated value.
If we change the set height from 500px to 200px, the rendering effect is as follows:
##If we do not set the height here but set it line-height: 200px;, the effect is as follows: At this time we can measure and find that the height of the div with the red background is 400px.The difference between line-height and height:
To put it simply, line-height means line height, and height defines the height of the element itself.line-height and height values consistent.
This article is about the implementation method ofdiv vertical centering. It is also very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to achieve the vertical centering effect of Div. For more information, please follow other related articles on the PHP Chinese website!