Quantcast
Channel: CSS Atoms | CSS3 tutorials, tips, and articles » CSS navigation
Viewing all articles
Browse latest Browse all 2

Creating a Site Navigation Using CSS

$
0
0

With a dash of CSS, we can turn a plain old list into a fairly snazzy navigation bar in no time at all. Today, we’ll take a look at how to build a navigational element for our website using HTML and CSS as our tools.

We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting, was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

STEP ONE:
The HTML

Our navigation will be based on a simple unordered list, wrap each item in a link tag, and then we’ll flesh out the list using CSS. Here’s what the initial HTML setup looks like:

Code Block
index.html
Our basic setup for our navigation:
<body>
    <ul id="navigation">
       <li><a class="link" href="#"> Home </a></li>
       <li><a class="link" href="#"> About </a></li>
       <li><a class="link" href="#"> Ninjas </a></li>
       <li><a class="link" href="#"> Bad Guys </a></li>
       <li><a class="link" href="#"> Good Guys </a></li>
    </ul>
</body>

That should be plenty of navigation for our simple project. Viewing this page in a browser will display a vertical list of our items:
Step One

STEP TWO:
The CSS

Since we want our navigation to be horizontal, we’ll need to use a bit of CSS.
We’ll declare that we want any items in our navigation list to be displayed inline. To do that, we will use the float property. Let’s add the first line of CSS now:

Code Block
style.css
Displaying the list items inline:
#navigation li
{    
 floatleft;    
}

Now our navigation elements will all appear in a nice horizontal line, as much of the navigation you see online today does.
Step Two

STEP THREE:
Element Definition

The next step will be to give each a bit of definition. To do this, we’ll give them a defined height and width, as well as a border. We’ll also add some padding to each of the elements, and remove the bullet from the items. Here’s what the CSS should look like now:

Code Block
style.css
Adding definition to the elements:
#navigation li
{
    floatleft;
    list-style-typenone;
}
 
.link
{
    border1px solid #999;
    displayblock;
    height20px;
    width90px;
    padding10px;
}

Now, if you take a look at the page, you’ll see our list items are starting to look a bit more like a typical navigation. They each have a border, a bit of padding, and are being displayed side-by-side. Exactly, what we are looking for.
Step Three

STEP FOUR:
Link Formatting

Let’s center our link titles a bit, and remove the underline. We’ll do that by setting the text-align property to center, and by setting the text-decoration property to none. Here’s what that looks like:

Code Block
style.css
Changing the text alignment and underline:
#navigation li
{
    floatleft;
    list-style-typenone;
    text-aligncenter;
}
 
.link
{
    border1px solid #999;
    displayblock;
    floatright;
    height20px;
    width90px;
    text-decorationnone;
    padding10px;
}

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting!

Preview in browser:
Step Four

STEP FIVE:
Styling Text

So we now have a basic navigation, however the text still looks fairly plain. Let’s spruce that up a bit by making the font size slightly larger and bolder. We’ll implement this by setting the font-weight property of our links to bold, and the font-size property to 18px. Here’s what that looks like in the code:

Code Block
style.css
Changing the text style:
#navigation li
{
    floatleft;
    list-style-typenone;
    text-aligncenter;
}
 
.link
{
    border1px solid #999;
    displayblock;
    floatright;
    font-size18px;
    font-weightbold;
    height20px;
    width90px;
    text-decorationnone;
    padding10px;
}

Preview in browser:
Step Five

STEP SIX:
Styling Links

Our navigation is coming together quite nicely. We can now begin setting up what we want to happen when a user rolls over our navigational elements. First, know that every browser chooses its own default color for links. Google Chrome and Firefox use purple, Internet Explorer uses a light-blue. We’ll want to go ahead and set our own default color for links, so let’s do that now:

Code Block
style.css
Changing the links’ default color:
#navigation li
{
    floatleft;
    list-style-typenone;
    text-aligncenter;
}
 
.link
{
    color#545454;
    border1px solid #999;
    displayblock;
    floatright;
    font-size18px;
    font-weightbold;
    height20px;
    width90px;
    text-decorationnone;
    padding10px;
}

Preview in browser:
Step Six

STEP SEVEN:
Hover States

The last step for the day is to create a hover state so that when a user hovers over one of the links, the background color changes. Creating a hover state in CSS requires the use of a psuedo-selector which looks like this:

Code Block
style.css
Adding hover states to our links:
.link:hover
{
    background-colorBlack;
    colorWhite;    
}

Adding the previous line of code to the style sheet will result in you being finished with a pretty good looking navigation in a fairly short amount of time. Preview the navigation in a browser to bask in all of its goodness:
Final Result

Creating good looking navigation isn’t hard to do, and only requires that you know how to target the elements and style them to look like a typical navigation. Mess around with colors and hover states until you get the style that suits your site perfectly. Join us next time when we discuss how to spruce up your paragraphs using some crafty CSS techniques. See you then!

If you’re ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Download Site Navigation Using CSS Source Files


Viewing all articles
Browse latest Browse all 2

Trending Articles