Email or username:

Password:

Forgot your password?
Bramus

Today I saw this React hook (see photo) get shared on birdsite. It enables one to “automatically scroll down when new messages arrive in a chat”.

While it might work, you could — and should — use this little bit of CSS instead:

```
.log {
scroll-snap-type: y proximity;
align-content: end;
}
.log::after {
display: block;
content: "";
scroll-snap-align: end;
}
```

“… remains snapped to the bottom …unless the user has scrolled away from that edge” — drafts.csswg.org/css-scroll-sn

The React Hook that was shared.
28 comments
Stuart Langridge

@bramus whoa. I had no idea this was possible! nice tip!

Ben

@bramus I wrote a hook for exactly this purpose and looking decently similar back in 2018 which is still in service. Pretty amazing to see it completely unnecessary now.

Bramus

@sangster The web always catches up (eventually) 😊

Ben

@bramus well technically mine also does data fetching for infinite scroll but it would be significantly simpler without the scroll behavior

Casey

@bramus Never do with 4 lines of CSS what you could do with 150 lines of React

Bramus

@casey I should print and frame that 😅😂

Martin Grubinger

@bramus I had no idea this was possible but was curious how it works, so I made a quick #codepen of this idea: codepen.io/mgrubinger/pen/LYKQ

I love how simple (and reliable) this #css solution is!

hazel

@grooovinger @bramus </3 this makes swipe scrolling try to end up lining up with every message on iOS safari, it feels really weird

hazel

@grooovinger @bramus with and without this scroll snapping. Hoping this is a problem that could be fixed with more CSS?

Martin Grubinger

@h I fixed the width, but I can't reproduce the strange behaviour (no iPhone around at the moment, simulator looks fine). Which iOS/Safari version are you on?

Nicolas Hoizey

@grooovinger @bramus it doesn't seem to work in Firefox unfortunately.

Based on Can I Use, `scroll-snap-type` and `scroll-snap-align` are supported, but maybe not these values?

Martin Grubinger

@chfkch @bramus apparently it's not supported by firefox yet.

Bramus

@grooovinger @chfkch Yeah, noticed that too … filing a bug as that should actually work.

Bramus

@grooovinger @chfkch Bug Report: bugzilla.mozilla.org/show_bug.

Workaround: instead of snapping generated content, you can snap the `:last-child`.

Amadeus Maximilian

@grooovinger @bramus as cool as this is, I think this also breaks inertia scroll on iOS. 🤔 Thanks for building it though, I was curious to implement it myself when I tread the original Toot. 😊

I feel like I’ve read about a similar solution using overflow-anchor somewhere, but I might be mistaken, since that property doesn’t do what I thought it did. 😅

Martin Grubinger

@amxmln I tested it today on a real iOS device. Can confirm that inertia is not the same as without snapping, which I guess makes sense but is still a bit disappointing. Hopefully I'll get around to play with the pen a bit more, maybe this can be fixed.

yshtolafucker926
@bramus this is why I don't like react. they don't know about REAL web design (static html files with crazy css and MAYBE some js)
meduz'

@bramus Ha, I discovered this trick this week-end while attempting to have a scroll container initialised with scroll end without JS, but then I haven’t found a way to “unlock” the scroll without JS.

Vesa Piittinen

@bramus There is also `overflow-anchor` as covered at CSS Tricks: css-tricks.com/books/greatest-

Here is a pen I made combining with another #CSS only trick to start the scroll from bottom with zero JavaScript:

codepen.io/Merri/pen/BagrowG

Bramus

@MerriNet Checked this in detail and while it does work, it doesn’t seem to play nice in situations where the scroller goes from non-overflowing to an overflowing state (which could be considered a bug).

Also a pity that it requires an element (or generated content) that actually takes up space (i.e. `min-height: 1px`).

And finally no Safari support for this, unfortunately :-(

codepen.io/bramus/pen/GRbxOPX/

Vesa Piittinen

@bramus Interesting, my pen works in Safari. So I tested a bit and `display: flex;` is what breaks it in Safari.

I don't have time to check if there is a bug report for Safari on this.

Bramus

@MerriNet Oh, I think we may be talking about two things here. Was talking about `overflow-anchor` which I used in that last demo I linked to.

Vesa Piittinen

@bramus I'm talking about that. It works (stays in bottom also in Safari) once you remove `display: flex;` on the `#list` element.

Kit Henry

@bramus how does this compare to using `flex-direction: column-reverse;`?

andrei!

@bramus Thank you this is amazingly simple.

Juan Villela

@bramus My favorite part of the hook is the lack of comments 🙃

Go Up