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

后端 未结 21 1708
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:56

    As Josh Lee and Colin 't Hart have said, you could optionally just use position: sticky; top: 0; applying to the div that you want the scrolling at...

    Plus, the only thing you will have to do is copy this into the top of your page or format it to fit into an external CSS sheet:

    
    

    Just replace #sticky_div's_name_here with the name of your div, i.e. if your div was

    you would put #example { position: sticky; top: 0; }.

提交回复
热议问题