This question already has an answer here:
- Fixed page header overlaps in-page anchors 28 answers
I am trying to clean up the way my anchors work. I have a header that is fixed to the top of the page, so when you link to an anchor elsewhere in the page, the page jumps so the anchor is at the top of the page, leaving the content behind the fixed header (I hope that makes sense). I need a way to offset the anchor by the 25px from the height of the header. I would prefer HTML or CSS, but Javascript would be acceptable as well.
I'm facing this problem in a TYPO3 website, where all "Content Elements" are wrapped with something like:
and i changed the rendering so it renders like this:
And this CSS:
The fixed topbar being 40px high, now the anchors work again and start 10px under the topbar.
Only drawback of this technique is you can no longer use
:target
.As this is a concern of presentation, a pure CSS solution would be ideal. However, this question was posed in 2012, and although relative positioning / negative margin solutions have been suggested, these approaches seem rather hacky, create potential flow issues, and cannot respond dynamically to changes in the DOM / viewport.
With that in mind I believe that using JavaScript is still (February 2017) the best approach. Below is a vanilla-JS solution which will respond both to anchor clicks and resolve the page hash on load (See JSFiddle). Modify the
.getFixedOffset()
method if dynamic calculations are required. If you're using jQuery, here's a modified solution with better event delegation and smooth scrolling.@AlexanderSavin's solution works great in
WebKit
browsers for me.I additionally had to use :target pseudo-class which applies style to the selected anchor to adjust padding in
FF
,Opera
&IE9
:Note that this style is not for
Chrome
/Safari
so you'll probably have to use css-hacks, conditional comments etc.Also I'd like to notice that Alexander's solution works due to the fact that targeted element is
inline
. If you don't want link you could simply changedisplay
property:This takes many elements from previous answers and combines into a tiny (194 bytes minified) anonymous jQuery function. Adjust fixedElementHeight for the height of your menu or blocking element.
If you don't like the animation, replace
with:
Uglified version:
I ran into this same issue and ended up handling the click events manually, like:
Scroll animation optional, of course.
A further twist to the excellent answer from @Jan is to incorporate this into the #uberbar fixed header, which uses jQuery (or MooTools). (http://davidwalsh.name/persistent-header-opacity)
I've tweaked the code so the the top of the content is always below not under the fixed header and also added the anchors from @Jan again making sure that the anchors are always positioned below the fixed header.
The CSS:
The jQuery (including tweaks to both the #uberbar and the anchor approaches:
And finally the HTML:
Maybe this is useful to somebody who likes the #uberbar fading dixed header!