Flexible HTML Table with Fixed Header and Footer around a Scrollable Body

Fairly self-explanatory title but I can’t seem to get all the details working at the same time. I’ve tried to lay out my requirements explicitly—along with my attempt at the end—but feel free let me know if more info is required.

Requirements

  1. Standard HTML table tags, such as thead, tfoot & tbody, are preferred as they provide most of my following requests for “free”.

    • I couldn’t get CSS display: table or display: flex approaches to work, but if you do, I’ll be interested.
    • While I prefer CSS-only answers, realistically, solutions including JavaScript (without dependencies) are just as fine.
  2. Fixed/static/sticky thead and tfoot at the top & bottom of the table, respectively!

  3. Flexible column widths that resize dynamically to the size of their cell contents without any fixed/pre-determined widths, whether that be in px or %.

    • All text should overflow & wrap normally within their cells. Row heights should adjust accordingly. These come standard in a HTML table.
  4. Vertical scrollbar should appear within tbody when max-height is exceeded. This shouldn’t overlap with thead or tfoot, as shown in my “Exemplar” demo.

Demonstration

Here’s my JSFiddle demo that displays 3 different tables, including my target as well as my attempt. It also includes my HTML and CSS code for a normal table populated with an (hopefully) interesting variety of placeholder text and styled to emphasize the consistent cell alignment at any size that comes courtesy of a table.

  • Table 1: Exemplar (Static Image) is just an image of what I’d like the final product to look like. Notice the positioning of the scrollbar within the body of the table and right next to the contents.

  • Table 2: Stock Default provides the HTML table template. Try resizing your viewport to observe how incredibly flexible it is.

  • Table 3: Positioning within Divs uses a combination of: absolute & relative positioning;nested divs within thead & tfoot; and, hard-coded spacing values to fix alignment. It is nowhere near robust enough for any amount of resizing.

Hi,

I remember playing around with this about 15 years ago and there was no fullproof solution then and its still not possible today (using the table-layout:auto algorithm) although it may be possible with the new css grid but haven’t had time to test yet.:slight_smile:

These demos are about 15 years old and are fully fluid but there are issues with the headers.
http://www.pmob.co.uk/temp/table-scroll-double-header.htm
http://www.pmob.co.uk/temp/table-fixed-header-example.htm

If the table can used the table-layout:fixed algorithm which makes equal width cells automatically then there is an easy solution.

https://codepen.io/paulobrien/pen/pPpbYJ/

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
table {
	max-width:980px;
	table-layout:fixed;
	margin:auto;
}
th, td {
	padding:5px 10px;
	border:1px solid #000;
}
thead, tfoot {
	background:#f9f9f9;
	display:table;
	width:100%;
	width:calc(100% - 18px);
}
tbody {
	height:300px;
	overflow:auto;
	overflow-x:hidden;
	display:block;
	width:100%;
}
tbody tr {
	display:table;
	width:100%;
	table-layout:fixed;
}
</style>
</head>

<body>
<table>
  <thead>
    <tr>
      <th scope="col">Header 1</th>
      <th scope="col">Header 2</th>
      <th scope="col">Header 3</th>
      <th scope="col">Header 4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell content with content to wrap as required Cell content with content to wrap as required Cell content with content to wrap as required Cell content with content to wrap as required Cell content with content to wrap as required Cell content with content to wrap as required </td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
    <tr>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
      <td>Cell content</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer 1</td>
      <td>Footer 2</td>
      <td>Footer 3</td>
      <td>Footer 4</td>
    </tr>
  </tfoot>
</table>
</body>
</html>

I just knocked this up based on another demo I did for someone on the forums but it does use one line of jquery,

https://codepen.io/paulobrien/pen/NvxNyY

I’m sure someone in the js forum could convert the jquery to vanilla js pretty easily :slight_smile:

1 Like

Many thanks for your comprehensive response, wasn’t expecting anything this quickly!

It seems those 15 years old demos have definitely stood the test of time, at least as far as the extent of CSS has grown. Especially the first one—double header—which has a full width tbody and an integrated scrollbar that’s positioned under the thead. As you mentioned though, thead’s cell contents don’t wrap around as one would expect. Plus, tfoot isn’t fixed to the bottom either but scrolls along with tbody instead.

The first CodePen with table-layout: fixed didn’t work out so well for me as the thead and tfoot didn’t resize proportionately to tbody. I assume this is because of the fixed width columns of tbody.

The second Pen you made is extremely close to my dream-come-true! It’s based on standard, semantic HTML table and both thead & tfoot contents resize/wrap accordingly to tbody perfectly. However, the scrollbar appears outside of the table, instead of between thead and tfoot. Furthermore, the scrollbar width is manually hardcoded so it’s inflexible across different OS/browser configurations. As I mentioned previously, I’d prefer CSS-only but I can totally understand that sometimes a touch of jQuery is needed to sort some bits out dynamically. On the other hand, using jQuery to duplicate the table two extra times is quite a performance hog, especially anywhere near large-scale data. These couple of things prevents me to accepting your answer as the complete solution.

Nevertheless, I’ve added your answer to my Pen (shown below) to get a better idea of how closely it fits with my vision shown in Table 1: Exemplar. Once again, many thanks for your response I really appreciate your efforts here. :smiley: :+1:

See the Pen Fixed Header & Footer Tables by Rishav Dhar.

No worries, it was fun :slight_smile:

Note I changed the js in my demo as it duplicated the table again in error.

Yes, that’s a downside of that approach. The only way I can think to overcome this is to have a height for the header and footer that is tall enough to allow the content in the header to wrap. Usually that would not be an issue as header content is not usually full of content like the cells. If we take that approach then you can get close to your desired outcome of a scrollbar inside the header and footer but will also display correctly on the mac where the scrollbar is overlaid when required. We have to lose the borders on the header though because as the header row is slightly longer depending on browser the borders won’t always match up exactly.

Yes, if you have very large tables then that is a downside but I would wager that they would need to be extremely large before a performance hit was noted. On the contrary all other js versions I’ve seen of fixed headers and footers are extremely ‘janky’ to start with as they have to dynamically update the headers and footers while scrolling etc.

The problem with all the pure CSS solutions is that they need to set tbody to block in order to get the vertical scrollbar on tbody but once you do that then there is no way to have a relationship between the table header and footers as they effectively become separate tables.

I do wonder if css grid can supply a solution but I’m guessing you would need nested grids in order to get the scrollbar and then you probably lose the relationship between the parent grid columns. (I’ll have to experiment when I have a few hours free :)).

Maybe @Erik_J or @ronpat have some suggestions as they always like a challenge :slight_smile:

It may also be of interest that we ran a quiz on this about 8 years ago at Sitepoint although it was only for a fixed header but makes interesting reading all the same.:slight_smile:

1 Like

Thanks for getting back to me!

This Pen looks even better than the previous demos. Loving the look of the integrated scrollbar within tbody. Like you said, as soon as borders get involved, it becomes obvious that cell alignment is fairly out-of-whack. Even though the thead and tfoot resize & wrap-around as expected, it’s clearly out of sync with tbody while their extended height (to accommodate wrapping) isn’t really ideal; row height should change only when necessary. In this regard, the Pen from your previous post was more on top of this, but I guess that makes sense given the different methods the 2 tables are rendered.

Fully agreed with you on the use of jQuery. There are good reasons why it’s still a popular library over as long as it has existed. Sometimes, CSS-only—even with a dash of vanilla JS—doesn’t quite cut it, especially in these “supposedly tricky” situations.

Just browsed through the winners of the Quiz & now I can see how your original demo came about. But with my request of a perfectly fixed header & footer with internally scrollable table contents, I daresay I’ve arrived upon another quest for the “Holy Grail”! I wonder if Grids will answer my calls as Flex did for the HTML Holy Grail layout… Just hope it’s sooner rather than later :anguished:

On a somewhat separate note, I’ve been experimenting with the CSS display: table style to see how that factors into this whole debacle (shown as Table 5 on my Pen). From one point of view, it completely throws HTML table semantics out the window, but then again, all of that is retained via the raw CSS. In fact, it’s retained a bit too well since I’ve essentially arrived back to the stock default table but using divs with classes instead of the semantic elements. The issue of display: block or position: absolute plagues this table just as it does on an actual HTML table. At the very least, I did intend on adding hyperlinks from certain cells/rows at a later stage and this provides a very simple way of doing just that using basic anchors wherever I want to place them; something that I didn’t find an easy way of doing with a regular table (especially clickable rows). So it’s not a complete loss!

On a somewhat somewhat separate note, I’ve been thinking about an alternative way to approach my issue with regards to the internal/integrated scrollbar. What if I create a JS function to dynamically determine the width of the scrollbar onLoad then use that value to add extra padding-right to thead & tfoot? That should take care of points 1, 3 & 4, just need to then work out how to best fix their positions in relation to the table. Actually, just realised point 2 is the main crux of the matter which affects/breaks everything else. :frowning:

Didn’t have much luck with CSS grid as nested grids lose their relationship with the parent. Managed to get a grid version with position:sticky but only Chrome and Firefox support though.

I think for the time being the only full proof solution will be to use js and perhaps a custom scrollbar so that you can control the widths etc. Currently browsers on the mac system and IE edge don’t show scrollbars until you need them and on the mac they overlay the content and don’t take up room .

Note that all the demos above are just proof of concept and untested so there may be drawbacks I haven’t noticed yet.

1 Like

Hi @PaulOB and @rishavdhar96

Sorry I have no time to spend ftm.
Though I can post an old demo of a vertical table scroll taking a noscript approach for the fixed headings:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<title>Table Vertical Scroll</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="PSPad editor, www.pspad.com">
<style>
html, body{ box-sizing:border-box}
*, *:before, *:after{ box-sizing:inherit}
html{ font:16px/1.2 arial, sans-serif}

.table-scroll{
	position:relative;
	margin:auto;
	outline:1px solid;
	outline-offset:-1px;
	min-width:48em;
}
.table-wrap{
	max-height:40vh;
	overflow-y:auto;
	line-height:1.2;
}
.table-wrap:before,
.table-wrap:after{
	position:absolute;
	z-index:0;
	top:0;
	right:17px;
	left:0px;
	border:solid;
	border-width:1px 0 1px 0;
	padding:5px;
	background:#ff6;
	line-height:1.6em;
	content:"\a0";
}
.table-wrap:after{
	top:auto;
	bottom:0;
	background:#eee;
	line-height:1em;
}
.main-table{
	border-collapse:collapse;
	width:100%;
}
.main-table th,
.main-table td{
	border:1px solid;
	padding:5px;
	max-width:15em;
	background:#ffc;
	vertical-align:top;
	text-align:left;
}
.main-table th,
.main-table tfoot td{
  vertical-align:middle;
	font-weight:bold;
	text-transform:capitalize;
	white-space:nowrap;
}
.main-table thead th{
	background:#ff6;
	line-height:1.6em;
}
.main-table tbody th{
	background:#ff9;
}
.main-table tfoot td,
.main-table tfoot th{
	line-height:1em;
	background:#eee;
}
.main-table thead th:before,
.main-table tfoot th:before,
.main-table tfoot td:before{
	position:absolute;
	z-index:1;
	top:1px;
	left:auto;
	margin-left:-5.55px;
	border-left:1px solid #000;
	padding:5px;
	background:inherit;
	white-space:inherit;
	line-height:inherit;
	font-weight:inherit;
	content:attr(data-th);
}
.main-table tfoot th:before,
.main-table tfoot td:before{
	top:auto;
	bottom:1px;
	line-height:1em;
}
</style>
</head><body>

<div class="table-scroll">
	<div class="table-wrap">
		<table class="main-table">
			<thead>
				<tr>
					<th scope="col" data-th="header 1">header 1</th>
					<th scope="col" data-th="header 2">header 2</th>
					<th scope="col" data-th="header 3">header 3</th>
					<th scope="col" data-th="header 4">header 4</th>
					<th scope="col" data-th="header 5">header 5</th>
					<th scope="col" data-th="header 6">header 6</th>
					<th scope="col" data-th="header 7">header 7</th>
					<th scope="col" data-th="header 8">header 8</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th scope="row" data-th="footer 1">footer 1</th>
					<td data-th="footer 2">footer 1</td>
					<td data-th="footer 3">footer 3</td>
					<td data-th="footer 4">footer 4</td>
					<td data-th="footer 5">footer 5</td>
					<td data-th="footer 6">footer 6</td>
					<td data-th="footer 7">footer 7</td>
					<td data-th="footer 8">footer 8</td>
				</tr>
			</tfoot>
			<tbody>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell &lt;br&gt; <br>&lt;br&gt; <br>&lt;br&gt;<br>content</td>
					<td>Cell content longer</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content longest longest longest longest longest longest</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
				<tr>
					<th scope="row" data-th="row header">row header</th>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell</td>
					<td>Cell content</td>
					<td>Cell content</td>
					<td>Cell content</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

</body></html>

I think I have a horizontal version too. I’ll post it later if I find a copy. :slight_smile:

1 Like

A couple of years ago, I spent several days (over a period of weeks) mulling over a “spreadsheet” style layout with fixed header and footer but eventually decided that HTML and CSS could not emulate the ability to position header and footer like Excel allows, yet. I haven’t gone back to it since. Maybe I’ll give it another look, but not right now. Sorry.

@PaulOB It’s amazing how those grids look fine on Chrome but completely bonkers on IE! :laughing:

I think JS (+ jQuery) is definitely needed for the full-proof solution but I think custom scrollbar might be going too deep, given that I’m not sure how that’d help with positioning the scrollbar beside tbody and sandwiched between the fixed thead & tfoot.

Having said that, I found some jQuery custom scrollbar examples that look terrific across all major browsers and might be worth considering, even if only from a visual/aesthetic point of view.

@Erik_J Thanks for your response!

I think using the data-th custom attribute to copy header content into the CSS is a novel idea of doing things. This gets around having to clone the entire table with minimal hit to HTML semantics. I created a JSFiddle demo based on your code to see it in action.

Unfortunately, it still has some flaws such as the non-wrapping table headers and scrollbar outside the table. Also, I’m finding it quite tricky to adapt the CSS to my table’s markup & custom styling due to the way spacing is handled using hardcoded values, but I guess that’s more on me than the code. You can see my attempt at adapting your code here while my target visual can be found on CodePen.

1 Like

@ronpat I fear the CSS standards haven’t changed much over the last couple of years as far as fixed headers on table go. I thought I wasn’t asking for too much with my 4 requests but I guess I underestimated the complexity involved. :anguished:

Thanks Erik :slight_smile:

I did have a similar version but it suffered from the same problem in that you can’t have a variable height header with wrapping content.

Yes, I was surprised how badly grid works in Edge. I believe Edge will be get the correct version soon. The delay was because IE implemented grid before everyone else but the spec changed afterwards.

Hi,
I’d be happy to try adapt the css to your demo table when I find the time. :slight_smile:

I think I found the old example of a horizontal scroll version that has the flexibility you want, though it isn’t vertical. :grin:

The demo example for a horizontal table scroll using the data attribute with some explaining of how it works:

Table-Horizontal-Scroll.html (6.0 KB)

I uploaded a version to JsFiddle to play with: https://jsfiddle.net/Erik_J/hzy53qc8/

1 Like

Hi Erik,

Nice demo:)

I notice there’s a bug/behaviour in Chrome where your text-transform capitalize is causing a problem. Some of the left column cells are overlapping the next row because you are losing the capital letter of the first word in the th content. The data-th is being capitalized ok but the original content is losing the capital on the first word thus making the content shorter and when some words wrap they cross over into the cell below.

You can solve the bug by including a space after the content in the th.

e.g.
<th scope="row" data-th="row header longer longer longer longer longer longer longer ">

Note the space at the end of the line.

I’m assuming that when you use:before then Chrome assumes that the item inserted is the first letter and you lose the capital on the first word of your normal data.

I still don’t think this addresses the fluid nature of the cells as you have simply moved the fixed height restrictions of the vertical demo into a fixed width restriction in the horizontal version. :slight_smile:

My original js demo is fully fluid in width and height for header, footer and content (although it does have problems in other areas as already mentioned) :slight_smile:

1 Like

I re-jigged my JS demo to use vanilla js instead of jquery and I fixed the scroll bar differences between browsers by using a holding div that was set overflow scroll so that the cloned tables are placed inside that div and thus match exactly the original content. (I’m sure the JS could be tidied up a lot but its only half a dozen lines anyway).

It doesn’t matter what size the browsers scrollbars are (or whether they are overlaid when scrolling as on mac systems) as the content will always match up exactly

The fix comes at the expense of an empty div but the table remains untouched and doesn’t need any special formatting. It should work in all modern browsers (ie11+ - maybe older but haven’t tested).

The table headers and footers are fully fluid both in width and height and have no restrictions upon them.

As mentioned earlier there may be overheads in cloning large tables so use at your own risk or test with care. I’m not sure of the accessibility implications either so probably need to mark up with aria:hidden rules.

1 Like

I’ve started so I’ll finish :slight_smile:

Here’s an improvement that only needs to clone the table once and is just a few lines of vanilla js.

As before this is fully fluid in width and height.

So far looks promising and seems to work in IE11+ and other modern browsers.

Usual caveats apply.

2 Likes

Thanks for debugging my poor example. :slight_smile:

I think I’ll just add a trailing space to the data attribute:
content:attr(data-th)"\20";

Impressive solution you recently posted (#16), especially the natural scrollbar! :slight_smile:

I’ll try improve my noscript version next free moment. :sweat_smile:

1 Like

:ballot_box_with_check: Standard HTML table & its constituent semantic elements.
:ballot_box_with_check: Fixed thead & tfoot at the top & bottom of the table, respectively.
:ballot_box_with_check: All column widths resize automatically depending on cell contents.
☐ Vertical scrollbar within tbody.

@PaulOB This is greatness so far. Even ignoring the last requirement, this is an amazing feat as it is! I have some follow-up questions for when you can spare some time:

  1. Looking at the JS, is there a difference between the 2 functions that’s called? I think one of them is meant to be removed/commented out since they both appear to be identical.

  2. Forgetting the 4th requirement for the moment & focusing on your execution of it, have you taken into account when the table isn’t populated enough to necessitate a scrollbar? When that’s the case, the hidden scrollbar hangs off the end of the table & pushes each column further out of alignment consecutively. When the scrollbar is an overlay, it doesn’t look so iffy but the misalignment is still present.

  3. … and returning to the “vertical scrollbar within tbody” request, I wonder if a slight modification could be made to it in your execution: can the scrollbar have margins at its top & bottom that corresponds to the height of thead & tfoot cut away from it? If that’s possible, the CSS can be adapted so that it’s not obvious whether the scrollbar is overlaid within the table or beside it. Removing borders & merging together background-colors could make it look just as if it’s inside the table, without actually adding any of the headache associated with actually doing that.

I can’t thank everyone enough for their input & insight into this. Really much appreciated :slight_smile:

Hi,

You just caught it when I was playing around and only the first one is needed. The Js could be condensed into one line of js but becomes less readable so I reverted it back to original. If you refresh you should see latest version.

That can be overcome by simply setting a height on the table that matches the other heights we set for the wrap (all 300px). In that way if there is only one row then the whole row stretches from header to footer. If there are two rows they take up 50% and so on until there are more rows than the height allows and then the height expands to allow the scroll automatically.

You could use js detect if the table was taller than the 300px height (or whatever height you have set) and then only add the fixed header and footer but that would complicate it a little I feel and I like the way it works at present and remains fairly simple.

The code is updated so just copy and remove rows etc to see how it behaves.

The problem with moving the thead and tfoot above and below the scrollbar is that you can never know the height of the thead/tfoot unless you constantly monitor them as they will change when the window is resized and content wraps. This would mean tying into the resize event in JS and then every time the screen size is changed you would need to update the thead and tfoot positions accordingly. This then becomes an intensive task for the browser (even with throttling) and leads to the ‘janky’ operation of the whole window when resized and something that will annoy most users. It’s not impossible but something you would want to avoid most of the time unless its something that you can’t do without.:slight_smile:

1 Like

If you don’t mind cloning the table twice then it can be done.

I’ve only just knocked this up so haven’t had time to test and I am away for the evening now.

Most of the rules are cosmetic in hiding and showing borders etc.

2 Likes