Html 将滚动添加到 CSS 网格布局中的每一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43879208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add scroll to each column in CSS Grid Layout
提问by SuddenlyRust
I want separate scroll on each of my columns in my grid layout.
我想在我的网格布局中的每一列上单独滚动。
Currently, I am developing a mobile only web application. I want to use a different grid layout for the portrait and landscape orientations.
目前,我正在开发一个仅限移动的 Web 应用程序。我想为纵向和横向方向使用不同的网格布局。
The portrait orientation is just 1 column and every element is after the other. No problem here.
纵向只有 1 列,每个元素都在另一个之后。这里没问题。
In the landscape orientation I want to use 2 columns. My whole content is displayed on the left side and my navigation moves to the right side. Now I want both parts to have a separate scroll. Is there a way to implement this? And the scroll should stop if the content of the current column ends.
在横向我想使用 2 列。我的全部内容显示在左侧,我的导航移动到右侧。现在我希望这两个部分都有一个单独的滚动。有没有办法实现这个?如果当前列的内容结束,滚动应该停止。
Code on CodePen: https://codepen.io/SuddenlyRust/pen/rmJOqV
CodePen 上的代码:https://codepen.io/SuddenlyRust/pen/rmJOqV
.grid-container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-gap: 15px 0;
}
header {
background-color: green;
grid-column: 1;
grid-row: 1
}
main {
background-color: blue;
grid-column: 1;
grid-row: 2;
}
nav {
background-color: pink;
grid-column: 1;
grid-row: 3;
}
footer {
background-color: teal;
grid-column: 1;
grid-row: 4;
}
@media only screen and (orientation: landscape) {
.grid-container {
grid-template-columns: 5fr 4fr;
}
nav {
grid-column: 2;
grid-row: 1 / span 3;
}
footer {
grid-row: 3;
}
}
h1 {
min-height: 200px;
}
<div class="grid-container">
<header>
<h1>Logo</h1>
</header>
<main>
<h1>content</h1>
</main>
<nav>
<h1>Navigation</h1>
</nav>
<footer>
<h1>Footer</h1>
</footer>
</div>
Thank you very much for your time!
非常感谢您的宝贵时间!
回答by Michael Benjamin
In the landscape orientation I want to use 2 columns. My whole content is displayed on the left side and my navigation moves to the right side. Now I want both parts to have a separate scroll. Is there a way to implement this? And the scroll should stop if the content of the current column ends.
在横向我想使用 2 列。我的全部内容显示在左侧,我的导航移动到右侧。现在我希望这两个部分都有一个单独的滚动。有没有办法实现这个?如果当前列的内容结束,滚动应该停止。
In the left column you have three separate grid items: the header
, main
and footer
elements.
在左边的列中,您有三个独立的电网项目:header
,main
和footer
元素。
In the right column you have one grid item: the nav
element.
在右栏中,您有一个网格项:nav
元素。
Adding a scrollbar – vertical or horizontal – to the left column is not feasible because there are three separate elements. You would need to wrap all elements in a container for a single scrollbar to work.
向左列添加垂直或水平滚动条是不可行的,因为存在三个单独的元素。您需要将所有元素包装在一个容器中才能使单个滚动条工作。
Adding a scrollbar – vertical or horizontal – to the right column is pretty easy because there is only one element.
向右列添加垂直或水平滚动条非常容易,因为只有一个元素。
Assuming that you're talking about a vertical scrollbar, here's one way to make it work:
假设您正在谈论垂直滚动条,这是使其工作的一种方法:
body {
margin: 0;
}
.grid-container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-gap: 15px 0;
height: 100vh;
}
header {
background-color: green;
grid-column: 1;
grid-row: 1
}
main {
background-color: blue;
grid-column: 1;
grid-row: 2;
}
nav {
background-color: pink;
grid-column: 1;
grid-row: 3;
overflow: auto;
}
footer {
background-color: teal;
grid-column: 1;
grid-row: 4;
}
@media only screen and (orientation: landscape) {
.grid-container {
grid-template-columns: 5fr 4fr;
grid-template-rows: 1fr 1fr 1fr;
}
nav {
grid-column: 2;
grid-row: 1 / span 3;
}
footer {
grid-row: 3;
}
}
<div class="grid-container">
<header>
<h1>Logo</h1>
</header>
<main>
<h1>content</h1>
</main>
<nav>
<h1>Navigation<br><br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br>nav item<br></h1>
</nav>
<footer>
<h1>Footer</h1>
</footer>
</div>
Browser Support for CSS Grid
浏览器对 CSS 网格的支持
- Chrome - full support as of March 8, 2017 (version 57)
- Firefox - full support as of March 6, 2017 (version 52)
- Safari - full support as of March 26, 2017 (version 10.1)
- Edge - full support as of October 16, 2017 (version 16)
- IE11 - no support for current spec; supports obsolete version
- Chrome - 自 2017 年 3 月 8 日起全面支持(版本 57)
- Firefox - 自 2017 年 3 月 6 日起全面支持(版本 52)
- Safari - 自 2017 年 3 月 26 日起全面支持(版本 10.1)
- Edge - 自 2017 年 10 月 16 日起全面支持(版本 16)
- IE11 - 不支持当前规范;支持过时版本
Here's the complete picture: http://caniuse.com/#search=grid
这是完整的图片:http: //caniuse.com/#search=grid
回答by Ason
Here is a extended version from my answer on your earlier question, how to get scroll for both header/content/main and nav using flexbox
.
这是我对您之前问题的回答的扩展版本,如何使用flexbox
.
Stack snippet
堆栈片段
(function(d, timeout) {
window.addEventListener("load", function() {
resizeHandler();
}, false);
window.addEventListener("resize", function() {
if (!timeout) {
timeout = setTimeout(function() {
timeout = null;
// do resize stuff
resizeHandler();
}, 66);
}
}, false);
function resizeHandler() {
if (window.innerHeight < window.innerWidth) {
if (!(d.body.classList.contains('landscape'))) {
d.body.classList.add('landscape');
d.body.appendChild(d.querySelector('nav'));
}
} else {
if (d.body.classList.contains('landscape')) {
d.body.classList.remove('landscape')
d.querySelector('section').appendChild(d.querySelector('nav'));
}
}
}
}(document));
html, body {
margin:0;
}
header, footer, main, nav {
margin: 5px;
padding: 5px;
border-radius: 5px;
min-height: 120px;
border: 1px solid #eebb55;
background: #ffeebb;
}
footer {
order: 2;
}
nav {
order: 1;
}
section {
display: flex;
flex-direction: column;
}
@media only screen and (orientation: landscape) {
main div {
height: 400px;
border: 1px dashed red;
}
nav div {
height: 800px;
border: 1px dashed red;
}
body.landscape {
display: flex;
}
section {
display: block;
width: calc(60% - 10px); /* 10px is for the margin */
box-sizing: border-box;
max-height: calc(100vh - 20px);
overflow: auto;
}
nav {
width: calc(40% - 10px); /* 10px is for the margin */
box-sizing: border-box;
max-height: calc(100vh - 20px);
overflow: auto;
}
}
<section>
<header>header</header>
<main>content
<div>
This div get a height when in landscape to show scroll in section
</div>
</main>
<footer>footer</footer>
<nav>navigation
<div>
This div get a height when in landscape to show scroll in nav
</div>
</nav>
</section>