views:

33

answers:

1

For a site I'm working on, I'd like to have a dotted outline appear around links when they are focused/hovered/active. I'd like this to happen for text and image links.

The issue I have is that whilst my code works great in Firefox and IE, in Chrome 7.0.517.41 the dotted outline is the same height as my text, not the height of the image.

Sample code:

<!DOCTYPE html> 
<html lang="en"> 
    <head>
        <style type="text/css">
            BODY { font: 15px/1.5 sans-serif; }
            a:focus, a:hover, a:active { outline: 1px dotted #11aa22; }
        </style>
    </head>
    <body>
        <a href="#">
            <img src="http://sstatic.net/stackoverflow/Img/wmd-buttons.png" />
        </a>
    </body>
</html>

This is annoying. As a workaround I use javascript to apply a class to distinguish anchors containing images, and ensure that the outline for anchors containing images is applied to the anchor, not the image:

a:focus, a:hover, a:active { outline: 1px dotted #11aa22; }
a:focus.img, a:hover.img, a:active.img { outline: none; }
a:focus img, a:hover img, a:active img { outline: 1px dotted #11aa22; }

And in my document ready

$('a:has(img)').addClass('img');

Is there a better way to do this?

+1  A: 

You have to set links as blocks for they wrap images, as

a { display:inline-block; }

eg.

MatTheCat
this can affect the layout is ways you don't want
Moin Zaman
So why is this only different in Chrome? ... also, did you mean to put something in your answer after eg. :-)
SamStephens
@Moin, yeah, I'm not keen on changing the display style of my a
SamStephens
inline-block won't change the layout.
MatTheCat
I agree with MatTheCat. I use `display: block;` to have an `a` tag cover the entire width and height of its container all the time.
ClarkeyBoy
Contrariwise `display: block;` will cause links take all width available. You can avoid this using `float`which display elements as blocks but be careful with layout too ^^
MatTheCat
Yeah I actually want them to take all width, and height... I use them in a menu made up of a `ul` and lots of `li` tags. I want the user to be able to click anywhere within them to go to the URL in the `a` tag.
ClarkeyBoy
So you're right ^^
MatTheCat
Did a little research and tried this in a few browsers, and it works for me. Thanks Mat. @Moin: I'd be keen to know exactly what you mean, I couldn't see any side effects.
SamStephens
I'll second that - I dont know of any side effects that this technique could have on anything else... It may well be that it has big side effects in IE5 or something - but lets be honest... who actually cares about anything prior to IE7 anyway.
ClarkeyBoy
My targets are pretty much IE7/8, Chrome latest, Firefox 3.5, Safari latest. It's a personal project, and I just don't have the time to test every browser.
SamStephens
This prevents links containing text from wordwrapping, it's pretty nasty. I've gone back to the workaround I detailed in the question.
SamStephens