tags:

views:

400

answers:

3

I have some data with one row of headers and one or more rows of values that I want to display. Currently, I'm using an HTML table, but I've come across situations where I have so many columns, the table goes off the end of the screen. Or that some column names are so long (they're usually just a single word that have underscores as spaces), even if there aren't that many columns, the table still goes off the end of the browser window.

What I want is to be able to display all data without having to scroll horizontally. Even if that means breaking the data up into multiple tables.

I'm currently using Embedded Perl, and one hackish solution I've used is to just hardcode 8 columns per table, and start a new table under the first if necessary. But ideally, what I want is to dynamically put as many columns as possible on one table, and wrap to a new line of rows/tables/whatever if necessary.

Can this be done HTML/CSS-only? Or do I need to somehow calculate how much width each column will take, and base my number of columns per table on that? I don't have to use HTML tables, so if there's a better solution, I'm more than willing to use it.

+1  A: 

What you can do is set a width on each column based on how many columns you have. The best solution would have a percentage width, so the table width can change as the page width changes.

Scott M.
A: 

If you make multiple tables then I'm assuming that's going to make you have to scroll vertically down the page to see all the data.

Another solution may be to wrap the table in a div, set a width on the div, and set that div's overflow to scroll. This way the whole page won't scroll horizontally, but the div containing the table will have a scrollbar.

kbosak
+2  A: 

Using table { display: inline; } combined with one table per column should solve your problem. Run this and load the output in a web browser. When you resize the window the columns that won't fit drop down. You might want to use a class with the tables if you don't want all tables affected.

#!/usr/bin/perl

use strict;
use warnings;

print "<html><head><style>table { display: inline }</style></head><body>";
for my $col (1 .. 100) {
    print "<table><tr><th>$col</th></tr>";
    for my $row (1 .. 10) {
     print "<tr><td>$row</td></tr>";
    }
    print "</table>";
}
print "</body></html>";
Chas. Owens
Thanks! I tried it out and found this works pretty well. I have one additional question. Is there a way I can have all the tables in any given row to take up the entire width (of the browser or a div)? Right now, if I have multiple rows of tables, the right side will be all staggered, depending on the widths of the individual tables in that row. It's not that big a deal, but it'd be nice to have the rightmost table in each row all aligned vertically.
accelerate
There is no way to know in advance which rows will be on the end, so there is no way to get them to line up.
Chas. Owens