Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can fix the navbar in place with <code>position:fixed</code> and <code>top:0</code> (as you're already doing), and set its <code>height</code> to a given value, and then apply the same value to the <code>padding-top</code> property of each of the target anchors.</p> <p>This way, when the navigation links are clicked on, the top of the anchor elements will move to the top of the browser's viewport, and while they will be partially covered by the navigation bar (due to <code>z-index:1</code> set in the navbar), this covered part will be the padding area which won't have any content, and will typically be invisible (unless borders or background colors are added). The content will thus correctly start right after the navbar.</p> <p>Here's example code:</p> <ul> <li><p>css:</p> <pre><code>#navbar { position: fixed; top: 0; z-index: 1; height: 1em; margin: 0; } .heading { padding-top: 1em; } </code></pre></li> <li>html: <pre><code>&lt;div id="content"> &lt;h2 id="one" class="heading">Section 1&lt;/h2> ... &lt;h2 id="two" class="heading">Section 2&lt;/h2> ... &lt;h2 id="three" class="heading">Section 3&lt;/h2> ... &lt;/div> &lt;div id="navbar"> &lt;a href="#one">Section 1&lt;/a> &lt;a href="#two">Section 2&lt;/a> &lt;a href="#three">Section 3&lt;/a> &lt;/div> </code></pre></li> </ul> <p>Of course, this doesn't solve the problem of a dynamic navbar height, but Bryan's suggestion to use JavaScript for that is probably the easiest (only?) way to implement this.</p> <p>ps - Keep in mind that the if the id'd element has <code>display:block</code> or <code>display:inline-block</code>, the padding will have an effect on the page layout, which might not be desirable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload