I'm using a static bar at the top of my site, about 20px high. When I click an anchor link(for those who don't know, the navigation on wikipedia works like that. Click a title and the browser goes down to it) part of the text disappears behind that top bar.
Is there any way to stop this from happening? I'm not in a position where I can use an iFrame. Onlything I can think of is make it scroll back a bit each time, but is there another way? Some CSS setting to manipulate the body or something?
Try with
window.scrollBy(xnum,ynum);
xnum: How many pixels to scroll by, along the x-axis (horizontal) ynum: How many pixels to scroll by, along the y-axis (vertical)
For example: http://www.w3schools.com/js/tryit.asp?filename=try_dom_window_scrollby
To fix this with CSS you can add a padding to the Elements you want to jump to:
Example
Alternatively, you could add a border:
However, this is not always applicable.
For a javascript solution you could use a click event attached to the anchor elements that scrolls an adjusted amount of pixels like following:
You could just use CSS without any javascript.
Give your anchor a class:
You can then position the anchor an offset higher or lower than where it actually appears on the page, by making it a block element and relatively positioning it. -250px will position the anchor up 250px
By Jan see offsetting an html anchor to adjust for fixed header
I had the same problem. Here's a jQuery solution
CSS-only: it's a little dirty, but
:target {padding-top: 20px;}
would work if you are linking to a block element (I assumed you do, since your question says div). However, it might not look so good when you scroll manually afterwards. Example http://dabblet.com/gist/3121729Still, I think that using a bit of JavaScript to fix this would be nicer.