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.
The portrait orientation is just 1 column and every element is after the other. No problem here.
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.
Code on 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!
If you have a grid element that should span two columns, you can do grid-column: span 2 , or grid-row: span 2 for rows.
You can use the position property set to “absolute” for the first column and specify its width. Then use the overflow-x property set to “scroll” for the entire table.
Using transitions is one way to animate your grid rows and columns. If your design adjusts the grid structure to cater to different viewports, as long as the number of rows and columns remain the same throughout, you'll see the rows and columns animate to their new sizes.
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.
In the left column you have three separate grid items: the header
, main
and footer
elements.
In the right column you have one grid item: the nav
element.
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>
revised codepen
Browser Support for CSS Grid
Here's the complete picture: http://caniuse.com/#search=grid
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
.
Fiddle demo
Stack snippet
(function(w, d, timeout) {
w.addEventListener("load", function() {
resizer();
}, false);
w.addEventListener("resize", function() {
if (!timeout) {
timeout = setTimeout(function() {
timeout = null;
// do resize stuff
resizer();
}, 66);
}
}, false);
function resizer() {
if (w.innerHeight < w.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'));
}
}
}
}(window, 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With