How can I make a div stick to the top of the screen once it's been scrolled to?

后端 未结 21 1691
Happy的楠姐
Happy的楠姐 2020-11-22 07:12

I would like to create a div, that is situated beneath a block of content but that once the page has been scrolled enough to contact its top boundary, becomes fixed in place

21条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 07:57

    As of January 2017 and the release of Chrome 56, most browsers in common use support the position: sticky property in CSS.

    #thing_to_stick {
      position: sticky;
      top: 0px;
    }
    

    does the trick for me in Firefox and Chrome.

    In Safari you still need to use position: -webkit-sticky.

    Polyfills are available for Internet Explorer and Edge; https://github.com/wilddeer/stickyfill seems to be a good one.

提交回复
热议问题