My question is highly related to this previously answered question. In fact, the solution provided by @harpo in the comments of the top-voted answer was a partial solution for me. However, I've found that it actually doesn't work on some of my elements, in particular, elements that have border
s.
Some context: I'm using Bootstrap for my page, and I have a fixed navbar on top, then a page with a load of unstyled divs
and .panel
divs inside those divs. I have a top padding of 60px on my body
which pushes everything down to account for the navbar. However, when I click on a link with an offset (e.g. example.com#div2-1
), the top of the div is still covered by the navbar.
Using the solution provided in the linked answer above, I put 60px of negative padding (pulling the element up by 60px) on the :before
element, then push it down by 60px again by setting the height to 60px. This creates no visual difference (intentional) but the calculated height of my div is now 60px taller, with the div starting 60px earlier than it should, and the real content starting 60px later. This essentially offsets the content for the navbar again.
However, I've found that on elements that have a border, such as my .panel
elements, the border seems to contain everything, and the :before
pseudo-element I have no longer has an effect on the calculated height of my div, which means it gets covered by the navbar again.
Here's my code on JSFiddle: http://jsfiddle.net/rszqtw80/3/
If you take a look at the rendered result in any dev tool, you can clearly see that the :before
element is properly created and has the right height (60px) for both the outer and inner divs
. But for the inner div
s, the overall calculated height of the element ends up not including the :before
element. Disabling the border
with Chrome Dev Tools makes it work for me, so I've deduced that the border is the problem.
So my question: is it possible to get this CSS-based offset to work with my .panel
elements without losing the border
or having to go back to inserting a
tags everywhere? Because quite honestly, without the border
, the .panel
s look quite ugly, and nesting every div
in an a
tag is also quite ugly.
Thanks in advance!
EDIT: Rewrote my question slightly to hopefully clear up any misunderstandings about my intent. Sorry for my confusing explanation :(
FURTHER EDIT: I've added 3 screenshots that hopefully demonstrates this better than any words can ever hope to do. I would've added it here, but I can't add more than two links due to lack of "reputation".