Main menu

This is a bit old - you may or may not notice the date on this post but it's over a year old. That doesn't mean it's not useful but we all know how fast things chance on the web so there's a chance that techniques and technologies described here could be a little dated.

A quick CSS based “Back to top” link

You see these in plenty of places, especially with popular single page sites. A page where the menu is at the top in the header and as you scroll down a small “Back to top” link appears that whizzes you back to the top of the page and therefore back to the menu. Here’s a quick way I do it just using a bit of CSS, it might not work in every circumstance but it’s easy to do and can be modified quite easily.

I’ve put together a little demo page for this. (Yes the styles are in the head section but that’s just for easy demo purposes.) Nice and straight forward, grabbed some excerpts from fillerati.com and put together a basic page with some nav jump links using hashtags. Each excerpt is in a separate section with a related h1 tag with an id, it looks basically like this. The actual working of the HTML etc isn’t a big issue it’s just to put together something to demonstrate with.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<header>

<nav>

<ahref="#mobdyDick">Moby Dick</a>

<ahref="#warOfTheWorlds">War of the worlds</a>

</nav>

</header>

<section>

<h1 id="mobyDick">Moby Dick</h1>

</section>

<section>

<h1 id="warOfTheWorlds">War of the worlds</h1>

</section>

We’ll add in a back to top link in the header, simply using the blank # to send us back to the top.

1

2

3

4

5

6

7

<header>

<nav>

<ahref="#mobdyDick">Moby Dick</a>

<ahref="#warOfTheWorlds">War of the worlds</a>

</nav>

<ahref="#">Back totop</a>

</header>

The way this will work is that we use fixed position to have the back to top link anchored to the window, we then use z-index to hide the back to top link behind the nav section but have it sit on top of content. So we have a z-index stacking like this

Nav – z-index 10
Back to top – z-index 5
Content – z-index 0

Now it’ll work fine with z-index 0, 1 and 2 but for some reason out of old habit I move z-indexes in increments of 5. There may well be no reason for this so please feel free not to do that, in fact if someone can think of any reason to do 5 increments please let me know.

So now we get onto the styling, in the demo the nav and the back to top spread across the page, at 100%, this isn’t necessary as long the nav covers the back to top link.

So we style the nav and header like so, just quickly spreading it across the full width of the window.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

header{

width:100%;

margin:0;

text-align:center;

}

nav{

position:relative;//needed to make z-index work

z-index:10;

background:#ddd;

}

nava{

width:15%;

display:inline-block;

padding:0.5em;

}

We then add the back to top styling. Now I will note here that generally it is advised not to style on IDs however I make an exception here because there really should only be one of these links. Again feel free to do it different and style on a class or even an advanced selector.

1

2

3

4

5

6

7

8

9

10

#backToTop{

position:fixed;

top:0;

z-index:5;

background:#999;

display:block;

padding:0.5em;

text-align:center;

width:100%;

}

So as you’ll see on the demo page when you scroll down the page or click on one of the hashtag links and go down the page the back to top link reveals itself from underneath the main nav.

The last thing to do in this example is to take into account the way the hash links work. If you leave the headings to work normally then when you click a link to jump down the page you’ll see that it aligns with the top of the screen and the title is partly covered by the back to top link. So to account for this we add a bit of padding to the top of the target id, nice and straight forward.

1

2

3

h1{

padding-top:2em;

}

As mentioned earlier you can style this up as you wish using the CSS, maybe reduce the back to top link to be a bit more smaller, sit to one side of the page, appear styled as a button, go mad with it. The main thing you’ll have to make sure of are that the main Nav hides the back to top link when the page is at the top.

Recent posts

Popular posts

Warning: The get_mostpopular() template tag has been deprecated since 2.0.3. Please use wpp_get_mostpopular() instead. in /nfs/c06/h02/mnt/92563/domains/kamikazemusic.com/html/wp-content/plugins/wordpress-popular-posts/wordpress-popular-posts.php on line 3403