首頁 > web前端 > css教學 > 主體

如何使用 CSS Flexbox 填充容器中剩餘的垂直空間?

Susan Sarandon
發布: 2024-10-28 08:42:02
原創
310 人瀏覽過

How to Fill Remaining Vertical Space in a Container with CSS Flexbox?

用#second填滿剩餘的垂直空間

為了用#second div填滿#first下#wrapper的剩餘垂直空間,使用僅使用CSS,您可以採取以下步驟:

  1. 將html 和body 設定為佔據100% 高度,沒有邊距。
  2. 將 display: flex、flex-direction: column 指派給父容器.wrapper。
  3. 給.first指定具體高度。
  4. 將flex-grow: 1設為.second,使其拉伸以佔據剩餘空間。

CSS 程式碼如下:

<code class="css">html, body {
  height: 100%;
  margin: 0;
}

.wrapper {
  display: flex;
  flex-direction: column;
  width: 300px;
  height: 100%;
}

.first {
  height: 50px;
}

.second {
  flex-grow: 1;
}</code>
登入後複製
<code class="html"><div class="wrapper">
  <div class="first" style="background:#b2efd8">First</div>
  <div class="second" style="background:#80c7cd">Second</div>
</div></code>
登入後複製

以上是如何使用 CSS Flexbox 填充容器中剩餘的垂直空間?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!