Ensure the table body scrolls the full height of the page while maintaining a fixed header when the table is out of view

My goal is to implement scroll bars for overflowed content within a single table on the page. Despite reading numerous posts on this topic and experimenting with various methods like setting height to 100% or using flex layout, I still can't seem to achieve the desired outcome.

The visualization provided should make it easier to understand. The blue squares indicate tables while the red/green ones represent divs.

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

Whenever I assign a specific height value to ancestors of the table, the scrollbar works properly. However, the challenge lies in not knowing the exact height in advance.

For instance, the total height of the page depends on factors such as screen size and browser window dimensions.

My aim is to have the green div and its associated table expand to fill up all the remaining screen height. While this approach almost works, the final height exceeds the available screen space.

Additionally, applying 'height: 100%' to the table results in an overly long scroll bar that extends beyond the window's boundaries.

Here is a plunk showcasing the concept. Unfortunately, it doesn't function as intended. What am I overlooking?

Answer №1

I believe I have grasped the concept correctly ;)

thead tr:first-child th {
  position: sticky;
  top: 0;
  color: #fff;
  background: red;
}

body {
  height: 100%;
  margin: 0;
  padding: 0;
}

thead tr {
  background-color: #558;
}

thead tr th {
  width: 50px;
}

tbody tr {
  background-color: black;
  color: white;
}

.pageHeader {
  text-align: center;
  height: 50px;
  background-color: #ddd;
}

.pageBody {
  height: calc(100vh - 50px);
}

.mainTable,
.centerTable,
.rightTable {
  vertical-align: top;
}

thead tr:first-child th {
  position: sticky;
  top: 0;
  color: #fff;
  background: red;
}

.theTable {
  width: 100%;
}

.mainTable {
  display: inline-block;
  height: 100%;
  width: 300px;
  background-color: #8a8;
  overflow: auto;
}

.centerTable {
  display: inline-block;
  height: 50px; 
  width: 150px;
  background-color: #550;
}

rightTable {
  display: inline-block;
  height: 50px;
  width: 150px;
  background-color: #550;
}
<div class="pageHeader">Page Header</div>
<div class="pageBody">
  <div class="mainTable">
    <table class="theTable">
      <thead>
        <tr>
          <th>AA</th>
          <th>BB</th>
          <th>CC</th>
        </tr>
      </thead>
      <tbody>
        <!-- ngRepeat: row in rowList -->
        <tr ng-repeat="row in rowList" class="ng-scope">
          <td class="ng-binding">0</td>
          <td class="ng-binding">0</td>
          <td class="ng-binding">0</td>
        </tr>
       ... (repeated code)
      </tbody>
    </table>
  </div>
  <div class="centerTable">
    Center table with fixed height/width
  </div>
  <div class="rightTable">
    Right Table with fixed height/width
  </div>
</div>

Answer №2

thead tr { background-color: #558; }
thead tr th { width: 50px; }

tbody tr { 
  background-color: black;
  color: white;
}

.pageHeader {
  text-align: center;
  height: 50px;
  background-color: #ddd;
}

.mainTable, .centerTable, .rightTable {
  vertical-align: top;
}

.centerTable {
  display: inline-block;
  height: 50px;
  width: 150px;
  background-color: #550;
}

.rightTable {
  display: inline-block;
  height: 50px;
  width: 150px;
  background-color: #550;
}

.mainTable{
    height: calc(100vh - 66px);
    display: inline-grid;
    width: 300px;
    background-color: #8a8;
    overflow-y: scroll;
}

body, html {
    height: auto;
}
<div class="pageHeader">Page Header</div>
<div class="pageBody">
  <div class="mainTable">
    <table class="theTable">
      <thead>
        <tr>
          <th>AA</th>
          <th>BB</th>
          <th>CC</th>
        </tr>
      </thead>
      <tbody>
        <!-- ngRepeat: row in rowList -->
        <tr ng-repeat="row in rowList" class="ng-scope">
          <td class="ng-binding">0</td>
          <td class="ng-binding">0</td>
          <td class="ng-binding">0</td>
        </tr>
        <!-- end ngRepeat: row in rowList -->
        ... (trimmed for brevity)
        <tr ng-repeat="row in rowList" class="ng-scope">
          <td class="ng-binding">99</td>
          <td class="ng-binding">198</td>
          <td class="ng-binding">297</td>
        </tr>
        <!-- end ngRepeat: row in rowList -->
      </tbody>
    </table>
  </div>
  <div class="centerTable">
    Center table<br>fixed height/width
  </div>
  <div class="rightTable">
    Right Table<br>fixed height/width
  </div>
</div>

Note: Internet Explorer, Edge 15 and earlier versions do not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one of top, right, bottom or left for sticky positioning to work.

.mainTable{
    height: calc(100vh - 66px);
    display: inline-grid;
    width: 300px;
    background-color: #8a8;
    //-webkit-overflow-scrolling: touch; // for Safari
    overflow-y: scroll;
}

body, html {
    height: auto;
}

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

What is the best method to restrict the size of a div to match the dimensions of the

In my webpage, I have a menubar (div) that contains bookmarks. However, when too many bookmarks are added, the menu becomes too wide for the page size I prefer (1280, 720) and becomes scrollable, causing some bookmarks to be out of view. My goal is to ens ...

Changing position with flair in CSS

Is there a way to create a transition effect on an element when it changes position from static to fixed? I attempted using the code: transition: position 2s linear;, however, it does not seem to have any impact. Are there any other methods I could experi ...

Update background to full screen and include text when hovering over DIV

Currently, I am utilizing Bootstrap and have a layout with 6 columns containing icons/text within. I desire to implement a hover effect where each column triggers a background change in transition for the entire section. In addition, text and button elemen ...

Achieving a seamless scrolling effect using CSS

I recently completed the design of a basic website that includes both mobile and desktop versions. However, I am looking to incorporate some animation into the site to enhance its appearance. One specific feature is a "go up" link at the bottom of the mob ...

Hiding content using the "display: none" CSS property may not function properly

I am facing an issue where my html code for email templates works perfectly on all platforms except for Microsoft Outlook Desktop. The problem lies in the display of product information that is not showing correctly in Outlook. Within my code, I have keys ...

Arrange the footers in the bootstrap card deck to be perfectly aligned at the top

Using bootstrap 4 to showcase cards has been successful, but there is an issue with footers that have varying content lengths. Instead of the footer adjusting its position based on the content, is there a way to keep the tops aligned while pushing the cont ...

Tips for positioning your side navigation menu parallel to the page edge

Im looking to have the side navigation floated to the left as I scroll down the page. I tried using the sticky property, but it didn't work out. Here's a snippet of the code: .sticky-class { position: sticky; top: 30px; } This is how my HTML ...

The CSS file is not connecting properly despite being located within the same directory

I have double-checked that both of these files have the correct names and are in the exact same directory. However, for some mysterious reason, the CSS is not applying any styling to my page. I would greatly appreciate any assistance with this issue! Belo ...

Is it possible to apply CSS based on a component's displayName?

Are you a CSS pro? I'm attempting to apply a class that will make all descendants of an element read-only, but for some reason the style isn't being applied as expected: #ComponentDisplayName * { -webkit-user-select: text; -moz-user-sel ...

Is the first child component of Material UI Stack not properly aligned?

Is there a CSS issue that needs fixing? I am working on creating a vertical list of checkboxes with labels using the <Stack /> component from Material UI. I have tried implementing this in the sandbox provided (check out demo.tsx): https://codesandb ...

Revamp the Bootstrap Carousel Control by replacing the default navigation arrows with stylish arrows featuring circle backgrounds

Looking to update the appearance of the carousel icons in Bootstrap 4? How about changing them from simple arrows to arrows with dark semi-opaque circles underneath? If you're wondering how to achieve this modification, keep in mind that the icons ar ...

Align an image in the middle with a heading

I am currently working on aligning a picture and heading in a header section. My goal is to center both elements, with the picture being twice as tall as the heading. So far, I have them stacked one above the other: .body { font: 15px/1.5 Arial, Helveti ...

Tips for containing `overflow` within a flexbox container

I've organized my body space into three sections: header, content, and footer. To achieve a sticky footer effect at the bottom, I used the flex property in my layout. However, I'm struggling to add a scrollbar to my main container box. I've ...

Pressing a button triggers the highlighting of a tab in HTML through the use of Javascript

In the corner of my webpage, I have three tabs: info, question, order. When I click on one tab header, only that tab should highlight. The info section includes two buttons that link to the question and order tabs. When these buttons are pressed, the respe ...

The CSS transition for a DIV on an iOS Safari browser is experiencing a lag

In an attempt to optimize CSS transitions in the IOS Safari browser, I decided to use the transform: translate() property instead of changing the left property like other suggestions. Unfortunately, this approach did not yield the expected results as the ...

What are some tips for keeping design proportions consistent on mobile apps?

Hey there, I have a question that may seem basic to some, but as a newbie in coding, I appreciate your patience. I've been working on an Ionic app and I'm struggling with maintaining CSS proportions of HTML elements. For instance, here's th ...

css - aligning text below another text in the center

Looking to style a text in a specific color with a message about another text below it? Want the text centered but not sure how to position it correctly? margin: 0px auto; If this code isn't delivering the desired result and is positioning your text ...

Creating a CSS Grid with Full-Width Columns and Consistent Margins

Currently, I am attempting to create a responsive grid with 4 columns that expands to the entire width of the screen on desktop displays. However, when I introduce margins to space them out evenly, I encounter an issue where the last column either doesn&ap ...

Can a stroke in an SVG image be created using a gradient color?

Is it possible to create a gradient in the stroke color of these two lines, currently set to #389967, within this SVG? .chart-three svg .circle-foreground { stroke: #389967; stroke-dasharray: 494.55px 549.5px; stroke-dashoffset: 494.55px; stroke- ...

Using Javascript to prevent css from changing the display

I want to make some CSS adjustments to a single element using a single script, but I want all the changes to be displayed at once. For example: $("#someelement").css({width:"100%"}); //this change should not be displayed $("#someelement").width($("#someel ...