Horizontal scroll with css grid
Asked Answered
A

6

20

I'm having a problem when I am trying to make a horizontal scroll when the grid complete four columns. See

#series {
    display: grid;
    grid-gap: 16px;
    overflow-x: scroll;
    padding: 16px;
    grid-template-columns: repeat(4, 1fr);
    grid-auto-flow: column;
}

Using this I get below output

enter image description here

But, you know, I want to get same like "four columns" and a scroll bar for see more.

What's the problem.

Abney answered 16/2, 2019 at 15:33 Comment(6)
the width of each column must be dynamic or sort of the same as in the above image?Kincardine
Nop, I've tried using grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); but all other items that cant be in the screen not appearsAbney
Can you post a jsfiddle or codepen link? I think that you should set the overflow-x: scroll on the grid's parent, not on the grid.Polythene
I guess detecting the width of a grid till some but not all grid items may not be possible; the same is the case with flexboxes as well.Kincardine
This is my code codesandbox.io/s/m77m8ory9j but I don't know why is not working.Abney
Because you minimum grid item size is 160px, and if you are ok to keep that size fixed, you can use overflow on a wrapper element (whose width is something like 4*160px) of the grid-box and grid-auto-flow: column as in the question.Kincardine
T
54

Try it with this:

display: grid;
grid-gap: 16px;
padding: 16px;
grid-template-columns: repeat(auto-fill,minmax(160px,1fr));
grid-auto-flow: column;
grid-auto-columns: minmax(160px,1fr);
overflow-x: auto;

grid-auto-flow: column; will force the grid to add your elements as column instead of following the free space.

grid-auto-columns: minmax(160px,1fr); the items added outside the viewport do not match auto-fit, so they won't get the size defined in your template. So you have to define it again with grid-auto-columns.

overflow-x: auto; auto will add the scrollbar

Tetrafluoroethylene answered 18/2, 2019 at 8:25 Comment(3)
When I tried to do this inside a div that is placed in a header with position:sticky the whole page is scrolledPartisan
@Partisan not sure this could apply to your problem without more details. But check out the following link for some tips about the combination between position: sticky and grid melanie-richards.com/blog/css-grid-sticky/?ref=heydesignerTetrafluoroethylene
Great solution! No need to use a JS slider plugin anymore!Milka
T
6

try this:

.container{
   display: grid;
   grid-auto-columns: max-content;
   grid-template-rows: 1fr;
   width: 100%;
   overflow-x: auto;
 }
.child{
   grid-row: 1;
   grid-column: auto;
  }
Tinishatinker answered 22/6, 2021 at 18:31 Comment(1)
This solution worked for what I was trying to accomplish: A scrollable, single row column.Quijano
M
4

Horizontal scrolling containers the right way! Source

:root {
  --gutter: 20px;
}

.app {
  padding: var(--gutter) 0;
  display: grid;
  grid-gap: var(--gutter) 0;
  grid-template-columns: var(--gutter) 1fr var(--gutter);
  align-content: start;
}

.app > * {
  grid-column: 2 / -2;
}

.app > .full {
  grid-column: 1 / -1;
}

.hs {
  display: grid;
  grid-gap: calc(var(--gutter) / 2);
  grid-template-columns: 10px repeat(6, calc(50% - var(--gutter) * 2)) 10px;
  grid-template-rows: minmax(150px, 1fr);
  
  overflow-x: scroll;
  scroll-snap-type: x proximity;
  padding-bottom: calc(.75 * var(--gutter));
  margin-bottom: calc(-.5 * var(--gutter));
}

.hs:before,
.hs:after {
  content: '';
}

/* Hide scrollbar */
.hide-scroll {
  overflow-y: hidden;
  margin-bottom: calc(-.1 * var(--gutter));
}


/* Demo styles */

html,
body {
  height: 100%;
}

body {
  display: grid;
  place-items: center;
  background: #456173;
}

ul {
  list-style: none;
  padding: 0;
}

h1,
h2,
h3 {
  margin: 0;
}

.app {
  width: 375px;
  height: 667px;
  background: #DBD0BC;
  overflow-y: scroll;
}

.hs > li,
.item {
  scroll-snap-align: center;
  padding: calc(var(--gutter) / 2 * 1.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #fff;
  border-radius: 8px;
}
<div class="app">
  <h1>Some headline</h1>

  <div class="full hide-scroll">

    <ul class="hs">
      <li class="item">test</li>
      <li class="item">test</li>
      <li class="item">test</li>
      <li class="item">test</li>
      <li class="item">test</li>
      <li class="item">test</li>
    </ul>

  </div>

  <div class="container">
    <div class="item">
      <h3>Block for context</h3>
    </div>
  </div>
</div>
Marlenemarler answered 17/2, 2019 at 9:7 Comment(1)
I'm sorry, I don't like this approach because need to use JavaScript to set the number of grid items. Just set grid-auto-flow: column; and grid-auto-columns with the same value as grid-template-columns and will work!Smitten
S
2

I faced a similar issue recently and none of the answers seemed to solve this the way I needed.

Here's my take on this:

body {
    background-color: #e2e2e2;
}

h1,
h2 {
    text-align: center;
}

.cards {
    /* How many columns to show. */
    --visible-cols: 4;

    /* Gap between columns. */
    --col-gap: 20px;

    /* How much to show of the next hidden column. You can use this as a hint to show the user that there are more scrollable items. Particularly helpful for mobile devices where the scrollbar gets hidden by default.*/
    --col-hint: 0px;

    /* Whitespace between the elements and the scrollbar. */
    --scrollbar-padding: 20px;

    /*  Calculated column size. */
    --col-size: calc((100% / var(--visible-cols)) - var(--col-gap) - var(--col-hint) / var(--visible-cols));
    
    display: grid;
    grid-auto-flow: column;
    grid-template-columns: var(--col-size);
    grid-auto-columns: var(--col-size);
    overflow-x: auto;
    overflow-y: hidden;
    grid-gap: var(--col-gap);
    padding-bottom: var(--scrollbar-padding);
    margin: 30px 0;
}

.cols-5,
.desktop-cols-5 {
    --visible-cols: 5;
}

.cols-3 {
    --visible-cols: 3;
}

.hint-next {
    --col-hint: 20px
}

@media (max-width: 1024px) {
    .laptop-cols-3 {
        --visible-cols: 3;
    }
}

@media (max-width: 767px) {
    .tablet-cols-2 {
        --visible-cols: 2;
    }
    .tablet-hint-next {
        --col-hint: 15px
    }
}

@media (max-width: 425px) {
    .mobile-cols-1 {
        --visible-cols: 1;
    }
    .mobile-hint-next {
        --col-hint: 10px
    }
}

.card {
    display: flex;
    flex-flow: column nowrap;
    background-color: lightblue;
    justify-content: center;
    align-items: center;
    padding: 10px;
    border-radius: 10px;
    min-height: 250px;
    overflow: hidden;
}

.card-title,
.card-desc {
    flex: 1;
    border-radius: 10px;
}

.card-title {
    font-size: 1.2em;
    font-weight: bold;
}

.card-desc {
    padding: 20px;
    background-color: orange;
}
<div class="main">
   <h1>CSS Grid - Scrollable Horizontal Layout Example</h1>
   <h2>4 Columns</h2>
   <div class="cards">
      <div class="card">
         <div class="card-title">Card 1</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 2</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 3</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 4</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 5</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 6</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 7</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 8</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 9</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 10</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
   </div>
   <h2>3 Columns - Hint Next</h2>
   <div class="cards cols-3 hint-next">
      <div class="card">
         <div class="card-title">Card 1</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 2</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 3</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 4</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 5</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 6</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 7</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 8</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 9</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 10</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
   </div>
   <h1>Responsive Example</h1>
   <h2>Desktop - 5 Columns</h2>
   <h2>Laptop - 3 Columns</h2>
   <h2>Tablet - 2 Columns - Hint Next</h2>
   <h2>Mobile - 1 Column - Hint Next</h2>
   <div class="cards desktop-cols-5 laptop-cols-3 tablet-cols-2 tablet-hint-next mobile-cols-1 mobile-hint-next">
      <div class="card">
         <div class="card-title">Card 1</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 2</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 3</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 4</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 5</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 6</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 7</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 8</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 9</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
      <div class="card">
         <div class="card-title">Card 10</div>
         <div class="card-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
      </div>
   </div>
</div>

The important bit being:

.cards {
    /* How many columns to show. */
    --visible-cols: 4;

    /* Gap between columns. */
    --col-gap: 20px;

    /* How much to show of the next hidden column. You can use this as a hint to show the user there are more scrollable items. Particularly helpful for mobile devices where the scrollbar gets hidden by default.*/
    --col-hint: 0px;

    /* Whitespace between the slider and the scrollbar. */
    --scroller-padding: 20px;

    /*  Calculated column size. */
    --col-size: calc((100% / var(--visible-cols)) - var(--col-gap) - var(--col-hint) / var(--visible-cols));

    display: grid;
    grid-auto-flow: column;
    grid-template-columns: var(--col-size);
    grid-auto-columns: var(--col-size);
    overflow-x: auto;
    overflow-y: hidden;
    grid-gap: var(--col-gap);
    padding-bottom: var(--scroller-padding);
}

It can be very easily made responsive by adjusting the relevant CSS properties for each screen size. I have included a responsive example as well.

You can also check this as a codepen here: https://codepen.io/amyriounis/pen/VwyydGj

Sheena answered 4/4, 2022 at 19:8 Comment(0)
P
1

Your problem is that you are using 1fr unit. Instead put 25% (use calc to accommodate for any gaps). This will size your columns relative to available screen width. Of course you must apply 25% to grid-auto-columns also.

Plainlaid answered 10/10, 2020 at 12:27 Comment(0)
L
0

I think this medium article has the answer: https://uxdesign.cc/creating-horizontal-scrolling-containers-the-right-way-css-grid-c256f64fc585

Basically it comes down to the fact that the individual cards need to have a fixed width instead of a dynamic one, in order to force the browser to not squish them together.

Legpull answered 16/2, 2019 at 15:59 Comment(2)
Bro, I´ve tried with grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); but I'm not getting the scroll bar, is same like the others items does not existsAbney
Try overflow:scroll!important on parent element.Kassia

© 2022 - 2024 — McMap. All rights reserved.