anchor linking to a certain position of the page

前端 未结 7 2107
渐次进展
渐次进展 2021-01-06 18:06

I have an anchor link which links to another page. When it is clicked, by default it goes to the top of the next page. I want it brought to a certain position of the page. H

7条回答
  •  走了就别回头了
    2021-01-06 18:33

    I was looking for a better way to do this, but I cannot find one. My solution was to create a zero-width anchor at the target point, pull it out of normal flow via "position: relative" and make sure that the content box was as high as it needed to be to force the following element farther down on the page:

    
    

    Pulling it out of normal flow keeps the padding from becoming part of the page, but when calculating the offset all the browsers I tested respect it when positioning. Of course, this only works for fixed offsets from the top of the viewport. It works, but it feels kludgy.

提交回复
热议问题