Stefan Judis Web Development

Stefan Judis Web Development -

CSS only "scroll-to-top" (#snippet)

David Darnes shared a nifty tiny trick to build a "scroll to top" component that automatically shows up after you scroll down a little. And thanks to modern CSS, position: sticky is all you need. 💙 Here's a scroll-top link in action. 👇 [Interactive component: visit the article to see it...] But why use 100vh instead of 100%? Fun fact: percentages in margin-top refer to the logical width of the containing block. margin-top: 50% isn't half the container's height, but width. Either way, this is a nifty little CSS trick. Thanks, David! Reply to Stefan

这篇文章介绍了使用CSS构建“滚动到顶部”组件的小技巧,通过position: sticky属性实现滚动到一定位置后自动显示链接。作者还分享了使用100vh而不是100%的小技巧,因为在margin-top中使用百分比时,它是相对于包含块的逻辑宽度而不是高度。总的来说,这是一个很有用的CSS技巧。

100vh CSS margin-top position: sticky 滚动到顶部

相关推荐 去reddit讨论

热榜 Top10

观测云
观测云
eolink
eolink
Dify.AI
Dify.AI
LigaAI
LigaAI

推荐或自荐