<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: List navigation</title> <link rel="stylesheet" href="listnav.css" /> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href="">Wine</a></li> <li><a href="">Fruit</a></li> <li><a href="">Spreads</a></li> <li><a href="">Biscuits</a></li> </ul> </div> </body> </html>
.nav { list-style: none; margin: 0; padding: 0; width: 200px; } .nav li { border-left: 10px solid rgb(144,154,181); border-bottom: 1px solid rgb(144,154,181); } .nav li a:link, .nav li a:visited { background-color: rgb(192,202,229); color: rgb(49,52,61); padding: 0.5em; display: block; text-decoration: none; border-left: 5px solid rgb(239,213,252); }
li
element: <ul> <li><a href="">Wine</a></li> <li><a href="">Fruit</a></li> <li><a href="">Spreads</a></li> <li><a href="">Biscuits</a></li> </ul>
ul
as we’re doing here, or place the list
inside another container:<ul class="nav">
<li><a href="">Wine</a></li>
<li><a href="">Fruit</a></li>
<li><a href="">Spreads</a></li>
<li><a href="">Biscuits</a></li>
</ul>
.nav { list-style-type: none; margin: 0; padding: 0; width: 200px; }
.nav li { border-left: 10px solid rgb(144,154,181); border-bottom: 1px solid rgb(144,154,181); }
.nav li a:link, .nav li a:visited { background-color: rgb(192,202,229); color: rgb(49,52,61); padding: 0.5em; display: block; text-decoration: none; border-left: 5px solid rgb(239,213,252); }
background-color
and
color
using RGB color values in this case, although
you could also use hex. I’ve also added padding to the link itself and
set it to display: block
.
This last tweak is important, because the link is an inline
element and by default doesn’t take up the full area of the li
—so it’s not a nice easy target to click. To
make links display like block-level elements, we need to
explicitly declare the value of the
display
property to be
block
. We’ll discover more about this in later
chapters; for now, remember that if you want your link to take up the
full area of its container, it needs to be set to display:
block
.
We need to add the padding to the link, not the list item; if we
added it to the list item, the padding area would be unclickable—and we
want to create an area where it’s easy to click the link.
I’ve also set text-decoration
to be
none
so as to remove the underline that browsers
apply to links, and given the link a left border, producing a double
border effect as the li
already has a
border.
You should now have a navigation bar as shown in Figure 4.1. You can play around with this technique trying
different colors and stylistic effects once you’ve mastered the basic
idea.
:hover
pseudo-class, which we met when
discussing styling links in Chapter 2.
Let’s take the previous list-navigation example and add the
following rule to create a rollover effect: .nav li a:hover { background-color: rgb(144,154,181); color: rgb(255,255,255); border-left: 5px solid rgb(250,136,234); }
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Chapter 4: List navigation</title>
<link rel="stylesheet" href="listnav-nested.css" />
</head>
<body>
<div class="wrapper">
<ul class="nav">
<li><a href="">Wine</a>
<ul>
<li><a href="">Red</a></li>
<li><a href="">White</a></li>
<li><a href="">Rosé</a></li>
</ul>
</li>
<li><a href="">Fruit</a></li>
<li><a href="">Spreads</a></li>
<li><a href="">Biscuits</a></li>
</ul>
</div>
</body>
</html>
body {
background-color: #fff;
color: #000;
margin: 0;
padding: 0;
font: 0.75em/1.3 "Lucida Grande", "Lucida Sans Unicode",
"Lucida Sans", Verdana, Tahoma, sans-serif;
}
.wrapper {
width: 80%;
margin: 20px auto 40px auto;
}
.nav {
list-style: none;
margin: 0;
padding: 0;
width: 200px;
}
.nav li {
border-left: 10px solid rgb(144,154,181);
border-bottom: 1px solid rgb(144,154,181);
}
.nav li a:link,
.nav li a:visited {
background-color: rgb(192,202,229);
color: rgb(49,52,61);
padding: 0.5em;
display: block;
text-decoration: none;
border-left: 5px solid rgb(239,213,252);
}
.nav li a:hover {
background-color: rgb(144,154,181);
color: rgb(255,255,255);
border-left: 5px solid rgb(250,136,234);
}
.nav ul {
list-style: none;
margin: 0;
padding: 0;
border: 0;
}
.nav ul li {
border: 0;
}
.nav ul li a:link,
.nav ul li a:visited {
background-color: rgb(237,241,252);
color: rgb(49,52,61);
padding: 0.5em 0.5em 0.5em 1em;
display: block;
text-decoration: none;
border-left: 5px solid rgb(239,213,252);
}
.nav ul li a:hover {
background-color: rgb(255,255,255);
color: rgb(49,52,61);
border-left: 5px solid rgb(250,136,234);
}
li
element of the main list:<ul class="nav">
<li><a href="">Wine</a>
<ul>
<li><a href="">Red</a></li>
<li><a href="">White</a></li>
<li><a href="">Rosé</a></li>
</ul>
</li>
<li><a href="">Fruit</a></li>
<li><a href="">Spreads</a></li>
<li><a href="">Biscuits</a></li>
</ul>
li
. The list now displays as in Figure 4.9:.nav ul { list-style: none; margin: 0; padding: 0; border: 0; } .nav ul li { border: 0; }
.nav ul li a:link, .nav ul li a:visited { background-color: rgb(237,241,252); color: rgb(49,52,61); padding: 0.5em 0.5em 0.5em 1em; display: block; text-decoration: none; border-left: 5px solid rgb(239,213,252); } .nav ul li a:hover { background-color: rgb(255,255,255); color: rgb(49,52,61); border-left: 5px solid rgb(250,136,234); }
li
to create
a pleasing effect; I also added some different rules for the hover state
of these subitems. Remember that here you’re overwriting the values set
on links for the external list, so you only need to set those you’ve
changed.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Horizontal navigation</title> <link rel="stylesheet" href="horizontal.css" /> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href="">Wine</a></li> <li><a href="">Fruit</a></li> <li><a href="">Spreads</a></li> <li><a href="">Biscuits</a></li> </ul> </div> </body> </html>
.nav { list-style: none; margin: 0; padding: 0; } .nav li { float: left; min-width: 8em; margin-right: 0.5em; text-align: center; } .nav li a:link, .nav li a:visited { background-color: rgb(255, 255, 255); color: rgb(85, 85, 102); display: block; padding: 0.2em; text-decoration: none; font-weight: bold; margin: 0 0 0.2em 0; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; -webkit-box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5); -moz-box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5); box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5); } .nav li a:hover { background-color: rgba(255, 255, 255, 0.8); color: rgb(43, 43, 77); }
<ul class="nav"> <li><a href="">Wine</a></li> <li><a href="">Fruit</a></li> <li><a href="">Spreads</a></li> <li><a href="">Biscuits</a></li> </ul>
display
property of the
li
to
inline
as we discussed in
Chapter 2
; however, to make the list items easier to style, I’m going to use a different method that relies on the
float
property. We’ll discuss
float
properly later in the book; for now, you can see how it floats items alongside each other:
.nav li { float: left; min-width: 8em; margin-right: 0.5em; text-align: center; }
.nav li a:link, .nav li a:visited { background-color: rgb(255, 255, 255); color: rgb(85, 85, 102); display: block; padding: 0.2em; text-decoration: none; font-weight: bold; margin: 0 0 0.2em 0; … }
.nav li a:link,
.nav li a:visited {
background-color: rgb(255, 255, 255);
color: rgb(85, 85, 102);
display: block;
padding: 0.2em;
text-decoration: none;
font-weight: bold;
margin: 0 0 0.2em 0;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5);
-moz-box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5);
box-shadow: 3px 3px 3px 3px rgba(43, 43, 77, 0.5);
}
.nav li a:hover { background-color: rgba(255, 255, 255, 0.8); color: rgb(43, 43, 77); }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Horizontal navigation</title> <link rel="stylesheet" href="tabs.css" /> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href="">Wine</a></li> <li><a href="">Fruit</a></li> <li class="selected"><a href="">Spreads</a></li> <li><a href="">Biscuits</a></li> </ul> <div class="content"> <h1>Spreads that go with cheese</h1> </div> </div> </body> </html>
.nav { list-style: none; margin: 0; padding: 0; float: left; width: 100%; } .nav li { float: left; min-width: 8em; text-align: center; } .nav li a:link, .nav li a:visited { background-color: rgba(255,255,255,0.4); color: rgb(0,0,0); text-decoration: none; display: block; padding: 0.75em; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-radius: 3px 3px 0px 0px; border-radius: 3px 3px 0px 0px; -webkit-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); -moz-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5); } .nav li.selected a:link, .nav li.selected a:visited { background-color: rgb(255,255,255); } .nav li a:hover { background-color: rgba(255,255,255,0.8); } .content { clear: both; background-color: #fff; color: #000; padding: 1em; } h1 { font-size: 128.6%; }
class
of selected
to one item:
<ul class="nav">
<li><a href="">Wine</a></li>
<li><a href="">Fruit</a></li>
<li class="selected"><a href="">Spreads</a></li>
<li><a href="">Biscuits</a></li>
</ul>
.nav { list-style: none; margin: 0; padding: 0; float: left; width: 100%; } .nav li { float: left; min-width: 8em; text-align: center; }
.nav li a:link, .nav li a:visited { background-color: rgba(255,255,255,0.4); color: rgb(0,0,0); text-decoration: none; display: block; padding: 0.75em; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-radius: 3px 3px 0px 0px; border-radius: 3px 3px 0px 0px; -webkit-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); -moz-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2); text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5); }
border-radius
to slightly curve the
top corners of the tabs,
box-shadow
to create a shadow effect
round the sides and top, and
text-shadow
to enhance the
text.I now want to make my selected tab look as if it joins onto
the background color of the content area. I do this using the following
declaration which targets the li
with
a class
of selected
. All it does is change the
background color to white:.nav li.selected a:link, .nav li.selected a:visited { background-color: rgb(255,255,255); }
include
statement—this may not be possible. How
can you indicate which menu item is selected here?
body
element of the page that is the same as one of the classes: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Horizontal navigation</title> <link rel="stylesheet" href="selected.css" /> </head> <body id="spreads"> <div class="wrapper"> <ul class="nav"> <li class="wine"><a href="">Wine</a></li> <li class="fruit"><a href="">Fruit</a></li> <li class="spreads"><a href="">Spreads</a></li> <li class="biscuits"><a href="">Biscuits</a></li> </ul> <div class="content"> <h1>Spreads that go with cheese</h1> </div> </div> </body> </html>
li
with a class
only if it’s inside the body
with the same id
:.nav {
list-style: none;
margin: 0;
padding: 0;
float: left;
width: 100%;
}
.nav li {
float: left;
min-width: 8em;
text-align: center;
}
.nav li a:link,
.nav li a:visited {
background-color: rgba(255,255,255,0.4);
color: rgb(0,0,0);
text-decoration: none;
display: block;
padding: 0.75em;
-moz-border-radius-topleft: 3px;
-moz-border-radius-topright: 3px;
-webkit-border-radius: 3px 3px 0px 0px;
border-radius: 3px 3px 0px 0px;
-webkit-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2);
box-shadow: 0px -3px 2px 0px rgba(0, 0, 0, 0.2);
text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}
#spreads .nav li.spreads a:link,
#spreads .nav li.spreads a:visited,
#fruit .nav li.fruit a:link,
#fruit .nav li.fruit a:visited,
#wine .nav li.wine a:link,
#wine .nav li.wine a:visited,
#biscuits .nav li.biscuits a:link,
#biscuits .nav li.biscuits a:visited {
background-color: rgb(255,255,255);
}
.nav li a:hover {
background-color: rgba(255,255,255,0.8);
}
body
element’s ID to any of the other ID names, you will see the selected
menu tab change. This works because when we use the following selector,
we’re saying, “target the link inside an li
with a class of spreads
, which is inside an element with an
ID of spreads
”:#spreads .nav li.spreads a:linkSo a
li
with a class
of wine
fails to match, and a li
with a class
of spreads
inside the body
with an id
of wine
also wouldn’t match. You then just need
to output the section name as an id
on the body
tag, which you may have
access to in your page, or be able to output via your CMS.
a
elements as their descriptions, but also
within strong
elements set to display
as blocks:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Horizontal navigation</title> <link rel="stylesheet" href="horizontal2.css" /> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href=""><strong>Wine</strong> <small>Cheese & wine parties - a classic!</small> </a></li> <li><a href=""><strong>Fruit</strong> <small>Grapes, apples - get your 5 a day while eating cheese.</small> </a></li> <li><a href=""><strong>Spreads</strong> <small>Pickles, chutneys, roasted garlic and more.</small> </a></li> <li><a href=""><strong>Biscuits</strong> <small>Put your cheese onto Bath Ovals, digestives and water biscuits.</small> </a></li> </ul> </div> </body> </html>
.nav { list-style: none; margin: 0; padding: 0; } .nav li { float: left; width: 130px; margin-right: 20px; } .nav li a:link strong, .nav li a:visited strong { font-size: 157.1%; display: block; font-weight: normal; color: rgb(119,126,134); font-style: normal; } .nav li a:link, .nav li a:visited { text-decoration: none; color: rgb(93,78,72); font-style: italic; } .nav li a:hover, .nav li a:hover strong { color: rgb(0,0,0); }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Showing external links</title> <link rel="stylesheet" href="external-links.css" /> </head> <body> <div class="wrapper"> <p>You can search for more delicious cheese recipes using <a href="http://google.com">Google</a> or view more recipes on <a href="/recipes">this site</a>.</p> </div> </body> </html>
href
attributes containing a value starting
with http:
and add a background
image:a[href^="http:"] { padding-left: 20px; background-image: url(icon-link-external.png); background-repeat: no-repeat; }
http:
(which should be external, as we don’t
link to pages on our own site this way) will display with the world
icon.
a[href ^="http:"]We’re selecting the
href
attribute, and we want our selector to match when it finds the text
http:
at the beginning of the
attribute value. The ^=
operator means “begins with.”
You could use a similar selector to match all email links; for example,
a[href ^="mailto:"]
.
Another
useful attribute selector is to target the file extension of a link.
This means that you can add a small icon to show that a document is a
PDF or other document type, depending on the extension. The selector
a[href $=".pdf"]
will match any link that has a file
extension of .pdf. The $=
operator means “ends with,” so this selector will match when an href
attribute value ends with
.pdf: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Using attribute selectors</title> <link rel="stylesheet" href="external-links2.css" /> </head> <body> <div class="wrapper"> <ul class="links"> <li><a href="http://google.com">Go somewhere else</a></li> <li><a href="/files/example.pdf">Download a PDF</a></li> <li><a href="mailto:[email protected]">Send an email</a></li> </ul> </div> </body> </html>
a[href^="http:"] { padding-left: 20px; background-image: url(icon-link-external.png); background-repeat: no-repeat; } a[href^="mailto:"] { padding-left: 20px; background-image: url(icon-link-email.png); background-repeat: no-repeat; } a[href$=".pdf"] { padding-left: 20px; background-image: url(icon-pdf.png); background-repeat: no-repeat; }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Image rollovers</title> <link rel="stylesheet" href="rollover-image.css" /> </head> <body> <div class="wrapper"> <ul class="intouch"> <li class="twitter"><a href="">Twitter</a></li> <li class="rss"><a href="">RSS</a></li> <li class="email"><a href="">Email</a></li> </ul> </div> </body> </html>
.intouch { list-style: none; margin: 0; padding: 0; } .intouch li a:link, .intouch li a:visited { padding: 0.5em 0 0.5em 40px; display: block; font-weight: bold; background-repeat: no-repeat; background-image: url(sprite-roll.png); text-decoration: none; color: rgb(136,136,136); } .intouch li.twitter a:link, .intouch li.twitter a:visited { background-position: 0 6px; } .intouch li.rss a:link, .intouch li.rss a:visited { background-position: 0 -30px; } .intouch li.email a:link, .intouch li.email a:visited { background-position: 0 -60px; } .intouch li.twitter a:hover { background-position: 0 -90px; color: rgb(105,210,231); } .intouch li.rss a:hover { background-position: 0 -126px; color: rgb(243,134,48); } .intouch li.email a:hover { background-position: 0 -156px; color: rgb(56,55,54); }
li
a class
to indicate the type of link it
is:<ul class="intouch"> <li class="twitter"><a href="">Twitter</a></li> <li class="rss"><a href="">RSS</a></li> <li class="email"><a href="">Email</a></li> </ul>
no-repeat
:.intouch {
list-style: none;
margin: 0;
padding: 0;
}
.intouch li a:link, .intouch li a:visited {
padding: 0.5em 0 0.5em 40px;
display: block;
font-weight: bold;
background-repeat: no-repeat;
background-image: url(sprite-roll.png);
text-decoration: none;
color: rgb(136,136,136);
}
.intouch li.twitter a:link, .intouch li.twitter a:visited { background-position: 0 6px; }
.intouch li.rss a:link, .intouch li.rss a:visited { background-position: 0 -30px; } .intouch li.email a:link, .intouch li.email a:visited { background-position: 0 -60px; }
.intouch li.twitter a:hover { background-position: 0 -90px; color: rgb(105,210,231); } .intouch li.rss a:hover { background-position: 0 -126px; color: rgb(243,134,48); } .intouch li.email a:hover { background-position: 0 -156px; color: rgb(56,55,54); }
opacity
property.
The effect shown in Figure 4.20 is
achieved with the same markup we’ve been using and an image sprite,
but this one only has three states in it. I have then used the
opacity
property to make the image and text
semitransparent.
On hover, I set opacity
to
1
—that’s fully opaque (not transparent at all),
making a simple rollover effect:.intouch li a:link, .intouch li a:visited { padding: 0.5em 0 0.5em 40px; display: block; font-weight: bold; background-repeat: no-repeat; background-image: url(sprite.png); text-decoration: none; } .intouch li.twitter a:link, .intouch li.twitter a:visited { background-position: 0 6px; color: rgb(105,210,231); opacity: 0.5; } .intouch li.rss a:link, .intouch li.rss a:visited { background-position: 0 -30px; color: rgb(243,134,48); opacity: 0.5; } .intouch li.email a:link, .intouch li.email a:visited { background-position: 0 -60px; color: rgb(56,55,54); opacity: 0.5; } .intouch li.twitter a:hover, .intouch li.rss a:hover, .intouch li.email a:hover { opacity: 1; }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Sitemaps</title> <link rel="stylesheet" href="sitemap.css" /> </head> <body> <div class="wrapper"> <ul class="sitemap"> <li><a href="">Home</a></li> <li><a href="">About us</a> <ul> <li><a href="">Directors</a></li> <li><a href="">History</a></li> </ul> </li> <li><a href="">Products</a></li> <li><a href="">Ordering information</a> <ul> <li><a href="">Our shops</a> <ul> <li><a href="">London</a></li> <li><a href="">Newcastle</a></li> </ul> </li> <li><a href="">Other stockists</a></li> </ul> </li> <li><a href="">Contact Us</a></li> </ul> </div> </body> </html>
.sitemap { list-style: none; margin: 0; padding: 0; } .sitemap > li { border: 2px solid rgba(153,178,183,0.2); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; margin: 0 0 1em 0; } .sitemap > li:hover { border: 2px solid rgba(153,178,183,1); } .sitemap > li > a:link, .sitemap > li > a:visited { background-color: rgba(153,178,183,0.1); color: rgb(0,0,0); text-decoration: none; display: block; padding: 0.75em; } .sitemap > li:hover > a:link, .sitemap > li:hover > a:visited { background-color: rgba(153,178,183,0.5); } .sitemap ul { margin: 1em 0 1em 0; padding: 0; list-style: none; line-height: 1.8; } .sitemap ul ul { margin: 0.5em 0 0.5em 0; } .sitemap ul a:link, .sitemap ul a:visited { padding: 0.75em; text-decoration: none; color: rgb(69,80,83); } .sitemap ul ul a:link:before, .sitemap ul ul a:visited:before { content: "> "; }
li
of the parent
list. Without CSS, the sitemap displays as in Figure 4.22. I’m going to wrap each main section—that’s each top-level
navigation point and its subnavigation—with a border to help demonstrate
it is a section of the site:.sitemap { list-style: none; margin: 0; padding: 0; } .sitemap > li { border: 2px solid rgba(153,178,183,0.2); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; margin: 0 0 1em 0; } .sitemap > li:hover { border: 2px solid rgba(153,178,183,1); }
li
that is a direct child of
.sitemap
; these will be the top-level navigation
elements. This is indicated in Figure 4.23. I’m
using RGBA for the border so that I can tweak the alpha value on hover
of the list item; this will give a nice visual indication of the part of
the map the user is in. Now I move on to the links that are within these top-level
list items, making them visually display as the main navigation item for
that section:.sitemap > li > a:link, .sitemap > li > a:visited { background-color: rgba(153,178,183,0.1); color: rgb(0,0,0); text-decoration: none; display: block; padding: 0.75em; } .sitemap > li:hover > a:link, .sitemap > li:hover > a:visited { background-color: rgba(153,178,183,0.5); }
li:hover
so that the state change happens when any
part of the li
is hovered over, as in
Figure 4.24. We can now style the items inside the section—the sublists
and links:.sitemap ul { margin: 1em 0 1em 0; padding: 0; list-style: none; line-height: 1.8; } .sitemap ul ul { margin: 0.5em 0 0.5em 0; } .sitemap ul a:link, .sitemap ul a:visited { padding: 0.75em; text-decoration: none; color: rgb(69,80,83); } .sitemap ul ul a:link:before, .sitemap ul ul a:visited:before { content: "> "; }
>
character on the “sub-sub” lists using the
pseudo-element :before
and generated
content.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Chapter 4: Dropdown navigation</title> <link rel="stylesheet" href="dropdown.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/ ↵jquery.min.js" type="text/javascript"></script> <script src="superfish.js" type="text/javascript"></script> <script> $(document).ready(function() { $('ul.nav').superfish({ delay: 1000, animation: {opacity:'show',height:'show'}, speed: 'fast', autoArrows: false, dropShadows: false }); }); </script> </head> <body> <div class="wrapper"> <ul class="nav"> <li><a href="">Home</a></li> <li><a href="">About us</a> <ul> <li><a href="">Directors</a></li> <li><a href="">History</a></li> </ul> </li> <li><a href="">Products</a></li> <li><a href="">Ordering</a> <ul> <li><a href="">Our shops</a></li> <li><a href="">Other stockists</a></li> </ul> </li> <li><a href="">Contact Us</a></li> </ul> </div> </body> </html>
.nav { list-style: none; margin: 0; padding: 0; font-size: 114.3%; } .nav > li { float: left; width: 130px; margin-right: 20px; position: relative; } .nav li a:link, .nav li a:visited { display: block; text-decoration: none; color: rgb(122,106,83); } .nav li:hover ul, .nav li.sfHover ul { margin-left: 0; } .nav li a:hover { color: rgb(153,178,183); } .nav ul { position: absolute; background-color: rgb(213,222,217); border: 5px solid rgb(153,178,183); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; padding: 0.5em; margin: 0.5em 0 0 -9999px; -webkit-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2)); -moz-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2); box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2)); list-style: none; font-size: 85.7%; width: 8em; line-height: 1.8; } .nav ul li a:link, .nav ul li a:visited { color: rgb(0,0,0); } .nav ul li a:hover { color: rgb(122,106,83); }
:hover
dynamic pseudo-class on elements
other than links would enable us to create CSS-only drop-down menus
without needing JavaScript at all. Solutions such as the now-famous
Suckerfish menus were developed using this
technique.
The problem with CSS-only menus is that they can be very difficult
to use. While there are no accessibility issues for screen-reader users
in the way there were for those using old JavaScript-inserted menus
(because the markup is right there on the page), the menus are generally
inaccessible for those using a keyboard to navigate, and can be fiddly
to click on when using a mouse, as we’ll soon see. My advice is to
create your menus using CSS, but then use JavaScript to enhance their
usability, as I’ll show you here.
We start with a set of nested lists, as seen in Figure 4.26. We can now use CSS to display this list as a horizontal
menu:.nav { list-style: none; margin: 0; padding: 0; font-size: 114.3%; } .nav > li { float: left; width: 130px; margin-right: 20px; position: relative; } .nav li a:link, .nav li a:visited { display: block; text-decoration: none; color: rgb(122,106,83); } .nav li a:hover { color: rgb(153,178,183); }
ul
. I’m giving these a
border and background color, and also having some fun with CSS3
properties, adding rounded corners and a
box-shadow
:.nav ul { position: absolute; background-color: rgb(213,222,217); border: 5px solid rgb(153,178,183); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; padding: 0.5em; margin: 0.5em 0 0 0; -webkit-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2)); -moz-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2); box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2)); list-style: none; font-size: 85.7%; width: 8em; line-height: 1.8; } .nav ul li a:link, .nav ul li a:visited { color: rgb(0,0,0); } .nav ul li a:hover { color: rgb(122,106,83); }
ul
to a large negative value,
throwing it off the side of the screen:.nav ul {
position: absolute;
background-color: rgb(213,222,217);
border: 5px solid rgb(153,178,183);
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding: 0.5em;
margin: 0.5em 0 0 -9999px;
-webkit-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2));
-moz-box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2);
box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2));
list-style: none;
font-size: 85.7%;
width: 8em;
line-height: 1.8;
}
display:
none
to hide them. I’m using a negative margin rather than
display: none
because screen readers that honor CSS
may read the display: none
declaration and not give
the user the opportunity to navigate to the hidden items.
The menus will now disappear when the page is reloaded. To bring
them back on hover, add the following rule: .nav li:hover ul { margin-left: 0; }When we hover over the
li
, the ul
within that li
will have its
margin-left
set to 0
, bringing
it back into view.
If you test this out, you’ll see that your drop-downs work;
however, you’ll probably also find that being able to click a link in
the drop-down is tricky. Sometimes it will disappear before you get onto
it! The other issue is that if you try and tab to the links using the
keyboard, you’ll find that the browser does tab to the hidden items—but
because they’re offscreen, they’re invisible to the naked eye.
To deal with this issue, we can add some JavaScript. There
are a number of methods of doing this, but as an example, I’m going to
use a jQuery plugin called
Superfish.
This plugin simply enhances the CSS menu you’ve already built. .nav li:hover ul, .nav li.sfHover ul {
margin-left: 0;
}
Download Superfish, add the
superfish.js file to your site, and then add the
JavaScript to the head of your document:<head>
<meta charset="utf-8" />
<title>Chapter 4: Dropdown navigation</title>
<link rel="stylesheet" href="dropdown.css" />
<script src="http://ajax.googleapis.com/ajax/libs/
↵jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="superfish.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('ul.nav').superfish({
delay: 1000,
animation: {opacity:'show',height:'show'},
speed: 'fast',
autoArrows: false,
dropShadows: false
});
});
</script>
</head>
18.188.187.165