簡體   English   中英

無法垂直居中 div

[英]Not able to vertically center a div

我一直在嘗試將 div 垂直居中,嘗試了position: absolute和 flexbox,均未成功。

還嘗試將 styles 添加到父 div 和子 div,仍然沒有工作。 它水平居中但不垂直居中

這是父元素的 HTML

<div className="all-questions">{renderQuestions}</div>

這是父元素的 CSS

.all-questions {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

謝謝!

您需要指定一個高度,然后它才會起作用。 試試下面的代碼。

.all-questions {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
}

我喜歡用display: flex; . 來源:MDN

 * { margin: 0; padding: 0; }.container { height: 100vh; width: 100vh; background: gray; display: flex; justify-content: center; /* center it horizontally (cross axis) */ align-items: center; /* center it vertically (main axis) */ }.v-centered { height: 40px; width: 40px; background: white; }
 <div class="container"> <div class="v-centered"></div> </div>

暫無
暫無

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

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