gpt4 book ai didi

html - 具有固定页眉、页脚和可滚动内容的响应式网格布局

转载 作者:行者123 更新时间:2023-11-28 16:04:32 25 4
gpt4 key购买 nike

我正在尝试使用 flexbox 创建 2 列全高设计。当我将滚动添加到整个中间部分时,我有一个奇怪的行为。如果父容器有滚动条,flex-grow/stretch 似乎不会增长/拉伸(stretch)其他元素。

这是我的 fiddle .代码也如下:

html, body {
height: 100%;
}
#container {
display: flex;
flex-direction: column;
height: 100%;

width: 50%;
background-color: red;
}

#container header {
background-color: gray;
}
#container section {
flex: 1 1 auto;
overflow-y: auto;
min-height: 0px;
}
#container footer {
background-color: gray;
}
aside {
width : 100px;
background-color: blue;
}
article{
width: 100%;
display:flex;
flex-direction: column;
}
article > div{
flex: 1 1 auto;
}

#content {
display:flex;
}
<section id="container" >
<header id="header" >This is a header</header>
<section id="content">
<aside>
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
test<br />
</aside>
<article id="article" >
<div>
This is the content that
<br />
With a lot of lines.
<br />
With a lot of lines.
<br />
This is the content that
<br />
With a lot of lines.
<br />
<br />
This is the content that
<br />
With a lot of lines.
<br />
<br />
This is the content that
<br />
With a lot of lines.
<br />
</div>
<footer id="footer" >This is a page footer</footer>
</article>
</section>
<footer id="footer" >This is a footer</footer>
</section>

基本上我试图涵盖 2 个场景。如果我不需要滚动它工作正常但是一旦我有一个滚动元素不能正确拉伸(stretch):

1. Main content 'smaller' than left

2. Main content 'bigger' than left

最佳答案

为了让这个布局在今天最新的 Firefox 和 Chorme 中工作(从 2016 年修改这个答案),我做了以下更改:

  1. margin: 0 添加到 body 以允许 container flex 到视口(viewport)高度。

    <
  2. #content 元素的内容包装在另一个 section 中,并使其成为一个 column flexbox。

  3. 将内部 部分设为全高 flexbox,并指定 min-height: max-contentflex: 1

请看下面的演示:

html,
body {
height: 100%;
margin: 0; /* ADDED */
}

#container {
display: flex;
flex-direction: column;
height: 100%;
width: 50%;
background-color: red;
}

#container header {
background-color: gray;
}

#container > section { /* ADDED > selector */
display: flex; /* ADDED */
flex-direction: column; /* ADDED */
flex: 1 1 auto;
overflow-y: auto;
min-height: 0px;
}

#container > section > section{ /* ADDED */
display: flex;
height: 100%;
min-height: max-content; /* fixes chrome */
flex: 1; /* fixes firefox */
}

#container footer {
background-color: gray;
}

aside {
width: 100px;
background-color: blue;
min-height: content;
}

article {
width: 100%;
display: flex;
flex-direction: column;
}

article>div {
flex: 1 1 auto;
}
<section id="container">
<header id="header">This is a header</header>
<section id="content">
<section>
<aside>
test<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br />
</aside>
<article id="article">
<div>
This is the content that
<br /> With a lot of lines.
<br /> With a lot of lines.
<br /> This is the content that
<br /> With a lot of lines.
<br />
<br /> This is the content that
<br /> With a lot of lines.
<br />
<br /> This is the content that
<br /> With a lot of lines.
<br />
</div>
<footer id="footer">This is a page footer</footer>
</article>
</section>
</section>
<footer id="footer">This is a footer</footer>
</section>

上面的解决方案充其量是hacky,并向我们展示了为什么 flexbox 在 2D 布局中。答案是它不是为此而设计的。但是CSS Grids是 - 看看一切是多么容易就位:

  1. 使 #container 成为全视口(viewport)高网格元素。

  2. 中间部分设为网格容器,其中grid-template-columns: 100px 1fr连同 overflow 属性,您就快完成了。

请看下面的演示:

body {
margin: 0;
}

#container {
display: grid;
width: 50%;
height: 100vh;
background-color: red;
}

header, footer {
background-color: gray;
}

#container section {
display: grid;
grid-template-columns: 100px 1fr;
overflow-y: auto;
}

aside {
background-color: blue;
}

article {
display: flex;
flex-direction: column;
}

article > div {
flex: 1 1 auto;
}
<section id="container">
<header id="header">This is a header</header>
<section id="content">
<aside>
test<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br /> test
<br />
</aside>
<article id="article">
<div>
This is the content that
<br /> With a lot of lines.
<br /> With a lot of lines.
<br /> This is the content that
<br /> With a lot of lines.
<br />
<br /> This is the content that
<br /> With a lot of lines.
<br />
<br /> This is the content that
<br /> With a lot of lines.
<br />
</div>
<footer id="footer">This is a page footer</footer>
</article>
</section>
<footer id="footer">This is a footer</footer>
</section>

关于html - 具有固定页眉、页脚和可滚动内容的响应式网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454280/

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