Implementation of css vertical center

Source: Internet
Author: User

Use absolute positioning to align the top of the element with the center line, and move the element up to 50%. This should be hard to understand. The principle can be illustrated in the following figure:

The code is as follows: Copy code
* Code implementation:
* Set the width and height. The parent node is position: relative. CSS is written as follows:
*/
. Selector {
Position: absolute; top: 50% ;.
Margin-top:-half of the height of an element;
}

Instance

The code is as follows: Copy code

<! DOCTYPE html>
<Html lang = "en-US">
<Head>
<Meta charset = "UTF-8"/>
<Title> vertical center </title>
 
<Style>
* {Margin: 0; padding: 0 ;}
Body {font-family: consolas; padding: 20px ;}
Img, p {border: 1px dotted # bbb; padding: 5px ;}
P {margin-bottom: 10px ;}
A {text-decoration: none ;}
 
. Wrap {
Width: 500px; height: 300px; border: 3px solid # ddd; margin: 0 auto; padding: 20px; display: table;
* Position: relative;
    }
. Hack {
Display: table-cell; vertical-align: middle;
* Position: absolute; * top: 50%;
    }
. Cnt {
* Position: relative; * top:-50%;
    }
</Style>
 
</Head>
<Body>


<Div class = "wrap">
<Div class = "hack">
<Div class = "cnt">

<P> The content here is www.111cn.net.
</P>
<P>
<A href = "http //: www.111cn.net" title = "return: vertical center implementation methods"> Return: vertical center implementation methods & raquo; </a>
</P> </div>
</Div>
</Div>

</Body>
</Html>

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.