I'm trying to implement a pure CSS scrolling UL-LI table with fixed header.
My requirements:
- using table CSS (table, table-row, table-cell, table-header-group...)
- all cells have to be list items (LI)
- header has to be fixed when table content is scrolling
- when table column changes width, appropriate header width should be changed
Currently I have HTML:
<ul class="testTable">
<div class="testHeader">
<li class="testRow">
<span>ID</span>
<span>Name</span>
<span>Description</span>
<span>Other details 1</span>
<span>Other details 2</span>
</li>
</div>
<div class="testBody">
<li class="testRow">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</li>
<li class="testRow">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</li>
</div>
</ul>
...and CSS...
.testTable {
display: table;
margin: 0px;
padding: 0px;
}
.testRow {
display: table-row;
}
.testRow > span {
list-style:none;
display: table-cell;
border: 1px solid #000;
padding: 2px 6px;
}
.testHeader {
display: table-header-group;
/*position: absolute;*/
}
.testHeader span {
background-color: #ccc;
}
.testBody {
display: table-row-group;
}
Fiddle is here: http://jsfiddle.net/ozrentk/QUqyu/1/
BUT! The moment I try to fix position of the header using position: absolute
or fixed
, the table falls apart. I tried several techniques, but to no avail. Also, there is zero to none examples how to do this using pure table CSS.
This was close, but not exactly what I require.
Is there a CSS guru that can help me?
EDIT
Now, why the hell did I want to display this list as a table?
In my dynamic ASP.NET MVC driven site I have a number of places where I return unordered lists to the browser. Browser will then take this markup and display it to the reader. But the display format itself can actually depend on context, like users display-related preferences or the device format itself. CSS is used for the display formatting, as it should be. At last, if there is some display-light-n-magic-effect to be used, jQuery and/or a plugin should be used for that, and hopefully only for that.
You see, I want my server to remain display-format agnostic. That is, I don't want my server to care about how the particular client want his display to look like. I don't want if-blocks that return unordered-list-items in one case and table-cells in the other. Of course I could have two return points, one which returns ul/li/span format and the other which returns table/tr/td, but that would be violating DRY principle.
Another thing is that I'm using a really nice jQuery plugin that displays tabular data and can be fed with list-items, but not table markup. And I decided to stick with the plugin. because I like it, it's great and supports the way my site should work.
I hope this sorts things out. You see, using one paradigm can be in contrast with the other. It turns out I have to give away general-tabular-data-semantics to have a DRY code.
P.S. The more I think about this situation, the more it looks like a pragmatic, not a semantic problem.
` or `
`) is a list item (`- `).
– André Dion Jul 31 '13 at 14:55` this *will* cause unexpected results. Beware.