kalpetros
kalpetros

Reputation: 983

Some of the content goes under the fixed header when user clicks an anchor link

I'm trying to create a anchor link so when the user clicks an item on the menu it goes to the specified destination. My menu consists of three items (one, two, three). When I click for example Three it jumps to Three but its heading goes under the header. How can I prevent that? I want the user to be able to see the heading. Note that I want my header to be fixed and I want the contents to scroll behind the header.

HTML:

<body>
<header>
    <nav>
        <ul>
            <li><a href="#one">One</a></li>
            <li><a href="#two">Two</a></li>
            <li><a href="#three">Three</a></li>
        </ul>
    </nav>
</header>

<section>
    <div id="one">
        <h1>One</h1>
        <p>Text...</p>
    </div>
    <div id="two">
        <h1>Two</h1>
        <p>Text...</p>
    </div>
    <div id="three">
        <h1>Three</h1>
        <p>Text...</p>
    </div>
</section>

<footer>
</footer>
</body>

CSS:

body {
    background-color: #cf8;
}

header {
    background-color: #000;
    height: 4em;
    width: 100%;
    top: 0;
    left: 0;
    position: fixed;
}

nav ul {
    list-style: none;
}

nav ul li {
    margin-top: 0em;
    padding: 5px;    
    float: left;
}

nav ul li a {
    color: white;
    text-decoration: none;
}

section {
    height: auto;
    width: 50%;
    margin-top: 4em;
    margin-left: 25%;
}

#one,#two,#three {
    margin-top: 1em;
}

div {
    background-color: #c00;
    height: auto;
    width: 100%;
}

footer {
    background-color: #000;
    height: 2em;
    width: 100%;
    clear: both;
}

JSFIDDLE, JSFIDDLE (Version 2)

JSFIDDLE (FULLSCREEN), JSFIDDLE (FULLSCREEN (VERSION 2))

Upvotes: 1

Views: 2528

Answers (3)

Rajesh Paul
Rajesh Paul

Reputation: 7009

for the <nav>-tag define the inline style as style="z-index:1; position:absolute;"

and

for the <section>-tag define the inline style as style="z-index:2; position:absolute;"

In this case the contents in the section-tag will be visible above the nav menu.

Upvotes: 0

Terry
Terry

Reputation: 66103

I would recommend using a jQuery-based solution instead (p/s: see [Edit #2] for the final code, where I also detect the window.location.hash property):

$(function() {
    // Only trigger .click() event when the link points to an internal anchor
    $("header a[href^='#']").click(function(e) {

        // Get the ID of the target
        var target = $(this).attr("href");

        // Animated scrolling to the vertical offset of the target element
        // PLUS the outer height of the <header> element
        $("html, body").animate({
            scrollTop: $(target).offset().top - $("header").outerHeight()
        });

        // Prevent default scrolling action
        // (I didn't use return false, because it stops event bubbling, too)
        e.preventDefault();
    });
});

http://jsfiddle.net/teddyrised/NHtvM/13/


[Edit]: However, you should note that this method does not work when the visitor is navigating to the specific div by entering the location hash in the url (e.g. /page.html#one).


[Edit #2]: Okay, I have revised my script so that it can detect the hashed URL if present, and perform the same thing as above (updated Fiddle here). An example would be: http://jsfiddle.net/teddyrised/NHtvM/15/show/light/#three, where you want the browser to navigate directly to the <div> with the ID of "three".

$(function () {
    // Scroll to function
    function scrollTo(ele) {
        $("html, body").animate({
            scrollTop: $(ele).offset().top - $("header").outerHeight()
        });
    }

    // Detect location hash
    if (window.location.hash) {
        scrollTo(window.location.hash);
    }

    // Detect click event
    $("header a[href^='#']").click(function (e) {
        var target = $(this).attr("href");
        scrollTo(target);
        e.preventDefault();
    });
});

Upvotes: 1

Marko Vranjkovic
Marko Vranjkovic

Reputation: 6869

You can accomplish this even without using JavaScript, just add empty divs with the same height and negative top-margin as menu before every part. Like this:

<div id="one"></div>
<div>
   <h1>One</h1>
...

with CSS

h1{ margin-top:0em; }
#one,#two,#three { margin-top:-4em; height:4em; }

See: http://jsfiddle.net/NHtvM/7/ (or in full screen http://jsfiddle.net/NHtvM/7/embedded/result/)

Upvotes: 1

Related Questions