Simple Tabbed Navigation with CSS
May 9th, 2008 | Design & Development |I had to create some tabbed navigation for a project at work recently. Not anything new really, lots of people implement it in lots of different ways. A colleague recommended a method demonstrated on SimpleBits for creating simplified css tabs however it uses background images to get the tab effect and I don't like that.
I'd much prefer to use a pure CSS method that doesn't involve editing an image if I want to make changes. So here I have it, a simple CSS only method of doing tabbed navigation. I've tested it in IE6, IE7, FF2, Opera 9 and Safari 3.1, all fine.
You can view/download the example here: Simple CSS Tabbed Navigation (to download: right click > save link as)
OR copy the code from below.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<title>CSS Tabbed Navigation</title>
-
/*
-
First, a very minimal css reset as I don't
-
want any default margins or padding messing
-
up the layout.
-
*/
-
h1, ul{margin:0;padding:0;}
-
-
body{background-color:#eee;}
-
-
/*
-
the main wrapper controls the overall width of the page and centers it
-
*/
-
#wrapper{
-
width: 800px;
-
margin: 0 auto;
-
}
-
-
/*
-
Here you can change the tab border and background in one place
-
*/
-
#page, #tabs li a{
-
background-color:#fff;
-
border:solid 1px #333;
-
}
-
-
/*
-
The important thing here is that bottom:-1px
-
This pulls the page element up to sit flush
-
with the bottom of the list items while the .active
-
list item is allowed to overlap the top border of the
-
page element by 1px.
-
*/
-
#tabs{
-
float:left; /* makes the positioning work properly in IE */
-
list-style-type:none;
-
position:relative;
-
bottom:-1px;
-
}
-
-
/*
-
make the tabs line up horizontally and space them out
-
*/
-
#tabs li{
-
float:left;
-
margin:0 0 0 5px;
-
display:inline; /*fixes double margin (IE bug)*/
-
}
-
-
/*
-
not too much going on here
-
just makes the link expand
-
to fill the tab
-
*/
-
#tabs li a{
-
display:block;
-
line-height:24px;
-
padding:0 10px;
-
text-decoration:none;
-
border-bottom:none;
-
}
-
-
/*
-
this makes the active tab protrude
-
1px more than the other tabs,
-
thus allowing it to cover the top
-
border of the page element.
-
*/
-
#tabs li a.active{
-
padding-bottom:1px;
-
}
-
-
/*
-
force the page element to sit beneath the floated list
-
*/
-
#page{
-
clear:both;
-
padding: 10px;
-
}
-
</style>
-
</head>
-
<div id="wrapper">
-
<ul id="tabs">
-
</ul>
-
<div id="page">
-
<h1>Simple Tabbed Navigation with CSS</h1>
-
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed elementum nulla. Vivamus ut sem sed turpis condimentum ultricies. Sed volutpat cursus sapien. Nulla arcu. Donec laoreet semper neque. Quisque elit purus, vestibulum sed, dignissim et, faucibus sit amet, magna. Donec lectus massa, facilisis eu, dictum eu, aliquet at, ante. Maecenas ut pede. Vestibulum fermentum pretium velit. Curabitur sit amet leo. Proin ultrices, tortor vitae bibendum ultrices, tortor turpis vestibulum nulla, a malesuada orci nibh ac nisi.
-
</p>
-
</div>
-
</div>
-
</body>
-
</html>







