gpt4 book ai didi

html - 使用 flexbox 拉伸(stretch)内容以扩展到整个视口(viewport)

转载 作者:行者123 更新时间:2023-11-28 01:51:24 25 4
gpt4 key购买 nike

header,
nav,
main {
border: 1px solid red;
}

.row {
display: flex;
flex: 0 0 100%;
}

.col {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}

.col_3 {
flex: 0 0 25%;
}
<html>
<hedad></hedad>

<body>
<div>
<header>Header</header>
<div class="row">
<div class="col col_3">
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</div>
<div class="col">
<main>main</main>
</div>
</div>
</div>
</body>

</html>

考虑到附加的标记,我是否可以只使用 css 而无需使用固定高度来设置侧边栏 <nav>元素和 main元素填充视口(viewport)底部的可用高度?

最佳答案

您至少需要定义一些height:100% 或使用vh 单位。这是一个例子:

body,
html,
nav,
main {
height: 100%;
}
* {
box-sizing:border-box;
}
body {
display: flex;
flex-direction: column;
margin: 0;
}

header,
nav,
main {
border: 1px solid red;
}

.row {
display: flex;
flex: 1;
}

.col {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}

.col_3 {
flex: 0 0 25%;
}
<header>Header</header>
<div class="row">
<div class="col col_3">
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</div>
<div class="col">
<main>main</main>
</div>
</div>

这是使用 vh 单位的另一个想法:

nav,
main {
height: 100%;
}
* {
box-sizing:border-box;
}
body {
display: flex;
flex-direction: column;
margin: 0;
height:100vh;
}

header,
nav,
main {
border: 1px solid red;
}

.row {
display: flex;
flex: 1;
}

.col {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}

.col_3 {
flex: 0 0 25%;
}
<header>Header</header>
<div class="row">
<div class="col col_3">
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</div>
<div class="col">
<main>main</main>
</div>
</div>

关于html - 使用 flexbox 拉伸(stretch)内容以扩展到整个视口(viewport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49906681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com