ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'd like to have a scrollable <tbody> in my table with the header staying fixed. None of the "tips" I've found work for me (Firefox or IE). The following:
Does give me a scrollable tbody, but the thead is all compressed to the left, not aligned with tbody. setting thead to width: 100% does nothing. Keeping the "width: 100%", but removing the "display: block" from thead stretches the row all the way across the page, not just to the width of the table. Removing "width: 100%" and adding "display: table-header-group" also stretches the row to full page width.
I can't think of any combinations that will work other than using the 2nd example and hard-coding the width of header elements and body elements -- not what I want to do.
I would hope there is a better way though. Certainly I would have expected the table layout to default to what you are asking, but I guess it's not the case.
One ham-fisted way would be to force the columns to be certain widths.
Yes, I mentioned that, but that's exactly what I'm trying to avoid. Column widths may vary depending on contents.
Quote:
Originally Posted by Turbocapitalist
I got curious and searched a bit. It looks like if you set the scope attribute, it fixes column widths:
That didn't work. Using the scope="cols" exactly as you've shown still puts the header row to full-width of the page. Adding "display: block" compacts the header row to the left. This is true on both Firefox and IE. The scope attribute seems to have no effect at all. Your link does no really say that the scope attribute will align header cells with body cells, nor do the examples I looked at use thead and tbody.
Quote:
Originally Posted by scasey
You're putting each cell on a separate row
:
PS: set width on the <table> tag, not on the <tr> tag.
Actually, no I'm not. Look again. I am specifying more than one row in <thead>, but the cells are not on separate <tr>'s. I do have a width set on the table. I don't have any widths set on the <tr>.
Hopefully you all, or someone, has some additional ideas. Otherwise, it seems like setting explicit widths on the columns is the only solution -- which I'm not going to do. In which case I think the thead and tbody tags need a bit more work to be really useful.
I'd like to have a scrollable <tbody> in my table with the header staying fixed. None of the "tips" I've found work for me (Firefox or IE). The following:
Actually, no I'm not. Look again. I am specifying more than one row in <thead>, but the cells are not on separate <tr>'s. I do have a width set on the table. I don't have any widths set on the <tr>.
IS specifying two rows (<tr></tr>), each containing one cell (<th></th>)
and the only place you've set width is on the tbody tag, which is NOT the <table> tag
Please try my suggestions to see if they give you what you want...then state specifically why they don't, if they don't.
IS specifying two rows (<tr></tr>), each containing one cell (<th></th>)
and the only place you've set width is on the tbody tag, which is NOT the <table> tag
Please try my suggestions to see if they give you what you want...then state specifically why they don't, if they don't.
Perhaps my meta-syntax is throwing you off. I have elipses in the row description: <tr><th> ... </th></tr>, which was intended to mean multiple columns. The elipses were not intended to indicate <th> content. It would have been more clear had I done, <tr><th>text</th><th>text</th> ... <th>text</th><th>text</th></tr>.
In any case, my actual table <thead> section does have two rows of multiple columns. I've also done one row of multiple columns, makes no difference.
That link you referenced is exactly where I went in the first place, having never done anything with thead or tbody tags before.
As an experiment, if you think you have a handle on how this should work, snag the html below and see if any of your ideas let the thead section line up with the tbody. Never mind explicitly setting column widths. I know I can make that work. They also line up OK if there is no block/height/overflow styling on the tbody, but that, I think, is rather the point of having the thead/tbody tags. Otherwise, it's just a normal table and thead/tbody adds nothing.
Code:
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<tr><th>The</th><th>rain</th><th>in</th><th>Spain</th></tr>
</thead>
<tbody style="display: block; height: 80px; overflow-y: auto">
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
</tbody>
</table>
</body>
</html>
I'll assume that "never search" comment wasn't directed to me specifically. I visited all those pages previously. None of the ideas worked. The thead and tbody align just fine until you put the height and overflow-y attributes in the tbody. Try it. Copy/paste my simple example and try it in your browser. I've done some additional test settings based on those very suggested sites that don't work for aligning thead and tbody:
Yeah, I saw that one too, but wow! He's got javascript, I assume running onload, that compares the width of the th cell in the <thead> (assuming all th's are in <thead>) with the width of the corresponding td cell in the <tbody> and sets both cells' widths to the max of either. This example assume only one table in the page as he gets all th and td elements, but that could be handled by adding names or ids or some other identifier to the elements of the target table.
I was hoping for something a bit less complex, but maybe that's as good as it gets. I'll experiment with that and post back.
You might try asking on css-d -at- lists.css-discuss.org before sinking to javascript. That list, if any, would have people who should be able to figure out the answer to getting both the scrolling and the matching columns at the same time. If you do find an answer that is only CSS, please do remember to post it here.
I'm throwing in the towel on this one. I tried a simplified javascript based on AnanthaP's link:
Code:
function adjusttbody()
{
thElements = document.getElementsByTagName("th");
tdElements = document.getElementsByTagName("td");
for (var i = 0; i < thElements.length; i++)
tdElements[i].style.width = thElements[i].offsetWidth + "px";
}
But that didn't work. If I need that elaborate set of css definitions the author of that link example has, it's not worth the trouble.
Turbocapitalist, excellent suggestion with the maillist, but I'm done investing time in this. Besides, if there were a css only solution someone would have probably posted it. I'm on a number of lists and normally those list postings are findable.
I suppose <tbody> could be useful for hiding sections of a table given multiple <tbody> statements, but for scrolling it seems near useless.
I'll revert to an easier solution I've used in the past which is to have 2 tables:
Actually, the <table><div> technique works like a champ with the auto-adjust javascript function. I don't even need to hard-code column widths unless I want to tweak. You do have to set the header table with and the <div> width to line up. To the devil with <thead><tbody>!
Code:
<!DOCTYPE html>
<html>
<head>
<style>
td, th { border: 1px solid red }
table { border-collapse: collapse }
</style>
<script>
function adjusttbody()
{
thElements = document.getElementsByTagName("th");
tdElements = document.getElementsByTagName("td");
for (var i = 0; i < thElements.length; i++)
thElements[i].style.width = tdElements[i].offsetWidth + "px";
}
</script>
</head>
<body onload="adjusttbody()">
<table style="margin: 0; width: 484px">
<tr><th>The</th><th>rain</th><th>in</th><th>Spain</th></tr>
</table>
<div style="width: 500px; margin: 0; height: 80px; overflow: auto">
<table style="margin: 0; width: 100%">
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
<tr><td>Falls mainly</td><td>In the Plain</td><td>Falls mainly</td><td>In the Plain</td></tr>
</table>
</body>
</html>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.