Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky header & internal links

Tags:

html

css

I have used css to make a "sticky header" that is always visible at the top of the page and the other content placed below it. In the header I have some internal links. The problem is that when a link is clicked then the page is scrolled so that the target is positioned at the top of the page - hidden by my sticky header - instead of just below it.

Any suggestions on how to solve this problem?

css:

#header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3.5em;
    padding:0;
    margin: 0;
}

#container {
    width: 100%;
    margin: 3.5em 0 0 0;
    padding: 0;
    overflow:auto;
}

#content {
    padding: 0 4em;
    margin: 0;
}

html:

<body>

    <div id="header">
        <div id="content">
            <p>
                <a href="#xyz">XYZ</a>
            </p>
        </div> <!--end content-->
    </div> <!--end header-->

    <div id="container">
        <div id="content">
            <p>A lot of text.</p>
            <a name="xyz"></a>
            <p>A lot of text</p>
        </div><!--end content-->
    </div><!--end container-->

</body>
like image 488
user635817 Avatar asked Oct 10 '11 19:10

user635817


People also ask

What is a sticky header?

Sticky headers (or persistent headers) are a common pattern for keeping the header of a website or app in the same place on the screen while the user scrolls down the page. A version of this pattern is the partially sticky header, which (re)appears at the top of the page as soon as the user starts scrolling up.

What is a sticky header in WP?

The WP Sticky Menu (or Sticky Header) On Scroll plugin allows you to make any element on your pages “sticky” as soon as it hits the top of the page when you scroll down. Although this is commonly used to keep menus at the top of your page to create floating menus, the plugin allows you to make any element sticky.


2 Answers

At first, it's better to use blocks with id instead of name — it's more standard way.

Then, add class to an anchor and then make it have absolute position + move it with a negative top margin equal to the header's height.

Look at this fiddle: http://jsfiddle.net/kizu/gfXJJ/

Or, alternatively, for browsers that support pseudo-elements, you can add one with the desired height and compensate it's height by negative top margin, so it would amount as the start of the block to which you'd make a link. Doing so you can add ids to already existent elements rather than creating extra ones.

Here is a version with pseudo-element: http://jsfiddle.net/kizu/gfXJJ/2/

Or you can add top padding and negative margin to an element with id itself: http://jsfiddle.net/kizu/gfXJJ/2/ — but in that case there can be problems with backgrounds on it, 'cause the block is physically extended at the top.

like image 105
kizu Avatar answered Oct 01 '22 18:10

kizu


A little bit of javascript (jQuery used here) can do it:

$('#header a').click(function (e) {
  e.preventDefault();

  var offset = $('a[name=' + $(this).attr('href').substr(1) + ']').offset();

  $('html, body').animate({ scrollTop: offset.top - $('#header').outerHeight() }, 'fast');
});

This finds the element with a name attribute that matches the href attribute of the link clicked, and then animates a scroll to that element's position less the height of the header.

http://jsfiddle.net/blineberry/bTa8b/

like image 37
Brent Avatar answered Oct 01 '22 18:10

Brent