您當前位置:圖趣網(wǎng)(Tuquu) >> 網(wǎng)頁設計教程 >> 移動前端 >> 瀏覽設計教程

DIV+CSS垂直居中一個浮動元素

 場景:在一個固定高度的div中,有一個浮動的元素,需要將這個浮動元素垂直居中。

原始代碼如下:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.wrapper{
width: 400px;
height: 300px;
background-color: #1f8dd6;
}
button{
float: right;
display: inline-block;
height: 50px;
width: 100px;
line-height: 50px;
}
</style>
</head>
<body>
<div class="wrapper">
<button>float right.</button>
</div>
</body>
</html>

 

現(xiàn)在只是簡單的設置這個button浮動,實現(xiàn)的效果看起來就像這樣:

DIV+CSS垂直居中一個浮動元素 三聯(lián)

現(xiàn)在需要將這個button在整個div里垂直居中。我的做法是在這個button外層加一個span,并且浮動這個span元素而不是之前的button。另外需要設置這個span的高和行高與外層div相同。


 
代碼如下:

span{
float: right;
height: 300px;
line-height: 300px;
}

現(xiàn)在應該就變成這樣了:

完整代碼:

代碼如下:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.wrapper{
width: 400px;
height: 300px;
background-color: #1f8dd6;
}
span{
float: right;
height: 300px;
line-height: 300px;
}
button{
float: right;
display: inline-block;
height: 50px;
width: 100px;
line-height: 50px;
}
</style>
</head>
<body>
<div class="wrapper">
<span>
<button>float right.</button>
</span>
</div>
</body>
</html>
[教程作者:佚名]
免責聲明:本站文章系圖趣網(wǎng)整理發(fā)布,如需轉(zhuǎn)載,請注明出處,素材資料僅供個人學習與參考,請勿用于商業(yè)用途!
本文地址:http://irelandcustomcontracting.com/tutorial/wd2223.html
div+css垂直居中的五種實現(xiàn)方法
CSS提高渲染性能實現(xiàn)方法
圖趣網(wǎng)微信
建議反饋
×