CSS elements are centered vertically

Source: Internet
Author: User

First, the CSS element is centered vertically in the initial state

<! DOCTYPE html>
<meta charset= "UTF-8" >
<TITLE>CSS Element Vertical Center </title>
<style type= "Text/css" >
. wrapper{
Background-color: #f24444;
width:200px;
height:200px;
margin:0px Auto;

Text-align:center;
}
/* . content{
line-height:200px;
}*/
</style>
<body>
<div class= "wrapper" >
<div class= "Content" > Nothing is difficult, I'm afraid of a conscientious. </div>
</div>
</body>

Two, 1, line-height text Vertical Center

. content{
line-height:200px;
}

2, line-height picture Vertical Center

<! DOCTYPE html>
<meta charset= "UTF-8" >
<TITLE>CSS Element Vertical Center </title>
<style type= "Text/css" >
. wrapper{
Background-color: #f24444;
width:200px;
height:200px;
margin:0px Auto;
Text-align:center;
}
. content{
line-height:200px;
}
. Content img{
Vertical-align:middle;

}
</style>
<body>
<div class= "wrapper" >
<div class= "Content" ></div>
</div>
</body>

Three, table method Vertical Center
<! DOCTYPE html>
<meta charset= "UTF-8" >
<TITLE>CSS Element Vertical Center </title>
<style type= "Text/css" >
. wrapper{
Background-color: #f24444;
width:200px;
height:200px;
margin:0px Auto;
Text-align:center;
display:table;
}
. content{
Display:table-cell;
Vertical-align:middle;
}

</style>
<body>
<div class= "wrapper" >
<div class= "Content" > Nothing is difficult, I'm afraid to be a conscientious </div>
</div>
</body>

Four, 1, absolute positioning and negative displacement vertical center
. wrapper{
Background-color: #f24444;
width:200px;
height:200px;
margin:0px Auto;
Text-align:center;
position:relative;
}
. content{
Position:absolute;
top:50%;
left:50%;
height:30%;
width:50%;
margin:-15% 0 0-25%;
}

Five, absolute positioning and margin expansion

. wrapper{
Background-color: #f24444;
width:200px;
height:200px;
margin:0px Auto;
Text-align:center;
position:relative;
}
. content{
Position:absolute;
top:0;
bottom:0;
left:0;
right:0;
width:50%;
height:30%;
Margin:auto;
}

Six, padding vertical center
. wrapper{
Background-color: #f24444;
margin:0px Auto;
Text-align:center;
padding:5% 0;
}
. content{
padding:10% 0;
}

Seven, floating method vertical Center

. wrapper{
Background-color: #f24444;
Text-align:center;
height:250px;
}
. floater{
Float:left;
height:50%;
width:100%;
margin-bottom:-50px;
}
. content{
Clear:both;
height:100px;
}

CSS elements are centered 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.