CSS implements vertical center of Div content

Source: Internet
Author: User

1. center a single line of content
It is easiest to consider a single row. No matter whether or not the container is given a fixed height, you only need to set the line-height and height for the container and make the two values equal, plus over-flow: hidden:
. Middle-demo-1 {
Height: 4em;
Line-Height: 4em;
Overflow: hidden;
}

Advantages:
1. Both block-level and inline polar elements are supported.
2. Support for all browsers
Disadvantages:
1. Only one row can be displayed
2. the center of is not supported in IE6.
Note that:
1. Use relative height to define your height and line-height
2. If you don't want to destroy your layout, overflow: hidden must

 If you wantAlign the text horizontally, and add "text-align: center ;".;CodeAs follows:

Note: If the parent-level element defines text-align: center, this means that the content in the parent-level element is centered. for IE, this setting is enough. However, it cannot be centered in Mozilla. The solution is to add "margin-Right: auto; margin-left: auto;" when the sub-element is defined ;". However, if all docdocype adopts the XHTML transitional mode, you can use the latter.

2. Multi-line content is centered and the container height is variable

It is also very simple,Provide consistent padding-bottom and padding-top.

. Middle-demo-2 {
Padding-top: 24px;
Padding-bottom: 24px;
}

Advantages:
1. Both block-level and inline polar elements are supported.
2. Supports non-text content
3. Support for all browsers
Disadvantages:
Container cannot be fixed height

3. How to vertically center an image in a div
background method .



the key is the final center. This parameter defines the image position. It can also be written as "top left" (upper left corner) or "bottom right", or "50 30 ".

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.