CSS page layout method [vertical center]

Source: Internet
Author: User

I. Problems with vertical center attributes

Vertical-align is defined vertically in CSS, but can only be used as inline elements.

Ii. Problems with defining vertical center Using Boundary Properties

Using margin: auto 0; the content in the fast element cannot be vertically centered. (Invalid)

3. Vertical center with fixed container and content sizes

1. Define the vertical center of the parent element's padding attribute

. Div1 {

Margin: 50px 0 0;

Width: 500px;
Height: 150px;
Background: #666666;
Border: 4px solid #000000;
}

. Div2 {
Width: 200px;
Height: 100px;
Background: #999999;
Border: 1px solid #000000;
}

<Div class-"div1">
<Div class = "div2"> here is the content to be centered </div>
</Div>

2. Define the vertical center of the boundary attribute of the child element

. Div1 {
Width: 500px;
Height: 200px;
Background: #666666;
Border: 4px solid #000000;
}

. Div2 {
Margin: 50px 0 0;
Width: 200px;
Height: 100px;
Background: #999999;
Border: 1px solid #000000;
}

<Div class-"div1">
<Div class = "div2"> here is the content to be centered </div>
</Div>

3. Use the positioning attribute of the child element to define vertical center

. Div1 {
Width: 500px;
Height: 200px;
Background: #666666;
Border: 4px solid #000000;
}

. Div2 {
Position: Relative;

Top: 50px;


Width: 200px;
Height: 100px;
Background: #999999;
Border: 1px solid #000000;
}

<Div class-"div1">
<Div class = "div2"> here is the content to be centered </div>
</Div>

4. Vertical center with fixed content size

. Div1 {
Width: 500px;
Height: 200px;
Background: #666666;
Border: 4px solid #000000;
}

. Div2 {
Position: relative;

Top: 50%;

Margin-top:-50px;


Width: 200px;
Height: 100px;
Background: #999999;

Border: 1px solid #000000;

}

<Div class-"div1">
<Div class = "div2"> here is the content to be centered </div>
</Div>

5. Vertical center with fixed container size

.div{
float:left;
width:250px;
height:200px;
background:#666666;
border:4px solid #000000;
}
.div1{
line-height:200px;
font-size:179pxl
}
.img{
height:100px;
vertical-align:middle;
}
<div class-"div1">

</div>
<div></div>

6. Vertical center with unfixed content and container sizes

. Father {
Position: relative;
Width: 500px;
Height: 200px;
Border: 4px solid #000000;
}

. In {
Position: absolute;
Top: 50%;
}

. Son {
Position: relative;
Top:-50%;
Width: 200px;
Height: 100px;
Background: #999999;
Border: 2px solid #000000;
}

<Div class = "father">
<Div class = "in">
<Div class = "son"> content display </div>
</Div>

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.