The bottom div's scrollbar will only show up when the viewport is smaller than the div itself, independent of the presence of another div at the top

Seeking to create a header with flex items within it, allowing for variable height. Below that, looking for a scrollable area that fills the remainder of the browser viewport exactly.

Attempted code:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <base href="/"
</head>
<body style="height:100vh;overflow:hidden">
    <div>
      Variable height
      <hr><hr><hr><hr><hr><hr><hr>
    </div>

    <div style="height:100%">
      object
      <div style="height:100%;overflow-y:scroll">
        <ul>
          <li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li>
          <li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li>
          <li>16</li><li>17</li><li>18</li><li>19</li><li>20</li>
        </ul>
      </div>
    </div>
</body>
</html>

Encountering an issue where the scrollbar does not appear when resizing the browser to a smaller height as expected: the scrollbar only appears when the clipped portion of the lower div is equivalent in height to the header. Experiencing this problem in chrome and firefox, suggesting possible misuse of styles.

Why does the header height affect the overflow of the second div? Is there a correct way to achieve the desired layout, potentially using a different method?

Answer №1

Many browsers have default padding or margins set on the html and/or body elements, which can impact other elements when using percentage values relative to the viewport dimensions.

To address this issue, I always ensure to reset these styles.

Here is an example:

html, 
body {
  padding: 0;
  margin: 0;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  flex-grow: 0;
  display:flex;
  align-items: center;
  justify-content: space-between;
  white-space: nowrap;
  flex-wrap: wrap;
}

.header > div {
  padding: 6px;
  flex: 1 1;
}

.scroll-box {
  flex-grow: 1;
  overflow: auto;
  padding: 6px;
}
<div class="container">
    <div class="header">
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
        <div>Variable height</div>
    </div>
    <div class="scroll-box">
        <ul>
          <li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li>
        </ul>
    </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

Vue 3 now allows for disabling the automatic renaming of CSS properties like "top/bottom/left/right" to "inset"

I noticed that Vue (or maybe Vite) automatically changes my css style attributes from "top: 0; right: 0; left: 0; bottom: 0;" to "inset: 0px;" However, this adaptation doesn't work well for older browsers that do not support the i ...

Achieving a smooth transition from a blur-out effect to a blur-in effect on a background Div

I created a blur in/out effect for my landing page, but it's not functioning as expected and I'm not sure why. It blurs out correctly, but the issue arises when I want the underlying Div to fade in. Currently, it just appears abruptly after the ...

Retrieving input field value in HTML through a button press

Feeling stuck trying to incorporate an input field value into an ajax call? Here's the code I have so far for a button and input field combination, but I can't seem to get it working. function submit() { $.ajax({ type: 'POST&apo ...

Adjust the appearance of an element that is being inserted using .before() by utilizing a variable

Using the jQuery function .before(), I am creating an element in this format: d.children().first().before("<div class='test-div'><span class='test-span'>" + text + "</span></div>") I want to style the span dyna ...

Is there a way to prevent the content in the <p> tag from wrapping and hiding its overflow?

Is there a way to use tailwindcss to ensure that the first <p> child truncates or hides overflow as its container shrinks? I've tried using classes like whitespace-nowrap and overflow-hidden, but the text always wraps regardless. Any suggestions ...

Using DataTable with backend processing

Has anyone successfully implemented pagination in DataTable to dynamically generate the lengthMenu (Showing 1 to 10 of 57 entries) and only load data when the next page is clicked? I'm currently facing this challenge and would appreciate some guidance ...

Issues arise when attempting to alter the background image using jQuery without browserSync being activated

I am facing an issue with my slider that uses background-images and BrowserSync. The slider works fine when BrowserSync is running, but without it, the animations work properly but the .slide background image does not appear at all. Can someone help me ide ...

Guide to pre-populate a text field within a WKWebView on a website with the use of Swift

I am a beginner in the world of coding, specifically with Swift. My goal is to allow users to input their username and password in the settings section, which will then automatically populate the login page on a WKWebView that I have created. Currently, I ...

Background does not extend beyond the visible screen area

I'm currently working on a website project for the students at my school, but I've encountered a minor issue. When scrolling left or right, the background doesn't extend to cover the entire page; instead, it stops where the edge of the page ...

What causes a fixed position div to extend beyond the boundaries of the main container?

Below is the CSS for the fixed div: .top-container { position: fixed; width: 100%; z-index: 999; } Upon zooming out the screen, the div causes the main container to break on the right side while the left side remains unchanged. Refer to the screenshot be ...

Utilizing a fallback option for HTML5 video with a flash player and the ability to manipulate the

My dilemma involves an HTML5 video where I am utilizing jquery to interact with the player using the code snippet: video.currentTime += 1;. However, when Internet Explorer switches to Flash plugins, my JQ commands cease to function. How can I manage and co ...

Detecting Pixel Colors Across Multiple Overlapping Canvases

I have a challenge with multiple canvas elements on my webpage. I am trying to retrieve the pixel color of all overlapping canvas elements when they are stacked on top of each other. Let me illustrate with an example below. In this scenario, I am attempt ...

Expansive Child Division with Ample Margins

I'm currently working with a nested set of divs and I need the inner div to occupy the full width without extending beyond the parent div's margins. Despite trying to use max-width:100%, it hasn't been successful so far. For this particular ...

Steps to insert an h2 element within a MeanMenu container

My current project involves working on a website that utilizes the meanmenu plugin. I have been facing a challenge for the past two days in trying to display a title inside the mean menu container. Here is the HTML code snippet for the menu: ...

Ensure that the menu remains hovered even after navigating to the submenu

I have successfully created a menu using CSS and JavaScript that reveals images and text upon hovering over the menu sections. However, I am facing an issue where the colors of the menu items revert back after hovering out. You can view the website here: ...

Guidelines on connecting this button to a different webpage

Click here for the code snippet <div class="sub-main"> <button class="button-two"><span>Hover Me</span></button> </div> I am new to this and struggling with where to place the code snippet to link a button to my websit ...

The alignment of the text is incorrect and needs to be fixed

In my code, I have an if statement that determines how the page should be rendered. <script language="JavaScript"> if (NS4) { document.write('<LAYER NAME="floatlayer" LEFT="22" TOP="60">'); } if ((IE4) || (NS6)) { document.w ...

Mobile View Not Displaying Bootstrap Column

I'm currently working on creating a two-column layout, one for text and the other for an image. Everything appears correctly on desktop view, but on mobile devices, the image column is not showing up. How can I adjust it so that on mobile devices, the ...

Issue with submitting input fields that were dynamically added using jquery

I created a table where clicking on a td converts it into an input field with both name and value. The input fields are successfully generated, but they do not get submitted along with the form. In my HTML/PHP : <tr> <f ...

Adjust the size of divs using JQuery UI's draggable feature

Looking for a way to make two divs share 100% of their parent's width, with a dynamic resizing feature in between them? Check out my solution here: http://jsfiddle.net/aRQ7a/ However, I'm facing an issue where when I decrease the size of the pre ...