The interaction of a horizontal scroll bar on a CSS Grid layout triggers the appearance of a vertical scroll bar

I am facing a challenge with my HTML layout. In Chrome, when the horizontal scroll bar is visible, a vertical scroll bar also appears. However, in Firefox, everything works fine. This issue seems to be related to the height of the div not auto-scaling properly due to using the grid 1fr property. The problem can be resolved by using height: 100%.

Here is the link to the solution on CodePen

html, body {
            height: calc(100% - 20px);
            display: grid;
            grid-template-row: 1fr 1fr;
        }

        .mainContainer{
            height: 100%;
            display: grid;
            grid-template-rows: auto 1fr;  
        }

        .container{
            display: grid;
            grid-template-rows: 1fr;
            grid-auto-flow: column;
            width: 500px;
            overflow-x: auto;
            overflow-y: scroll;
        }

        .box{
            width: 120px;
        }
<div class="mainContainer">
           <h3>Without horizontal scroll - no vertical scroll bar</h3>
           <div class="container">
            <div class="box">1</div>
            <div class="box">2</div>
            <div class="box">3</div>
          </div>
        </div>

        <div class="mainContainer">
           <h3>With horizontal scroll- vertical scroll is comming</h3>
           <div class="container">
            <div class="box">1</div>
            <div class="box">2</div>
            <div class="box">3</div>
            <div class="box">4</div>
            <div class="box">5</div>
          </div>
        </div>

If you remove div-4 and div-5, the vertical scrollbar will not appear. I am unsure about how to resolve this issue.

Answer №1

It's a bit unconventional how you structure everything in a grid format. It's definitely not something you see every day.

The issue arises when the horizontal layout takes up some vertical space, which ends up affecting the available vertical space. Unfortunately, the grid rows don't adjust to accommodate for this change.

I cannot definitively say whether this behavior is intentional or if it's simply not specified by W3C and left up to individual browser vendors to interpret.

However, I do have a workaround that may work for your specific case, although it might not be universally applicable. By wrapping the container boxes and separating their responsibilities, along with using position: relative and absolute, you can potentially resolve the issue.

html {
  height: calc(100% - 20px);
}

body {
  height: 100%;
  display: grid;
  grid-template-rows: 1fr 1fr;
}

.mainContainer {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

.container {
  display: grid;
  grid-template-rows: 1fr;
  grid-auto-flow: column;
  width: 500px;
  overflow-x: auto;
  overflow-y: auto;
}

.container-wrap {
  display: grid;
  width: 500px;
  overflow-x: auto;
  overflow-y: auto;
  position: relative;
}
.wrap {
  display: grid;
  grid-template-rows: 1fr;
  grid-auto-flow: column;
  position: absolute;
}

.box {
  width: 120px;
}
<div class="mainContainer">
  <h3>Without horizontal scroll - no vertical scroll bar</h3>
  <div class="container">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
  </div>
</div>

<div class="mainContainer">
  <h3>With horizontal scroll- vertical scroll is comming</h3>
  <div class="container-wrap">
    <div class="wrap">
      <div class="box">1</div>
      <div class="box">2</div>
      <div class="box">3</div>
      <div class="box">4</div>
      <div class="box">5</div>
    </div>
  </div>
</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

Steps to incorporate a personalized design onto a card element within Material UI

As a JavaScript developer, I was tasked by my brother to create a simple webpage for his business. Admittedly, frontend design is not my strongest suit so I decided to utilize Material UI and added some CSS to paste everything together. Right now, I am wo ...

Using setInterval on a batch of freshly generated div elements

I am interested in creating a small website where I can display multiple clocks for various time zones. However, I have encountered an issue with the setInterval function. Below is the code snippet: function addClock () { $("#container").append('& ...

Is there a way to stop my <pre> code from being displayed on the page?

Currently, I am utilizing the google code prettifier found at http://code.google.com/p/google-code-prettify/ This tool functions similarly to stack overflow by enhancing and highlighting syntax when a block of code is input. However, I have encountered a ...

Tips for choosing the injected HTML's List Element (li) while ensuring it remains concealed

How do I hide the list item with iconsrc="/_layouts/15/images/delitem.gif" image when this div is injected by a first party and I am a third party trying to conceal it? $("<style> what should be inserted here ???? { display: none; } </style>") ...

Is it possible for the shadow of a pseudo element to remain lingering behind its corresponding element?

I'm experiencing an issue where the shadow of the pseudo element :before appears in front of its associated element. Check out this JSFiddle link for reference <div class="rectangle"> <p>Lorem Ipsum</p> <a href="#" style ...

Using Razor view form with various submit buttons

In my razor view, I have a BeginForm command surrounded by a loop that generates an HTML table with a submit button in each row. My concern is how do I identify which specific row the submitted form corresponds to when sending it back to the controller? T ...

Having issues with Drupal's lightframe and lightbox functionalities not functioning properly

I recently came across a Drupal 6 project that I have little knowledge about. My goal is to incorporate an Iframe into a lightbox, which seems simple but I'm encountering some obstacles. Despite researching online and attempting to implement the code ...

Show and hide a div with the click of a button

As I embark on rebuilding a website from the ground up, I find myself pondering how to achieve a specific functionality: My goal is for another view to appear when one of two buttons is clicked. How can this be accomplished? I attempted the following app ...

What could be causing my buttons to malfunction once I've applied padding to the container?

I'm struggling with getting my buttons to appear active. They currently don't have any functionality and lack a hover effect when clicked. Despite my efforts to add effects, nothing seems to change. After experimenting with the CSS, I've no ...

What is the method for incorporating a link in an accordion header next to the toggle button with Bootstrap?

I'm attempting to enhance a link in the accordion-button so that only the arrow is clickable for collapsing. The CSS I added below makes the arrow clickable, but I am having trouble getting the link in the accordian-button to work. I want users to be ...

Formulate a multi-line string using a collection in React's JavaScript framework

I'm working on a React function that involves a set and I need to update an HTML element using the data from this set. Below is an example of my code: const updateElement = (mySet) => { document.getElementById('myId').innerHTML = Arra ...

Guide on how to forward the response obtained from an Ajax call to a different HTML page

I am working with a mongoose database that stores data containing an individual's first and last name. The user inputs their first name into a field, triggering an ajax request sent to the file named controller.js. This file generates a JSON response ...

Disappear solely upon clicking on the menu

Currently, I am working on implementing navigation for menu items. The functionality I want to achieve is that when a user hovers over a menu item, it extends, and when they move the mouse away, it retracts. I have been able to make the menu stay in the ex ...

How can I adjust the height of an iframe dynamically using AngularJS?

I wrote a function that resizes the height of an iframe element to always be 100% after it loads: app.directive('iframeAutoSize', [function() { return { restrict: 'A', link: function(scope, element, attrs) { ...

Switching out CSS files on the fly in ASP.NET

Looking to dynamically change the CSS file being used in my ASP.NET Web Application when it's running. Imagine I have two CSS files, red.css and blue.css. I attempted the following method: In the Master Page, I have this link: <link rel="Styles ...

I am having trouble compiling sass in VS Code using the sass --watch command

I've encountered an issue while trying to compile sass through the terminal. Despite having already installed node.js and successfully using Live Sass, I consistently receive the following error message: sass : The term 'sass' is not recogni ...

Is there a way to spin these hexagons around?

Hey there, I need some assistance with a problem. I have to rotate these three hexagons slightly, about 15 degrees or so. The catch is, it needs to work in Internet Explorer only. I've been struggling with this all day and it's getting ...

Is there a sweet syntax for CSS variables available?

What about using this instead: :root { --primary-color: gray; --secondary-color: lightgray; } var(--pc) Is there a more concise syntax available, perhaps shorter than var(--pc)? ...

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

What class is utilized when multiple classes are specified?

When an element has two classes assigned to it and the CSS for the two classes conflict with each other, which one takes precedence? Is there a method to determine which one will be used? For instance: <p class='red small'>Some Text Here& ...