I think this is a commonly-known technique among experienced CSS developers, but many beginning web designers will probably resort to images to create this effect, which is not necessary. So, in this brief tutorial I’ll show you how to create a navigation bar with beveled edges using pure CSS, identical to that on CNN.
View the demo page to see exactly what we’ll be creating.
First, the markup, which is nothing unusual, just an unordered list:
- <ul id="navigation">
- <li class="first"><a href="#">Homea>li>
- <li><a href="#">Videoa>li>
- <li><a href="#">NewsPulsea>li>
- <li><a href="#">U.S.a>li>
- <li><a href="#">Worlda>li>
- <li><a href="#">Politicsa>li>
- <li><a href="#">Justicea>li>
- <li><a href="#">Entertainmenta>li>
- <li><a href="#">Techa>li>
- <li><a href="#">Healtha>li>
- <li><a href="#">Livinga>li>
- <li><a href="#">Travela>li>
- <li><a href="#">Opiniona>li>
- <li><a href="#">iReporta>li>
- <li><a href="#">Moneya>li>
- <li><a href="#">Sportsa>li>
- ul>
The only thing to note in that code is the class “first”, which will help us round out the effect.
Here’s the CSS for the
- element, which has an
- #navigation {
- list-style: none;
- background: #b60002;
- overflow: hidden;
- width: 962px;
- }
id of navigation: The primary navigation element above is styled with a set width, a background color, and a set height. The width is not that important, but it ensures that the element can be centered using margin: auto.
Next, we need to style the individual
- #navigation li {
- float: left;
- border-right: solid 1px #ca0002;
- height: 35px;
- }
The height matches that of the
- element, and each
- element is given a solid right border, the latter of which constitutes the first part of the beveled effect.
Next are the anchors inside the
- elements. Here’s the CSS:
- #navigation li a:link, #navigation li a:visited {
- text-decoration: none;
- display: block;
- height: 35px;
- color: #fff;
- line-height: 35px;
- padding: 0 9px 0 9px;
- border-right: solid 1px #990000;
- font-family: Arial, Helvetica, sans-serif;
- font-size: 12px;
- font-weight: bold;
- }
The important things to note above, again, is the right border setting, which is the second ingredient for getting the beveled effect. Thus, the color of this border is darker than the border on the
- element. The rest of the CSS shown above is trivial, making the buttons look similar to those on CNN’s website.
So, although we can’t create a double border on a single element without images, we can get around that by taking advantage of the nested elements that are part of every navigation bar, the anchor and the list item.
Next we give each button a hover state:
- #navigation li a:hover {
- background: #990000;
- color: #fff;
- }
The result is missing just one element. Since each button has a double border on the right, that means the leftmost button will not have a border on the left. So, we add a class to the first
- element to give that button a left border, which completes the look:
- #navigation li.first {
- border-left: solid 1px #ca0002;
- }
It’s that simple. No need to slice up images in Photoshop; you can create a simple, yet beautiful beveled effect with some pretty straightforward CSS.
Nema komentara:
Objavi komentar