惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
博客园 - 叶小钗
I
InfoQ
MyScale Blog
MyScale Blog
H
Help Net Security
月光博客
月光博客
Vercel News
Vercel News
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky

Ben Frain

Well done AI, that’s the end of the honest boxes The Beginnings, by Rudyard Kipling So, you want a React modal that uses the <dialog> element and transitions in AND out? Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! Center items in a container, and make then left aligned when they overflow A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Managing multiple versions of node, without NVM or additional tools Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Yes! You can use position: sticky and overflow together Neovim – how to do project-wide find and replace? Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations?
Scroll indicators on tables with background colours using...
Ben Frain · 2026-02-13 · via Ben Frain

Imagine a HTML table, with alternating rows with different background colours. The table content overflows the container on the right but on a mobile browser, with no obvious scrollbar, there is no affordance to know the table is overflowing.

I want to have a shadow/indicator on the side of the table that is overflowing the container.

Go to solutions that won't work

Many moons ago, Lea Verou popularised this approach: http://lea.verou.me/2012/04/background-attachment-local/

That general approach works great when your table does not have a background color. But mine does, so it doesn't work [insert Raspberry noise].

Then I remembered there was some way to do this with scroll() timelines. A web search or two past various web Illuminati (hi Adam!, hi Bramus) landed me at Dave's site.

Thank goodness Dave had written this as it got me a decent distance to the solution I ended up with.

However, the trouble I had with Dave's example as it was, was the same as the old skool one from years hence – it only worked if you have a table with no background colours in it. With background colors, it covered the inset shadows of the container.

"He conquers who endures." — Persius

So, having thought about it a little more, here is what I came up with…

The solution

I've got a table with a few columns, you can use whatever HTML table you like – mine shows the top 6 teams and their stats in the England Premier League.

Here is the result with the shadow technique added:

See the Pen
Untitled
by Ben Frain (@benfrain)
on CodePen.

Let's look at how this works. The table in a standard HTML table, nothing unusual there. It gets a wrapping element, such as a div, let's call the wrapping element 'TableWrapper'.

CSS Grid for layout

We set the TableWrapper layout with CSS Grid with a single row, and columns at either end for where we want our scroll indicators. For example:

.TableWrapper {
    display: grid;
    grid-auto-flow: column;
    grid-template: "leftindicator content rightindicator" auto / 30px max-content 30px;
    /* More styles */
}

Remember I said the table had background color? Let's add that to our table now.

table {
    tr:nth-child(even) {
        background-color: #444;
    }
}

We need our indicators to sit in the columns at either side of our grid. You could add extra elements inside the wrapper but I used ::before and ::after pseudo elements. I made them about 30px wide and gave them a linear gradient to make the shadow effect – one fades from dark to the transparent in one direction, and the other fades in another direction.

I ended up using grid to place the indicators, rather than something more conventional like absolute positioning, because this way, they will sit at either end of the table, but crucially, we can make them appear on top of the table and stuck to the edge of the scroll container with position: sticky. The higher z-index on the before/after ensures they appear above the table background.

Let's add the before/after elements into our TableWrapper. Here is the complete rule – note the grid-template to layout everything in the container. I removed the scroll-bar just to make the scroll indicators more needed (because a scrollbar is a pretty good scroll affordance already ;)).

.TableWrapper {
    display: grid;
    grid-auto-flow: column;
    grid-template: "leftindicator content rightindicator" auto / 30px max-content 30px;
    overflow-x: auto;
    scrollbar-width: none;

    &::before,
    &::after {
        content: "";
        height: 100%;
        width: 30px;
        display: block;
        z-index: 10;
        position: sticky;
        opacity: 0;
    }

    &::before {
        grid-area: leftindicator;
        left: 0;
        animation: show-indicator linear reverse;
        animation-timeline: scroll(nearest inline);
        background-image: linear-gradient(to right, #000, transparent);
    }

    &::after {
        grid-area: rightindicator;
        right: 0;
        animation: show-indicator linear;
        animation-timeline: scroll(nearest inline);
        background-image: linear-gradient(to left, #000, transparent);
    }
}

Modern CSS magic

There are a few things that only work due to modern CSS. If you want to know more about modern CSS, here is where I pimp my book, Responsive Web Design with HTML5 and CSS which covers all this stuff – find out more at https://rwd.education

First, I'm making both pseudo-elements position: sticky. The left/::before stuck to the left, the right/::after to the right.

Secondly I'm using animation-timeline, which is being set to scroll(nearest inline). So, if your brower doesn't support that, you won't see it.

In plain english, we have an animation, defined like any other with @keyframes:

@keyframes show-indicator {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

The animation is used on both before/after element and 'driven' by the scroll, with the ::before running the animation in reverse. We set the scroll() to be the nearest scrollable ancestor of the element we have set it on (because the before/after are effectively children of the TableWrapper). Super important — always put your animation-timeline after the animation shorthand otherwise the shorthand will wipe it out!

And that is it. Scroll indicators that show when either end of the scrollable content can be scrolled — even if that content has a background.

Postscript: I found this after the fact as well! Bunch more approaches too https://css-tricks.com/unleash-the-power-of-scroll-driven-animations/#aa-add-scroll-shadows-to-a-scroll-container