StylizedWeb is a blog about web trends and tutorials, KnowHow community forum designed and maintained by Dejan Cancarevic who also runs Serbian IT job board at itPoslovi.info

Categories Archive

Popular Posts
Advertisement
Free Photoshop Tutorials Advertise on Stylized Web Najbolje ponuda IT poslova u Srbiji na itposlovi.info Advertise on Stylized Web
Make all content a link
add to Save to Delicious Save to Stumble Save to Digg
Digg this

Ever seen on some site whole content area with images etc as just one link? Usually there’s some hover for the background and they are mostly on the sidebars left or right or let’s say on the footer. You can see live example on previous version of my portfolio here - those links on the right. This tutorial will show you how to do that with CSS, and tell you something more about display:block; option.

Make all...

In this simple example I will go with <li> for it, because I think it’s used more

 
<ul>
<li>
<a href="#"><img src="someimage.gif" border="0" />
<strong>Some Headline</strong>
<span>Some text will go here</span>
 
…
 
</a>
</li>
</ul>
 

So just put everything in <a> tag, and it will all be a link of course but there will be many blank spaces for mouse and it won’t show a link for that, so let’s apply some CSS to it.

 
ul {
list-style:none;
}
 
li *{
display: block;
}
 
li a {
background: #fff;
height:100%;
}
 
li a:hover {
background:#ccc;
}
 
display: block;

display: block means that the element is displayed as a block, as paragraphs and headers have always been. A block has some whitespace above and below it and tolerates no HTML elements next to it, except when ordered otherwise (by adding a float declaration to another element, for instance).
And it works for: IE 6, IE 7, Firefox 2.0, Safari 3.0 Win, Opera 9.5b, iCab 3.0, Konqueror 3.5.7

UPDATE:
The code in this example was updated, h2 and p are replaced with strong and span to pass validation

What do you think?





12 Responses so far

By Kevin
on March 29, 2008

Sadly it doesn’t work with Firefox 3 (Beta 4)

By Kilburn
on January 31, 2008

@YGW

As you said, replacing the and tags some semantic meaning is lost. The replaced tags degrade worse and are less accessible (specially strong vs h2), so it should be seriously considered.

If you don’t want to sacrifice the page meaning, use a behavioral specification (yeah, javascript :P) to link the whole li and achieve any hover effect through the “:hover”:

<ul>
<li>
<h2><a href=”somehwere”>Some headline</a></h2>
<p>Some text…&lt/p>
</li>
</ul>

Notice the missing . It think it should be specified throught CSS’s background-image property (it has no meaning!).

By fadern
on January 17, 2008

Thanks

By milo
on January 9, 2008

Interesting note, here is the another approach on the block hover effect at smileycat

By Jermayn Parker
on January 9, 2008

I like this tip :)

I have been after something like this for a while and so thanks.

By Dejan
on January 8, 2008

@ Ywg
Yes, of course you are right, so let’s respect containment rules and put allowed tags into <a> i will make a correction

By Ywg
on January 8, 2008

@Dejan

Breaking containment rules is an extremly bad habit, even if in cases like that it can be convenient.

I regulary have to fix some co-workers bugs caused by innapropriate tag nesting. In your example browser aren’t too strict, but try to nest a list inside a paragraph and look at the DOM output : your list is rejected from your tag.
There’s no guaranty that upoming versions of browser won’t do the same thing with your code (as this is the correct behaviour), therefore you shouldn’t do that.

A more correct method would be to write your file like that :

<ul>
<li>
<a href=”#”>
<img src=”someimage.gif” border=”0″ />
<strong>Some Headline</strong>
<span>Some text will go here</span>
</a>
</li>
</ul>


li *{
display: block;
}

It may be less specific from a semantic view, but it’s correct and there’s no risk for it to be broked in the future.

By Dejan
on January 8, 2008

@ Laszlo
That’s correct, thanks for the note, I’ve forgotten to mention it in text ;)

@ Country
Yes you are right it can’t pass validation this way, but it works. If you want it that way, you’ll have to make all elements blok and place <a> inside all of them, but then you won’t have hover effect

By Laszlo
on January 8, 2008

The :hover pseudo class is ignored in IE 6 for all elements except <a>. (IE7 can apply :hover class to all elements in standards-compliant mode.) A better way to use the :hover for the <li> element, and a little js to emulate :hover in IE6.

By Country
on January 8, 2008

Thanks for this tips but it is completely incorrect.
The <a> tag is an inline element and it cannot contain some block elements (like H2, P, DIV, etc.).

  1. Jun 6, 2008: Ephedra
  2. May 29, 2008: Beats Programming Timbaland Style Javascript Tutorial J2ee