div水平布局兩邊對齊的三種實(shí)現(xiàn)方法
發(fā)布時間:2021-01-21 16:07:12 作者:參謀總長薩博
我要評論
這篇文章主要介紹了div水平布局兩邊對齊的三種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
本文主要介紹了div水平布局兩邊對齊的三種實(shí)現(xiàn)方法,分享給大家,具體如下:

方法一
父容器div使用 position: relative;,子div使用 position:absolute;定位,注意邊距問題
html
<div class="div-container"> <div class="div1">1</div> <div class="div2">2</div> </div>
css
/* 方法一 */
.div-container {
margin: 10px 0;
padding: 10px;
width: 400px;
border: 2px solid #ccc;
position: relative;
}
.div1 {
width: 100px;
height: 50px;
border: 2px solid red;
}
.div2 {
width: 100px;
height: 50px;
border: 2px solid red;
position: absolute;
/* 邊距設(shè)置 */
right: 10px;
top: 10px;
}
方法二 推薦
父容器div使用 display:flex; justify-content:space-between; 即可
html
<div class="div-container2"> <div class="div3">3</div> <div class="div4">4</div> </div>
css
/* 方法二 */
.div-container2 {
margin: 10px 0;
padding: 10px;
width: 400px;
border: 2px solid #ccc;
display: flex;
justify-content: space-between;
}
.div3 {
width: 100px;
height: 50px;
border: 2px solid red;
}
.div4 {
width: 100px;
height: 50px;
border: 2px solid red;
}
方法三
父容器div使用display: flex;實(shí)現(xiàn)水平排列, 子div設(shè)置寬度進(jìn)行填充占位
html
<div class="div-container3"> <div class="div5">5</div> <div class="div7">占位div</div> <div class="div6">6</div> </div>
css
/* 方法三 */
.div-container3 {
margin: 10px 0;
padding: 10px;
width: 400px;
border: 2px solid #ccc;
display: flex;
justify-content: space-between;
}
.div5 {
width: 100px;
height: 50px;
border: 2px solid red;
}
.div6 {
width: 100px;
height: 50px;
border: 2px solid red;
}
.div7 {
width: calc(100% - 100px - 100px);
height: 50px;
border: 1px solid #ccc;
}
GitHub 完整代碼鏈接
https://github.com/gywgithub/exercise01/blob/master/div-flex/index.html
到此這篇關(guān)于div水平布局兩邊對齊的三種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)div水平布局兩邊對齊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
- li水平布局在實(shí)際中使用的比較廣泛,實(shí)現(xiàn)方法也有很多,下面為大家介紹下比較實(shí)現(xiàn)的幾種方法,有此需求的朋友可以參考下,希望對大家有所幫助2013-08-18

