Tips for creating a table that fills the height of its parent div without surpassing it

Issue:

I am facing a challenge with making a <table> element fill 100% of its container's height dynamically, without going over the limit.

Context:

When the height of the <table> surpasses that of the container, I want the ability to scroll through the rows of the table without losing sight of the information above it.

Currently, when the height of the <table> exceeds the container, the scroll bar appears on the container itself, which pushes the content above the table off the viewable page.

Constraints:

I am specifically using <table> elements and do not want to resort to the <div> with display: table method.

Additionally, I am looking for a solution that is adaptable and does not rely on setting a fixed pixel height.

Sample HTML:

<div class="demo-container">
  <div class="demo-header">Please do not scroll me off the page</div>
  <div class="demo-container>
    <div class="table-container">
      <table>
        <thead>
          <tr>
            <th>Table Header</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>The rows are dynamically populated via API and will expand the table's height</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

Sample CSS:

.demo-container {
  height: 100%
  display: grid;
  grid-area: body;
  grid-template-areas:
      'demo-header'
      'demo-container';
  grid-template-rows: 60px 1fr;
}

.demo-header {grid-area: demo-header;}
.demo-container {grid-area: demo-container;}

.table-container {
  height: 100%
  overflow-y: auto;
}

I understand that setting the height of .table-container in pixels and using overflow-y: auto can achieve the desired outcome.

However, I am seeking a solution that does not rely on fixed pixel heights and can adapt to changes on the page. Is there an alternative approach that does not involve exact pixel measurements?

Appreciate any insights!

Answer №1

If you're wondering how to tackle this task, the first step is to consider the setup of the outer element based on the structure of your entire page.

html,
body {
    height: 100vh;
    margin: 0;
    padding: 0;
}

.demo-container {
    display: flex;
    flex-direction: column;
    max-height: 100%;
}

.demo-header {
    flex: none;
}

.table-container {
    overflow: auto;
}

table {
    background: pink;
}

td {
    padding: 15px;
    border: 1px solid #fff;
}
<div class="demo-container">
    <div class="demo-header">Don't scroll me off the page please</div>
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>Table Header</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <tr>
                    <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
                <td>Pretend that this expands the table height past the container</td>
                </tr>
            </tbody>
        </table>
    </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

The Date Picker pops up automatically upon opening the page but this feature is only available on IE10

There seems to be an issue with the date picker opening automatically on IE10, while it works fine in Firefox where it only appears when you click on the associated text box. Does anyone have insight into why this might be happening specifically in IE10? ...

Excessive width of Bootstrap container

Encountering a rather simple issue: the bootstrap container inside my navbar is too wide, causing an unwanted horizontal scrollbar as it expands the body. You can view the problematic page here. The theme used for this site can be found here. What's ...

Parallax Effect in CSS Background

I am currently working on my website at and have implemented a "parallax-bg" class to create a parallax background effect. Interestingly, the parallax effect is working flawlessly for the hero section at the top and the texture in the middle, but it&apos ...

How to position menu on the right side using bootstrap

Hello everyone, as a newbie I have been struggling to align my navigation menu to the right while keeping the logo on the left side. I have tried multiple approaches without success. Currently, I am working with Bootstrap 4.7 and using the navbar code in ...

Creating customizable divider lines with text next to them using HTML in emails

How can I create an adjustable line with a word next to it, ensuring that long words do not wrap onto a second line? I want the line to stay on one line and the left side to shrink accordingly. Also, I need the text input area to be flexible so I can input ...

Personalized AWS Cognito: Strategies for Tailoring Input Field Designs

MY CURRENT CHALLENGE: Within my Vue application, I am utilizing the AWS authenticator for managing login and signup processes. However, customizing its style has proven to be difficult due to the structure being built with shadow DOM elements. https://i. ...

"Interaction with jQuery causes button element to shift position upon resizing of browser

I am encountering an issue with a div element that has jQuery appended to it for a bounce effect. The element is absolutely positioned within its relative parent. However, when the browser is resized, the child element does not remain centrally aligned dur ...

Error encountered in my JavaScript file - Unexpected Token < found on line 1 of the loadash.js script

I am right at the end of creating a sample dashboard with charts using dc.js, but I have hit a roadblock with one error remaining. This error is causing an issue. Unexpected token < on line 1 for loadash.js. The loadash.js file is valid, but for som ...

Retrieve the HTML code from a webpage on a different domain

Looking to extract HTML from another domain. Attempted solution: $.get("http://website1.com", function(data) { alert("Data Loaded: " + data); }); (not working) For example, as the owner of two websites: website1.com website2.com Now, the goal is to ret ...

onpageshow event behaves as expected exclusively on webkit browsers (triggers function solely when visible on the screen)

I am inserting an HTML file using an object tag. The encompassing div of the object tag is hidden with a display:none property. However, I encounter an issue where the onpageshow event that I placed on the body tag of the HTML object behaves differently a ...

Achieve a sliding effect between two divs using CSS only

I'm trying to create a design with 2 divs that are the same size. The first one is visible by default, while the second one is hidden initially. My goal is to achieve an effect where when you hover over the first div, the second one slides upward and ...

Tips for creating an HTML table that fills the entire width of its parent element:

One of the rows in my table contains a nested table. I want both tables to fill up 100% of the width of the parent element. How can I achieve this? Check out the demo here Here's the HTML code: <div class="container"> <table> <tr ...

Ensuring a div remains perfectly centered

Currently, I am new to HTML and still in the process of learning. I am facing a challenge in centering a button within a div, and I have been using margins to position it. While this method works well when the window is fullscreen, the button's positi ...

Step-by-step guide on crafting your own twig with customizable color options using a colorpicker

I attempted to implement a color picker for my HTML form in Symfony, and it functioned flawlessly. However, when I tried using Twig for my form, I encountered difficulties meeting my expectations. I explored the GenemuFormBundle at https://github.com/gen ...

issues with adding class to div element

I have a div section that contains various movie items and overlay elements. <div class="overlay"> <a title="yes" href="#"><div class="yes"></div></a> <a title="no" href="#"><div class="no"></div>< ...

Name of Document (changing from php to ?)

Greetings! Currently, I am utilizing PHP to include the document name as an example. folder/<?phpecho basename(__FILE__, '.' . pathinfo(__FILE__, PATHINFO_EXTENSION));?>_button.png I have observed that using PHP for this purpose and havin ...

Exploring the file attributes within nw.js

I'm in the process of developing a native application using nw.js. I have included the following code snippet: <input id="fileDialog" type="file" accept=".pdf,.epub" multiple/><a id="add" href="#">Add</a> Below is my JavaScript cod ...

Display/Conceal a div using CSS in React JS/Redux

I've been working on a CSS toggle for a div with className toggling. My approach involves using redux to manage the boolean state. Using the redux chrome extension, I can see that the state is indeed changing but the div remains invisible. If you have ...

What is the method for inserting a line break following an input element using CSS?

I am utilizing a service that provides me with dynamic HTML containing labels and inputs as shown below: input:after { content: "\a"; white-space: pre; } <label for="username">Username</label> <input type="text" id="username" nam ...

Responsive Material-UI horizontal navigation bar

Hey there! I'm working on developing a UI using Material-UI, and I want to make it so that the menu can be collapsed into a slide-in style menu when accessed on a mobile device. Currently, I'm using tabs which allow for sliding left and right, b ...