[英]How to have two side by side divs align to bottom?
而不是圖1,我想要圖2。在圖1中,菜單和圖像在頂部對齊,但我希望它們在底部對齊。
這是我的CSS:
<div id="branding">
<div class="brand-left"></div>
<div class="brand-right"></div>
</div>
#branding {
display: inline-block;
width: 100%;
}
.brand-left {
position: relative;
float: left;
width: 730px;
}
.brand-right {
text-align: right;
width: 222px;
float: left;
}
您可以使用display: flex
實現此行為。 您所要做的就是在您的branding
div中添加以下規則:
#branding {
display: flex; /* ADDED */
align-items: flex-end; /* ADDED */
width: 100%;
border: 1px solid green;
}
這是一個片段
#branding { display: flex; width: 100%; border: 1px solid green; align-items: flex-end; } #branding > div{ border: 1px solid blue; } .brand-left { position: relative; float: left; width: 730px; } .brand-right { text-align: right; width: 222px; float: left; }
<div id="branding"> <div class="brand-left">content</div> <div class="brand-right"> <img src="http://senda-arcoiris.info/images/100x100.gif"/> </div> </div>
不使用flex
另一種解決方案是從子div移除floats
並將其絕對放置。 請查看下面的代碼段:
#branding { position: absolute; /* ADDED */ width: 100%; border: 1px solid green; } #branding > div{ position: relative; /* ADDED */ display: inline-block; /* ADDED */ border: 1px solid blue; bottom: 0; } .brand-left { width: 330px; /* I changed the width in order to fit in the snippet */ } .brand-right { text-align: right; width: 222px; }
<div id="branding"> <div class="brand-left">content</div> <div class="brand-right"> <img src="http://senda-arcoiris.info/images/100x100.gif"/> </div> </div>
有很多方法可以做到這一點,最終取決於您要完成的工作以及您的項目要求(例如,瀏覽器支持)。 也就是說,一種方法是將要底部完全對齊的元素定位。
position: relative;
財產和價值 position: absolute;
財產和價值 這是一個工作示例: http : //codepen.io/mjoanisse/pen/grvBwE
您可以嘗試使用display: flex;
.bottom { display: flex; bottom: 0; left: 0; position: fixed; width:100%; } .right { margin-left: auto; }
<div class="bottom"> <div class="left"> left </div> <div class="right"> right </div> </div>
嘗試在.brand-left div
添加line-height
你可以用flexbox試試
#branding{ width:100%; display:flex; height:250px; background-color:green; align-items:flex-end; } .brand-left { position: relative; width: 730px; height:150px; background-color:pink; } .brand-right { text-align: right; width: 222px; background-color:blue; height:250px; }
<div id="branding"> <div class="brand-left"></div> <div class="brand-right"></div> </div>
以防萬一感興趣而無需display: flex
#branding { width: 100%; } .brand-left { position: relative; float: left; width: 750px; background: #f00; min-height: 250px; } .brand-left .nav { position: absolute; bottom: 0px; right: 15px; } .brand-left .nav li { display: inline; } .brand-right { text-align: right; width: 222px; float: left; background: #ff0; min-height: 250px; }
<div id="branding"> <div class="brand-left"> <ul class="nav"> <li><a href="#">Registration</a> </li> <li><a href="#">Log In</a> </li> </ul> </div> <div class="brand-right"></div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.