Creating your own website top bar


- Post was last modified on June 3rd, 2009.
post-thumb

Top bar's seem to appearing on more and more websites each passing day. I've always quite liked the idea and with some XHTML and CSS you can also create your own top bar. Using it for whatever purpose you want it for.

I call them top bar's but other names like menu bar's, top navigation bars also mean the same thing. If you want some visual reference of what I mean look below:

top-bar

(Screenshot is of the top bar featured on this website)

Okay so what I call top bar's are something relatively new on websites. You tend to see these appearing on websites that suit them, depending on what type of website it is and the overall design of them. You will notice that they are popular on certain blog layouts (Even though the one you see above is my own addition) but there popular as they can be used for different purposes that suit the webmaster. Anyway lets forget my rambling on and dive right in into coding one ourselfs.

Step 1: Creating the divs in XHTML

First off all were going to create two divs called top-bar and bar-content and apply them into a XHTML document like so:

<div id="top-bar">
<div class="bar-content"></div>
</div>

Embedding our bar-content div within the top-bar div is important, otherwise it's all going to fall to pieces. Thats all the XHTML we need to apply in right now, now let's move onto our CSS.

Step 2: Creating our divs CSS

The CSS will control most of how the top bar is displayed.

First div (Top-Bar):

#top-bar{
position:absolute;
top:0px;
width:100%;
height:35px;
margin-left:-9px;
background-color:#000000;
}

This is the main div that controls and tells the browser what position the div will be displayed in and what size etc.

Second div (bar-content):

.bar-content{
margin-left:8px;
color:#666666;
margin-top:9px;
font: bold 11px Arial, Helvetica, sans-serif;
}

This is the div that controls what's within the main div, so attributes like colour of the text and margins and controlled here.

Now that we have our CSS laid out, let's just add in some text between the bar-content div to test out what our result is so far:

<div id="top-bar">
<div class="bar-content">Test</div>
</div>

After our first test we should get something like this:

Test

(Image Scaled down, so the layout of this blog doesn't break it's div)

Step 3: Making it look good!

Okay so we have our basic top-bar code in place. We can now start to make it more appealing, first off we can get rid of that boring on colour background colour and add in a gradient. You can create this yourself or download my example image below:

example graident

(Make sure you right click save as and apply where it is saved on your computer.

So now we have the gradient we can simply replace the background-color attribute on the top-bar's CSS div to this:

background-image:url(images/topbar-bg.png);
background-repeat:repeat;

Replace the background-color code with this code above so our top-bar CSS div will look like this:

#top-bar{
position:absolute;
top:0px;
width:100%;
height:35px;
margin-left:-9px;
background-image: url(images/topbar-bg.png);
background-repeat:repeat;
}

A few notes on the graident image:

If you create this graident image yourself, you will need to make sure the height of the image is the same as the height specified on the div. In this case it's 35px. I'd recommend setting the width of the imago to about 10px and then create a gradient with that image size.

Also you must have a repeat attribute in the div's CSS otherwise the background will not strech across the top-bar.

Apply our new changes and our top-bar should now look like this:

gradient

Notice how this small gradient makes the bar more appealing?

Step 4: Choosing what to put in it

Now this part is entirely up to you. You could use it as a new's bar where it displays the current updates on your website, or use it to contain links. Here's some screenshots of what I've done as examples.

As a news bar:

News Bar

As a sub link navigation bar:

sublinks

Remember when adding in your content you place it within the bar-content div:

<div id="top-bar">
<div class="bar-content">Your top bar content will go here</div>
</div>

Theres two examples of what you can do with top-bar's

Note: Remember my forcing Div's on one link hack?

You going to be needing this if your going to be using more div's within the bar content div in the XHTML, link is below:

http://www.james-blogs.com/2008/08/27/css-hack-making-divs-appear-on-the-same-line/

Additional code:

If you want your top-bar to be in a fixed position so it moves down the page when you scroll down on the page then you simply need to replace the position on the top-bar CSS div from this:

position:absolute;

To this:

position:fixed;

You may need to adjusted the margin's set in top-bar div in the CSS if you change the positioning

So there you go. I've gave you the XHTML and CSS for coding a top-bar, see what you make out of it!

34 Reader Comments, Why not join the party?

  •  
  • Bryan - After5PC
    January 27th

    Thank you for this tutorial! I've been searching on Google for some time now... trying to figure out how to add a top bar area for my network of sites. Your explanations are clearer than most I've seen.

  • gjperera
    February 2nd

    Awesome tutorial...hopefully I'll be adding this to my site soon, just need to customize it a bit. I had some trouble trying to find a tutorial on this because like you I really didn't know what to call it. Again great tutorial and thanks for making our lives (non-coders) simpler.

  • Bob
    April 29th

    Did you know your blog name is grammatically incorrect? Plurals are made possessive by the addition of a single apostrophe. Singular nounds that end in "s" are made possessive with the addition of "'s" (apostrophe s).

    Strunk & White is widely regraded as authoritative on usage - check this link:

    http://www.bartleby.com/141/strunk.html

    Not trying to be pedantic, just thought you might want to know, since it's the title of your blog!

  • Bob
    April 29th

    Great tip! I tried to build this myself from scratch, but had all sorts of problems and questions. Your tutorial broke it down beautifully. Thanks for your work!

  • James
    May 3rd

    While what you've said is correct. I could argue that this blog i.e. website belongs to me, therefore it is James' Blog :) . Though I was taught that both can be used, I believe there is mixed opinions about the use of 's and s' that one being wrong, conflicts with different views of another depending on how you were taught.

  • tommy
    May 5th

    Wow I cant look at who is creating all these spam posts. Your content is excellent and it attracts a lot of amounts of these forms people. take care and continue for the work!

  • zztops1988
    May 12th

    How does this tie to one of the other stories? Maybe I'm slow... because I could've been on a differnet blog. lol Nevermind. At any rate, it was a solid post. Cheers

    T. Johns
    Back scrubber

  • Czeslaw
    May 16th

    This tools are maby handy but I for example hate toolbars because they're hiding some space of the window. That's very anoing.

  • Myles Haffner
    May 29th

    Good job! Thanks for such astonishing writing on the Internet as I have been able to find here. I agree with most of what is written here and I'll be returning to this site again. Thanks again for posting such great reading material!

  • Ma Panyko
    May 29th

    Thanks mate, I must mention this is a clever description.If you could add more opinions would be brilliant. I'll certainly be back on this blog again soon. Bye

  • Blog4Cash
    May 30th

    Hi, well done site. Want to get money for blogging? Check out: http://bit.ly/PaidWriting

  • Errol Brandorff
    June 8th

    Hey, I just forwarded this to some friends, loving it!

  • Flights Rhodes
    June 24th

    very good work, i enjoy it!!

  • Sam C. Tailer
    July 6th

    Even though xp is faster, in my opinion, windows 7 is much better! good for gaming too. My wife disagrees.

  • Jannet Sabha
    July 28th

    Interesting article dude Thank you

Leave a Reply





Trackbacks/Pings

Please Note:

Comments are moderated by both human and automatic resources, to help control the amount of spam comments, as well as unsuitable comments. When you post a comment it should appear instantly, but a comment may be held for moderation if it is deemed suspect for a variety of reasons such as spam, or contains content that may be unsuitable for display. If this is the case you will be notified when you post your comment. Comments are reviewed on a daily basis. For information about comments on James' Blog please click here to read the comments policy.