I’m trying to figure out a way to put a table in a web page in which the columns contain rows (figure 2) instead of the other way around (figure 1).
Making a table in which rows contain columns is simple because of the TR
tag, but there is no corresponding TC
tag to allow tables to work the other way around.
I did manage to find this one, single, proposal to the W3C, but it was not responded to and seems to have gone nowhere.
Obviously the W3C did not deem it worth implementing, so I am trying to figure out a work-around or something to accomplish the same thing.
(A general-purpose solution would be ideal, but in my current case, I'm trying to efficiently fit a bunch of images of constant width.)
Figure 1: Three rows, each containing some columns:
Figure 2: Three columns, each containing some rows:
You can achieve this layout with the usual markup of course. But I don't think that's what you're after. You want to nest the elements differently.
You can use div to achieve this, and the markup at the end is just as economical as table markup.
<div class="table">
<div class="column">
<div class="row colspan2">
</div>
<div class="row"></div>
</div>
<div class="column">
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
</div>
<div class="column">
<div class="row"></div>
<div class="row colspan2"></div>
</div>
</div>
The CSS classes would need some mucking around to get right though. EDIT: I had a go with jsfiddle, but the sizes are static, so it's not very flexible.
CSS is
.row{
background-color:red;
height: 50px;
border: 1px black solid;
}
.column{
width: 100px;
height: 100px;
text-align:left;
float:left;
}
.table{
height: 200px;
float:left;
}
.colspan2{
height: 100px;
}
I sort of agree with husbas's master table idea except I would do it a tiny bit differently with lists instead of nested tables. I think it depends on what type of data you're trying to represent or if you're simply using the table as a way to lay out the page.
<table>
<tr>
<td>
<ul>
<li>Row 1</li>
<li>Row 2</li>
<li>Row 3</li>
</ul>
</td>
<td>
<ul>
<li>Row 1</li>
<li>Row 2</li>
<li>Row 3</li>
</ul>
</td>
<td>
<ul>
<li>Row 1</li>
<li>Row 2</li>
<li>Row 3</li>
</ul>
</td>
</tr>
</table>
You can define a master table with single row and three columns. And then each column in the master table can contain a table (or any other HTML container) within. Something like this:
<table>
<tr><td>
<table>This is your first columns</table>
</td></tr>
<tr><td>
<table>This is your second columns</table>
</td></tr>
<tr><td>
<table>This is your third columns</table>
</td></tr>
来源:https://stackoverflow.com/questions/9205790/making-a-column-oriented-table-in-html