CSS elements vertically horizontally centered instance

Source: Internet
Author: User
Tags relative truncated

Cases

/*
Name: Vertical Horizontal Center
Usage:
1. [principle] set width and height, the parent node is position:relative; CSS is written in this way:

position:absolute;left:50%;top:50%;
Half of the margin-left:-element's own width;
Half of the margin-top:-element's own height;

2. [Principle] table, it's easier to use, just add:

Text-algin:center;
Vertical-align:middle;

3. [Method] provides a templated CSS class method with the following rules:

The code is as follows Copy Code


<div class= "sl-hvalign" style= "width:500px" > <!--Remember to widen, do not write inline style, here is just a hint-->
<div class= "sl-hvalign-cnt" >
<div class= "Sl-hvalign-cnt-inner" >
<!--your code-->
</div>
</div>
</div> <!--. sl-hvalign-->
*/


. sl-hvalign{
display:table;
Overflow:hidden;
margin:0 Auto;
height:100%;
*position:relative;
}

. sl-hvalign-cnt{
Display:table-cell;
Vertical-align:middle;
*position:absolute;
top:50%;
}

. sl-hvalign-cnt-inner{
*position:relative;
*top:-50%;
}

Pure Picture Vertical Center, write three layer label really some fees, but this has to say is a compromise scheme, when there are pictures, there is text, is a better choice.

  code is as follows copy code

<! DOCTYPE html>
<meta charset= ' utf-8 '/>
<title></title>
<style type= "Text/css"
. out{width:400px;height:300px;margin:20px Auto;display:table-cell; Text-align:center;vertical-align:middle;background: #ccc;}
. Out img{width:100px;height:100px;background: #fcc;}
</style>

<body>
<div class= ' out '

</div>
</body>


Other CSS to achieve a vertical centering method
A
Inserts a div outside the content element. Set this div height:50%; Margin-bottom:-contentheight;.
The content clears the float and is displayed in the middle.
Benefits: Applies to all browsers; when there is not enough space (for example, the window shrinks) content is not truncated, scroll bars appear
Disadvantage: The only thing I can think of is the need for extra empty elements.

The code is as follows Copy Code

#floater {float:left; height:50%; margin-bottom:-120px;}
#content {clear:both; height:240px; position:relative;}

<div id= "Floater" ></div>
<div id= "Content" >
Content here
</div>

B
uses a position:absolute, with a fixed width and height div. This div is set to top:0; bottom:0;. But because it has a fixed height, actually can not and up and down both spacing is 0, so margin:auto; will make it center. Using Margin:auto; it is easy to center the block-level elements vertically. Advantages: simple; Disadvantages: Invalid in IE (IE8 beta); content is truncated when there is not enough space, but no scroll bar appears

  code is as follows copy code

<!doctype html>
<title> </title>
<meta charset= ' utf-8 '/>
<style type= "Text/css"
. Outer{height:240px;width : 500px;margin:20px auto 0;position:relative;background: #ccf;}
. Inner{width:300px;height:100px;background: #9f9;p osition:absolute;top:0;bottom:0;left:0;right:0;margin: Auto;text-align:center;}
. Inner img{height:100px;}
</style>

<body>
<div class= "outer"
<div class= "inner" >

</div>
</div>
</body>

C
Using the absolutely positioned Div, set its top to the 50%,top margin set to the negative content height. This means that the object must specify a fixed height in the CSS.
Because there is a fixed height, you may want to specify Overflow:auto for the content so that if there is too much content, the scroll bar will appear, lest the content overflow.
Benefits: Applies to all browsers, and does not require nested labels. Disadvantage: When there is not enough space, the content will disappear (like div within the body, when the user shrinks the browser window, the scroll bar does not appear)

The code is as follows Copy Code

. Content {
Position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* Negative half of the height * *
}

<div class= "Content" >
Content goes here

</div>

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.