Example of CSS controlling the center of elements in Div

Source: Internet
Author: User

div row-level element centered

The

code is as follows:


<div class= "Treetitle" >


<img src= "Images/app.png"/>


<span> navigation Menu </span>


</div>


Requirements: Center and <span> labels

Solution: DIV for block-level elements, IMG and span are inline elements. So this problem comes down to the center of the inline element in the block-level element.

The

code is as follows:


#treeTitle {


Background:url (.. /images/bj.png);


height:30px;


Color:white;


Text-align:left;


line-height:30px;


}


#treeTitle img{


Vertical-align:middle;


}


Line-height is the row height, which refers to the distance between the baselines of the line of text.

Inline elements generate a inline box, which is just a concept that cannot be displayed, but it does exist. In the absence of other factors, the inline box equals the content area, and a set row height increases or decreases the height of the inline box, that is, by dividing the value of the line spacing (row height-font size) by 2, to the top and bottom sides of the content area, as shown below:

Because row heights can be applied to any element, several elements within the same row may have different row heights and a high inline frame. A row box is a virtual rectangular box that is the height of the highest value for all elements within the bank. When there are multiple lines of content, each row has its own row box.
When the content contains pictures, if the height of the picture is greater than the row height, then the row box containing the picture line will be propped up the height of the avenue picture. Although the picture is stretched to a row box, it does not affect the row height and therefore does not affect other properties that are computed based on row heights. When a row contains a picture, the vertical alignment of the picture and text defaults to the baseline alignment. So when the following code is not added, the picture and text are aligned at the bottom.

The

code is as follows:


#treeTitle img{


Vertical-align:middle;


}


About Line-height here is a very good blog explanation, Line-height detailed, the effect as shown in the following figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.