views:

188

answers:

5

On my web site, I have the following CSS:

a:hover{color:red;border-bottom:1px solid}

This displays a red underline on text links when hovered, which is desired.

However, this also displays a red underline on image links when hovered, which is NOT desired.

I only want to display the red underline on text links when hovered but not image links when hovered.

Any ideas how I could accomplish this with CSS?

+1  A: 

Because the style is probably applies to the anchor elements and its applying the style to the image links as well

a { border-bottom:1px solid red}

would also apply to

<a href="somelink"> <img src="someimage.jpg"> </a>
Rishav Rastogi
Soooo ... would you mind helping me figure out a way using CSS to not have this happen? Please
Taser
A: 

Seeing as the :hover text colour is almost red, why not just use text-decoration: underline?

Failing that, either have a textlink class and set the border property, a.textlink { border-bottom: 1px solid red }, or have an imglink class and clear the border property, a.imglink { border-bottom: none; }

Mike Anchor
+3  A: 

You are styling the links with

border-bottom:1px solid red

It might be better to instead use

color: red;
text-decoration: underline;

as images cannot be underlined.

Dave Hinton
Note that this'll change the text colour as well as the underline, which is the most common reason I've seen borders used instead.
ceejayoz
Surely you could assign a different class to links which have just images in? .class1, .class2{ /*something*/ }.class1:hover{border-bottom:1px solid red}.class2:hover{border-bottom:0px;}
JonB
A: 

You have two options.

The first option is to do a { color: red; text-decoration: underline; }. This has the downside of changing the text to a red colour, not just the line. If that's acceptable, this is the easiest way.

If you need the text to stay whatever colour it normally is, things are a bit more complicated. There's no way with CSS to say "apply this rule unless there's an image tag within the link", so you're somewhat stuck. What I've always done is manually (or in your CMS, if it's something automatic) add a class to links that contain images - <a href="#" class="imagelink"><img src="#" alt="" /></a> - and remove the border from those.

Also note that you'll want to set a one pixel transparent border on non-hovered links, or the page'll jump around a little due to one pixel being added/subtracted to the height of the line containing the link.

ceejayoz
A: 

I'm pretty sure that this cannot be done with CSS alone (I'm thinking that maybe some CSS pseudo-selectors can do the trick, only if we are talking about thumbnail images linking to their larger versions) but if you are already using jQuery in your page, it may be done with pretty much one line of code:

$('a img').each(function(){$(this.parentNode).addClass('image-link')});

and do not forget to add the correspondent class in your CSS:

a.image-link{ border:none;}

And that's it.

Soska