簡體   English   中英

底部對齊父 div 內的 2 個 div(同時將一個子 div 保持在另一個孩子的頂部)

[英]Bottom align 2 divs inside parent div (while keeping one child div on top of another child)

我有 3 個 div:

<div class="parent"> 
  <div class="child1"> </div>
  <div class="child2"> </div>
</div>

CSS:

.parent {
  height: 500px;
  width: 500px;
  position: relative;
}

.child1, .child2 {
  height: 100px;
  width: 100px;
}

我需要對齊child2格是在底部.parent格,而child1格應該是在上面child2格。

因此, child1child2都應該與父 div 底部對齊,但child1應該在child2之上。

我怎樣才能做到這一點? 如果只有一個子 div - 可以通過設置position: absolute; bottom:0來實現position: absolute; bottom:0 position: absolute; bottom:0 ,但我不知道是否有 2 個子 div,一個子 div 應該在另一個之上。

您可以在使用Flexbox的.parent ,並應用以下樣式:

.parent {
  /* Introduce Flexbox */
  display: flex;

  /* Establishes the y-axis as the main axis.
     This displays the children from top to bottom */
  flex-direction: column;

  /* Packs the children from the end of the main axis */
  justify-content: flex-end;
}

 .parent { height: 500px; width: 500px; display: flex; flex-direction: column; justify-content: flex-end; border: 1px solid black; } .child1, .child2 { height: 100px; width: 100px; } .child1 { background: red; } .child2 { background: blue; }
 <div class="parent"> <div class="child1"> </div> <div class="child2"> </div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM