Solution to the problem of page title and page anchor overlapping
P粉521748211
2023-08-21 19:18:36
<p>If I have a non-scrolling header in an HTML page that is fixed at the top, with a defined height: </p>
<p>Is there a way to use URL anchors (<code>#fragment</code> part) to make the browser scroll to a specific position in the page but still respect the height of the fixed element<strong> Help without using JavaScript</strong>? </p>
<pre class="brush:none;toolbar:false;">http://example.com/#bar
</pre>
<pre>Error (but common behavior): Correct:
---------------------------------- ------------------ ----------------
| BAR/////////////////////// header | ////////////////////////// header |
---------------------------------- ------------------ ----------------
| rest of text here | | BAR |
| ... | | |
| ... | | rest of text here |
| ... | | ... |
---------------------------------- ------------------ ----------------
</pre>
<p><br /></p>
If you can't or don't want to set a new class, you can add a fixed-height
Or use jQuery to scroll the page relative to:target# to the
::beforepseudo-element in CSS.
##Pseudo class::target
:
I encountered the same problem. I solved this problem by adding a class to the anchor element and using the topbar's height as the value of padding-top.
I used the following CSS: