Applying Vetical-align:middle to block-level elements using pseudo-elements to center vertically

Source: Internet
Author: User

The function of the vetical-align is to align the inline elements vertically.

Possible values

Baseline Default. element is placed on the baseline of the parent element.
Sub Subscript for vertical alignment of the aligned right.
Super Superscript of vertical aligned aligned
Top Aligns the top of the element with the top of the highest element in the row
Text-top Aligns the top of the element with the top of the parent element's font
Middle Place this element in the middle of the parent element.
Bottom Aligns the top of the element to the top of the lowest element in the row.
Text-bottom Aligns the bottom of the element with the bottom of the parent element's font.
Length
% Use the percent value of the "Line-height" property to arrange this element. Negative values are allowed.
Inherit Specifies that the value of the Vertical-align property should be inherited from the parent element.

You can control the alignment of elements in the vertical direction of lines within a picture, text, etc.

How do I use Vetical-align to center the block-level elements vertically?

Take the following example:

body{

<div class= "wrapper" >

<div class= "Content" >some content</div>

</div>

}

1. Set the height for the parent container wrapper

html,body{

height:100%

}

. wrapper{

height:100%

}

2. Make content containers appear inline elements

. content{

Display:inline-block;

}

3. Construct an empty content inline-block by before pseudo class before content container

. wrapper:before{

Content: "";

Display:inline-block;

height:100%;

Vetical-align:middle;

}

At this point, the entire page has only one row, with two inline-block, allowing empty content lines to align up and down

4. Add Vetical-align property for content container

. content{

Vetical-align:middle;

}

This way, block-level elements are always vertically aligned in the browser.

There are also elements such as adding an empty IMG attribute to an HTML element or displaying it as a TABLE element, which destroys the structure of the HTML element and is not described here.

Applying Vetical-align:middle to block-level elements using pseudo-elements to center vertically

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.