"Create a grid container with CSS that allows horizontal scrolling when children are placed using the grid-column

CODEPEN Layout Example

Trying to achieve a GridCSS layout similar to this:

https://i.sstatic.net/hJgIt.png

Challenges:

Struggling to make child elements overflow their parent grid when using grid-column: span 4;.

Note:

  • Children will peak on mobile
  • The grid will have scroll bars if children overflow
  • Children perfectly align with the grid.

Question: Is it possible for CSS Grid Items to overflow with a scroll bar while aligning children to the grid using grid-column? If so, what properties are missing? If not, what workarounds can be used to achieve these layouts shown above when utilizing CSS Grid?

HTML:

<section>
  <div class="container">
    <div class="grid">
      <div class="element">
        <img src="https://placebear.com/400/500" alt="">
        <div class="copy">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sint, eveniet commodi?
        </div>
      </div>
      <div class="element">
        <img src="https://placebear.com/400/500" alt="">
        <div class="copy">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sint, eveniet commodi?
        </div>
      </div>
      <div class="element">
        <img src="https://placebear.com/400/500" alt="">
        <div class="copy">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sint, eveniet commodi?
        </div>
      </div>
     <!-- UNCOMMENT BELOW   -->
    <!--       <div class="element">
    <img src="https://placebear.com/400/500" alt="">
    <div class="copy">
       Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sint, eveniet commodi?
    </div>
  </div> -->
    </div>
  </div>
</section>

CSS:

section {
  width: 100%;
  display: block;
  box-sizing: border-box;
  padding: 64px 48px;
  background: green;
}

.container {
  margin: 0 auto;
  max-width: 1032px;
  background: rgba(244,244,244, .25);
}

.grid {
  display: grid;   
  grid-auto-flow: column;  
  grid-gap: 10px; 
  overflow: auto;
  grid-template-columns: repeat(12, 1fr);
  grid-gap: 48px;
}


.element {
  padding:30px 0;
  text-align: center;
  background: papayawhip;
  grid-column: span 4;
}

Answer №1

To simplify, remove the

grid-template-columns: repeat(12, 1fr);
1. Let the browser handle column templates automatically for an unknown number of elements by defining the implicit grid.

You might want to define grid-auto-columns to set each column's width.

section {
  width: 100%;
  display: block;
  box-sizing: border-box;
  padding: 64px 48px;
  background: green;
}

.container {
  margin: 0 auto;
  max-width: 1032px;
  background: rgba(244, 244, 244, .25);
}

.grid {
  display: grid;
  grid-auto-flow: column;
  overflow: auto;
  grid-gap: 48px;
  grid-auto-columns: minmax(40px, 1fr);
}

.element {
  padding: 30px 0;
  text-align: center;
  background: papayawhip;
  grid-column: span 4;
}

img {
  max-width: 100%;
}
<section>
  <div class="container">
    <div class="grid">
      <div class="element">
        <img src="https://placebear.com/400/500" alt="">
        <div class="copy">
          Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sint, eveniet commodi?
        </div>
      </div>
      ...
    </div>
  </div>
</section>

If you remove

grid-auto-columns: minmax(40px,1fr);
, the top grid will look like this:

https://i.sstatic.net/p0mdP.png

The red lines represent columns with a width of 0, making each item equal to 3 gaps. By using minmax(), allow flexibility in setting column width based on available space and number of elements.

In summary, each element will have a min-width calculation, ensuring consistent sizing regardless of content volume. Without 1fr and with grid-auto-columns: 40px;, element minimum width is simply dictated by the final dimensions.


1 A defined template may conflict with the Auto-Generated Grid, leading to unexpected column widths. For example:

.grid {
  display:grid;
  grid-template-columns:repeat(4,1fr);
  grid-auto-flow: column;
}
img {
  grid-column:span 2;
  border:2px solid green;
  max-width:100%;
}
<div class="grid">
 <img src="https://picsum.photos/200/200?image=0">
 <img src="https://picsum.photos/200/200?image=0">
...
</div>

Observe how differently-sized images result from mixed grid declarations. Exclusively utilizing grid-auto-columns ensures uniform element width:

.grid {
  display:grid;
  grid-auto-flow: column;
  grid-auto-columns:minmax(40px,1fr);
}
img {
  grid-column:span 2;
  border:2px solid green;
  max-width:100%;
}
<div class="grid">
 <img src="https://picsum.photos/200/200?image=0">
 <img src="https://picsum.photos/200/200?image=0">
...
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty adjusting the width of a div element

I've been struggling to adjust the width of the div assigned with the colors class, whether in percentage or pixels. var colors = ["RED", "GREEN", "BLUE", "YELLOW", "PURPLE"]; for (var h = 0; h <= 4; h++) { for (var i = 0; i <= colors.lengt ...

Tips for effectively utilizing the overflow:auto property to maintain focus on the final

I am working on a Todo App and facing an issue where the scrollbars don't focus on the bottom of the page when adding a new element. How can this problem be resolved? https://i.stack.imgur.com/IzyUQ.png ...

How can I anchor a div Banner saying "Get 50% off our products" to the Navbar directly above it?

Bootstrap Question: How can I attach the div Banner ("50% off of our products") to the Navbar directly above it? The structure looks like this: <Navbar> </Navbar> <div> <span>Discount: 50% off of our products </span </di ...

Differentiate input elements with custom styling

I'm experiencing an issue while trying to style a specific form field in Angular 7. The style doesn't seem to be applying properly. Below is my form structure: <ul> <li> <mat-form-field *ngIf="number"> <input ma ...

outputting mysql information onto an HTML webpage using socket.io and node.js

Currently, I am experimenting with printing MySQL data to an HTML page as a way to practice and enhance my learning. Updated Code Below is the snippet from my server.js file var mysql = require("mysql"); var app = require('express')(); var htt ...

There seems to be an issue with the functionality of the dropdown option in my HTML when integrated with my PHP code. I have implemented POST as the retrieval method, but unfortunately, it is resulting in

Is the POST method not appropriate for gathering information about the Employees? I previously used this form with just name, email, and phone fields, and it worked fine. As a PHP beginner, I'm feeling a bit overwhelmed. I trimmed down the form to av ...

Is it unwise to conceal the horizontal scrollbar?

My webpage includes a jquery slideshow using the jquery circle plugin, featuring the "shuffle" option. var slideshow = $('#slider').cycle({ fx: 'shuffle', shuffle: { top: 0, left: 1300}, .... When the images move out ...

Align navigation bar using Angular Material

Trying to center my navbar has been a struggle for me. I attempted using 'layout-align='space-between center' but it's not taking up 100% width and is acting more like an inline element. I've been following the documentation close ...

What is preventing me from viewing my cards in flex or grid layout?

Recently, I created cards for my team members, but encountered layout issues despite using CSS Flexbox and CSS3 Grid. Although both are supported by my browser, the problem persists. However, the layout works perfectly on mobile devices! I experimented wi ...

Trouble with CSS animations in ThreeJS's CSS3DRenderer

Seeking a way to incorporate plain text (2D or 3D) into a threeJS scene while applying CSS effects, I stumbled upon this resource. It seemed to offer the solution I was looking for. However, when I tried to apply an animate.css class (a well-known CSS anim ...

Accordion checkbox with dynamic features

Currently, I am dynamically populating data into a jQuery accordion. My goal is to include a checkbox just before the <h2> text. <div id="checkbox"> <h2> <span> <input type="checkbox" class="mycheck" value="apple" / ...

Move and place multimedia items using IE's drag and drop feature

Is there a way to enable the drag and drop feature across different browsers or windows using HTML5's native DnD API? I have noticed that when I set the data type to 'text' in Internet Explorer, it functions properly. However, if I attempt t ...

Issue in Firefox: Footer is wrapping around the main content

Hey, I've been dealing with a pesky bug that just won't go away. We're currently working on a redesign for www.petpoint.com - The footer looks perfectly fine in every browser except for Firefox. It gets all jumbled up for some reason. I&a ...

Downloading an Excel file by clicking on a hyperlink using Selenium in Python

To download a CSV file, I aim to simply click on the hyperlink provided. Here is the HTML code snippet: <a class="dxbButton_Office2010Blue XafFileDataAnchor dxbButtonSys" id="Dialog_v7_15797720_MainLayoutView_xaf_l104_xaf_dviFile_View_HA ...

The navigation bar expands in height as the media width changes

I have a rather straightforward navbar: <nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header pull-left"> @Html.ActionLink("Contact ...

Can a button be created with multiple borders and a drop shadow effect?

I am trying to create this specific button using CSS https://i.sstatic.net/sLNzK.png (source: gyazo.com) Don't pay attention to the grey background I attempted cropping the center and adding border top, left, right, but it still looks odd. Do you ...

Showing the usage of a JavaScript for loop within an HTML document

I'm attempting to create a countdown feature on my website using a JavaScript function that includes a for loop. However, I'm struggling with getting it to display correctly. The desired outcome is to showcase the countdown within my webpage as ...

Modifying SAS ODS contentitem in PROC SQL SELECT query

When using SAS to generate ODS output in HTML format, you have the option to include a table of contents with the "CONTENTS" and "FRAME" settings: ODS HTML PATH="C:\Path\" (url=none) BODY="body.html" CONTENTS="contents.html" ...

Combine multiple jQuery click functions into a single function

One issue I am facing on my website is that there are multiple modules, each of which can be disabled by the user. The problem lies in the fact that I have a separate script for each module. Hence, my query: How can I combine these scripts into one (perhap ...

When the URL is modified, triggering an event to load

Is there a way to make a page load directly to a specific section and automatically open an accordion? For instance, I have a page with multiple accordions displayed vertically. I already know that using id's in the URL like page#accordion1 will scro ...