Quick CSS class explanation

Associate
Joined
7 Feb 2008
Posts
1,196
Location
Surrey
Hey guys, I'm using this im my CSS to work as a rollover image link. Anyone care to explain what it's doing :) Its the part double quoted I'm stumped on.

CSS said:
* html a:hover {
visibility:visible
}

/*About*/
.about_nav {
float: left;
font-family: arial, helvetica, sans-serif;
background: url(images/buttons/about_b.jpg) no-repeat;
white-space: nowrap;
display: block;
width: 160px;
height: 100px;
margin: 0;
padding: 0;
}

.about_nav a {
display: block;
color: #000000;
font-size: 11px;
width: 160px;
height: 100px;
display: block;
float: left;
color: black;
text-decoration: none;
}

.about_nav img {
width: 160px;
height: 100px;
border: 0
}

.about_nav a:hover img {
visibility:hidden
}

And the XHTML part
XHTML said:
<div class="about_nav">
<a href="about.html"><img src="images/buttons/about_a.jpg" alt="About" border="0" /></a></div>

Thank you - here's it so far in action www.guildwarsbase.com/uni/web
 
That sure is a very messy way of going about it. You could do it considerably easier and with less css and html.


To answer your question regarding the bit you've double quoted...

* html is used to target IE6 and the css itself is just telling it to make all links visible on hover. Going from the code you've posted, it seems kind of pointless. (however, with IE6, you never know, it could be there to fix a bug the designer came across)
 
I was thinking that that CSS seems pointless to, but it does make sense if it was being used for a particular bug...
 
Is there a simpler CSS/XHTML way of doing it? I don't want to use javascript, I saw a way of placing rollover buttons in an ordered list tag <ol> to avoid using float. But I don't this is this XHTML valid.
 
Not sure about simpler, but there are other ways to produce rollover effects with CSS.

Can't say i use a:hover img....

I always use backgrounds for rollover effects....

Example...

Code:
.myclass 
{
background-image:url('myimage.jpg');
background-position:0 0;
}

.myclass:hover
{
background-position:0 20px;
}
.myclass a
{
do whatever you want with the links here
}
You have 1 image, where the image is different as you move down or up vertically, then you simply change the background position on hover...

My class could be a LI element or whatever else....

Can't say if that's XHTML valid, but i've been coding CSS in stylish for so long now i tend to do whatever is easiest (and as long as it works in Firefox/Stylish)..... hehehe... :)

Of course you could change the image on hover as appose to changing the position, the possibilities are quite numerous....

You could also do something like this...

Code:
<div class="about_nav">
        <a href="about.html"><img class="thislink" src="images/buttons/transparent_pixel.gif" alt="About" border="0" width="width you want" height="height you want" /></a></div>

Where transparent_pixel.gif is a 1 pixel transparent gif... usually known as spacer.gif for me (Fireworks creates one by default when exporting images with HTML - i've used that name whenever i need a transparent pixel ever since)

Code:
.about_nav .thislink
{
background-image:url('image.jpg');
}

.about_nav .thislink:hover
{
background-image:url('image2.jpg');
}
Really depends on how it's being used and what you want to archieve... though as said, lots of ways to do it...
 
Last edited:
Ah cool, I will give these a try. This is for a uni assignment, one of the criteria is valid XHTML and CSS so I have to play by the rules:)

Thanks again!
 
Being valid should be easy, whether it will work across browsers is the hard part...

Pretty sure the code i gave will validate, just not sure it would look the same in all browsers....

If you run your code or page through here..
http://validator.w3.org/

That should tell you specifically what doesn't validate and why....

If you get stuck or you have failed validations, post where you have problems...
 
I would do it like so....

Code:
<ul id="mainNav">
  <li id="about"><a href="#">About</a></li>
  <li id="players"><a href="#">Players</a></li>
  <li id="monsters"><a href="#">Monsters</a></li>
  <li id="maps"><a href="#">Maps</a></li>
</ul>


and then for the css (just typed this up now without testing so may need some tweaking)

Code:
ul#mainNav { margin: 0; padding: 0; list-style: none;}
ul#mainNav li { float: left; display: inline;}
ul#mainNav li a { float: left; display: block; width: 160px; height: 100px; text-indent: -9999px; outline: 0;}
ul#mainNav li#about a { background: url('about.jpg') no-repeat;}
ul#mainNav li#players a { background: url('players.jpg') no-repeat;}
ul#mainNav li#monsters a { background: url('monsters.jpg') no-repeat;}
ul#mainNav li#maps a { background: url('maps.jpg') no-repeat;}
ul#mainNav li a:hover { background-position: bottom left;}

and then for your images, combine both states into one image like so

about.jpg



What we're doing is giving each 'li a' element common styling and then applying a background image to each one individually. On hover, we just shift the image down. This means that the hover image doesn't need to be loaded on hover. We're also keeping the link text there but just indenting it off the page. Removing the outline of a link isn't generally recommended but I do when using this technique as otherwise, the outline leads off the page to where we've indented the text.
 
whatever you do, please do not use 1 image for the normal state and another separate one for the hover state. Really irritates me how I have to wait for a new image to load when I put my mouse over the navigation. Use the method mentioned above where both states are loaded at once and when the user moves their mouse over the link it all looks very streamlined :)
 
This is what i was getting at initially, you simly shift the background position for the link state.

Agreed with tsinc, better to use one image....

You can use 1 image for multiple classes, a quick example would be how google use theirs...

Very effective use of a single image..
http://www.google.co.uk/images/nav_logo4.png
 
Thanks again, Ill give this a crack 2morro. Really interesting seeing that google one, crazy concept I'd never thought of moving a single image position for multiple images; not just roll over!
 
Just if anybody is wondering, the technique you are all explaining is called css sprites. :)
 
Back
Top Bottom