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