The new value of position is very confusing to me.. a lot of search result give javascript/jQuery (JavaScript-framework) solutions.
In the example in bottom i have a table
with a thead
and tbody
.
No matter what i cannot achieve the desire result.
Desire result is thead to be sticky to the table. sticky means when not in view the element is some kind of position:fixed
fixed means it sticks to your screen. What i did try:
display: inline/block/inline-block;
- th element
position:sticky
(th element is an element inside a tr witch is inside a thead element) - mix of
display:inline/block
etc; (values of property display)
I just cannot find how to achieve this sticky in firefox (supported)
Any solutions ??
(as position:sticky
still an experimental API and should not be used in production site http://developer.mozilla.org/en/docs/Web/CSS/position )
table {
background-color: rgba(241, 31, 0, 0.3);
width: 100%;
}
thead {
background-color: rgba(241, 0, 241, 0.3);
position: -webkit-sticky;
position: sticky;
}
th {} tbody td:nth-child(2) {
height: 200px;
}
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>
Firefox seems not to allow yet 'sticky' on table childs elements.
a workaround would be to set
table
asblock
, thenthead
,tbody
,tfoot
todisplay:table;
so one of them can be sticked.unfortunately this breaks the table-layout and split table into few tables .. :(
you also need to set coordonates where sticky comes in action http://codepen.io/gc-nomade/pen/reoExq . not the best :(
CSS base would be like:
Another method is to use css3 to translate the header cells. This method does require javascript and will work in all modern browsers, but because it translates the table cell, the border does not get included for some reason (demo)
Also, this css is necessary to include a background color on the translated cells
jQuery
Update Q1 2018
position: sticky
has now landed in stable Firefox 59. The fiddle linked below now works unchanged in Firefox as well.I know you specifically asked for Firefox compatibility of
position: sticky
, but unfortunately its styling effect in Firefox is not defined on inner table elements:That being said,
position: sticky
is about to land in Chrome Stable channel (it is in Canary at the time of writing and there's a developer.google.com blog post about it). Their implementation does work fine onthead
, solving my own long-standing problem of sticky theads as all other solutions fail when you need to resize the table/cell widths.I've created a fiddle to test the sticky positioning. On all channels of Firefox, this has no effect.
My hope is that
position: sticky
now gains tractions due to the more complete implementation in Chrome, it will stir discussions again on the lacking inner table support ofsticky
.There's also a bug report on Firefox' Bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=975644