How To Make A Fixed Header Background Not Dissapear
Fixed Table Headers
A few months ago I built an example of stock-still tabular array headers that used CSSposition: sticky
, partly to demonstrate information technology is possible but mostly to attempt to dissuade customer(south) from using a double-<table>
approach or an all-<div>
approach. Then I forgot, then CSS Tricks did a postal service, then I forgot, then a thing happened, and at present here we are. Standard HTML tables are non that circuitous. But when developers are unfamiliar with HTML or allow 3rd-party libraries generate them, they are usually an inaccessible over-engineered mess. One of the more common reasons I hear developers reach for them is because they want fixed headers. For elementary tables, that is mostly unnecessary.
The affair nearly position: viscid is that it merely works in manufactures which explain how to employ
position: viscid
.
Everything in this post assumes a left-to-right, top-to-bottom reading order and language.
The CSS
For stock-still cavalcade headers:
th { position: -webkit-sticky; position: viscous; top: 0; z-index: ii; }
For fixed row headers:
thursday[scope=row] { position: -webkit-viscid; position: sticky; left: 0; z-alphabetize: 1; }
If you plan to have both in one site (page, screen, whatever), so you may desire to avoid conflicts with a more than specific selector. After all, row headers will not work properly without telescopic="row"
, merely column headers do just fine without a scope
(and in my experience are generally absent).
th:non([scope=row]) { position: -webkit-sticky; position: gummy; top: 0; z-index: 2; }
The different z-index
values assistance ensure your column headers sit down in front of your row headers. Otherwise the visible row header will look like it is for the column headers and that is just weird. While you are at information technology, make sure the header cells have a groundwork color or you lot volition go layered text.
For row headers, you may want to add together a border on the right to make the clipping of side by side cells a flake less weird. The problem is that CSS borders don't work here. They only scroll away. A lilliputian trickery with a background gradient can solve that trouble:
thursday[scope=row] { background: linear-gradient(90deg, transparent 0%, transparent calc(100% - .05em), #d6d6d6 calc(100% - .05em), #d6d6d6 100%); }
Examples
View this example at Codepen.
Meet the Pen Fixed Tabular array Header Demo by Adrian Roselli (@aardrian) on CodePen.
Responsive Uses
Manifestly this approach can piece of work well for responsive tables. I take shown how to use a scrolling container. That means we will definitely have a wrapper container and it also means it is likely to scroll independent of the folio.
Note my selector is intentionally convoluted. I want to ensure that whatever overflow
styles won't be applied unless the container has tabindex
(for keyboard users), along with a region role
and attainable name (for screen reader users). Feel free to accommodate for your own uptightness and target browser support.
div[tabindex="0"][aria-labelledby][role="region"] { overflow: car; }
Scrollbars are often hidden by default on mobile devices (and in some desktop browser configurations), so the visual affordance that the user needs to scroll is ofttimes gone. Using Lea Verou's 2012 postal service Pure CSS scrolling shadows with background-attachment: local
will add together a bit of a shadow for the vertically-scrolling content, and Chen Hui Jing adapted it to a horizontal whorl. I tweaked them to apply em
s instead of px
so they will calibration ameliorate.
div[tabindex="0"][aria-labelledby][part="region"].rowheaders { background: linear-gradient(to right, transparent 30%, rgba(255,255,255,0)), linear-gradient(to right, rgba(255,255,255,0), white lxx%) 0 100%, radial-gradient(uttermost-side at 0% 50%, rgba(0,0,0,0.2), rgba(0,0,0,0)), radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.two), rgba(0,0,0,0)) 0 100%; background-repeat: no-repeat; background-colour: #fff; groundwork-size: 4em 100%, 4em 100%, ane.4em 100%, i.4em 100%; background-position: 0 0, 100%, 0 0, 100%; background-attachment: local, local, gyre, scroll; } div[tabindex="0"][aria-labelledby][function="region"].colheaders { background: linear-gradient(white xxx%, rgba(255,255,255,0)), linear-gradient(rgba(255,255,255,0), white lxx%) 0 100%, radial-gradient(farthest-side at fifty% 0, rgba(0,0,0,.two), rgba(0,0,0,0)), radial-gradient(uttermost-side at 50% 100%, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%; background-repeat: no-repeat; background-color: #fff; background-size: 100% 4em, 100% 4em, 100% one.4em, 100% 1.4em; background-attachment: local, local, roll, scroll;
Larger tables on narrow or brusque screens can end up scrolling in 2 directions. A WCAG auditor may argue this violates WCAG two.1 SC 1.4.10: Reflow (Level AA), only data tables have an exception. Regardless, yous should ensure the cavalcade header for the row headers does non disappear when scrolling left-correct.
The easiest way to do that is grab the showtime header cell that is not also a row header and increase its z-index
, making sure to also give it a border effect every bit I covered above:
th:non([scope=row]):offset-child { left: 0; z-index: 3; background: linear-gradient(90deg, #666 0%, #666 calc(100% - .05em), #ccc calc(100% - .05em), #ccc 100%); }
Example
View this responsive example on Codepen.
Run across the Pen Stock-still Tabular array Header Demo: Responsive past Adrian Roselli (@aardrian) on CodePen.
Compatibility Notes
- Only Firefox supports
position: sticky
on<thead>
, so for maximum compatibility nosotros have to lean on<th>
(and few transmission coders utilize<thead>
anyway). There is a Chromium bug acknowledging this with some detail; there was a legacy-Edge problems that provided a lot more context just that never made information technology into the Wayback, so, yeah.- 3 June 2021: Some confirmation on the Twitters that Chrome 91 got the prepare from the problems I cited, which y'all tin confirm in this demo. It seems tables got a full re-write in Chrome.
- 16 June 2021: In that location is finally a proper confirmation from the Chrome team in the post TablesNG Resolves 72 Chromium Bugs for Better Interoperability.
- xiii June 2021: Safari 14 on macOS and iOS supports
position: sticky
on<thead>
. - thirty September 2021: Safari Technology Preview 133 supposedly fixes spacing in tabular array cells with
position: viscid
.
- 3 June 2021: Some confirmation on the Twitters that Chrome 91 got the prepare from the problems I cited, which y'all tin confirm in this demo. It seems tables got a full re-write in Chrome.
- The
<caption>
chemical element staunchly refuses to honor existence mucilaginous. This means in a left-right scroll it disappears when it would be best retained. - Safari on macOS and iOS requires
-webkit-sticky
.- xiii June 2021: Safari 14 on macOS and iOS no longer requires
-webkit-sticky
.
- xiii June 2021: Safari 14 on macOS and iOS no longer requires
- Safari on macOS and iOS does not reclaim the space from
<explanation>
. This creates a gap with data cells appearing above the row of column headers when scrolling vertically. Because<explanation>
is part of an attainable table and also feeds the accessible name of the wrapping<div>
, you cannot just remove it. Instead, consider using thevisually-hidden
form on the<caption>
(available beneath).- xiii June 2021: This is still truthful in Safari 14 on macOS and iOS. However, since that version supports
position: sticky
on<thead>
, you can switch to using<thead>
.
- xiii June 2021: This is still truthful in Safari 14 on macOS and iOS. However, since that version supports
- This will not work in Net Explorer xi (or older). I experimented with the Stickybits polyfill and Stickyfill polyfill, but they did not work well with tables for a variety of reasons that are lost to the winds.
- If the fixed row is not sufficiently shorter than the container/viewport height, or the fixed column is non sufficiently narrower than the container/viewport width, then this will be terrible. So be careful and test across devices, zoom sizes, and zoom sizes on smaller devices.
- Read more about
viscous
support at Tin can I Use, and the linguistic communication in the latest CSS position draft.
visually-hidden
Course
/* Proven method to visually hibernate something but */ /* withal make it available to assistive technology */ .visually-subconscious { position: absolute; tiptop: automobile; overflow: hidden; prune: rect(1px 1px 1px 1px); /* IE 6/7 */ clip: rect(1px, 1px, 1px, 1px); width: 1px; height: 1px; white-space: nowrap; }
Update: 29 September 2020
LĂ©onie Watson has just posted How screen readers navigate information tables where she walks through a sample table to become some data, explaining each step, the keyboard commands, and the output. She also links to a video demonstration, which I have embedded beneath.
Update: 13 June 2021
Chrome 91 at present supports position: mucilaginous
on <thead>
.
Safari 14 on macOS and iOS now supports supports position: sticky
, so you tin can dump -webkit-sticky
if your Mac audience is guaranteed to be on the latest release. It also supports a viscid <thead>
.
The bug where Safari does not reclaim space from the <caption>
is still there, but if you make the <thead>
sticky the issues does not appear.
A codepen demo of each for your own testing:
Run into the Pen Testing position: sticky on <thead> past Adrian Roselli (@aardrian) on CodePen.
Update: 23 June 2021
This postal service does not utilise CSS scroll snap. That is intentional. Two reasons why I did not implement it:
- A Chrome problems from 2018, Effect 835301: [css-scroll-snap][position-gummy] scroll-snapping "stickily" positioned elements tin crusade inaccurate snap positions, which was non fixed with the Chrome 91 table fixes;
- In testing, users often wanted to straddle a prison cell to compare with others, and on smaller screens, higher zooms, or fuller cells, this became specially difficult.
I forked the pen, added curlicue-snap-marshal: commencement
to the th
s and td
s, and added whorl-snap-type: both mandatory
to the wrapper. You lot volition come across in Chrome the caption gets clipped on vertical scroll and the 2nd cell tin be clipped on horizontal roll.
Once you factor in users who resize text or zoom, things can go problematic. Test your scroll snap solution against WCAG SCs ane.iv.4 Resize Text, 1.4.10 Reflow, and 1.4.12 Text Spacing. Then try it with just the keyboard.
Come across the Pen Fixed Table Header Demo: Responsive with Curl Snap past Adrian Roselli (@aardrian) on CodePen.
If yous want to use roll snap, test the heck out of it both with users and in browsers.
How To Make A Fixed Header Background Not Dissapear,
Source: https://adrianroselli.com/2020/01/fixed-table-headers.html
Posted by: graingermisiongs.blogspot.com
0 Response to "How To Make A Fixed Header Background Not Dissapear"
Post a Comment