1

I defined a bottom border (2px) on my anchor links.

The problem: this bottom border is also applied on my image when these are links.

How to avoid this bottom border on images?

Html:

<a href="#">hello</a>
<a href="#">
    <img src="https://cdn0.iconfinder.com/data/icons/cosmo-medicine/40/dog-128.png">
</a>

Css:

a {
    text-decoration: none;
    color: #3e3e3e;
    border-bottom: solid 2px #60bbd2;
}

a img {
    border: 0;
}

a:hover{
    border-bottom: 0px none;
    color: #60bbd2;
}

Jsfiddle: http://jsfiddle.net/8L63n7nu/1/

Thanks.

Bronzato
  • 9,152
  • 21
  • 106
  • 196

2 Answers2

2

The border is not on the img, it's on the a, which is why a img {border: 0;} changes nothing. CSS unfortunetly doesn't have a :parent or :contains-selector which you could use to check if the a contains an image, so you'll have to separate links that do somehow. I suggest giving the links a class, for instance like this:

<a href="#" class="image">
    <img src="https://cdn0.iconfinder.com/data/icons/cosmo-medicine/40/dog-128.png">
</a>

And add rule:

a.image{border:none}

...or...

If you don't want to add classes, you'll have to use JS. jQuery has got the :has() selector, which you can use to check if the element contains specified elements. If you wrap this in a jQuery wrapper, it's run when the page loads and works just like css would.

$(function(){
    $("a:has(img)").css({border:"none"});
});

Be aware, that if you add more links without refreshing the page - for instance with ajax - you'll have to run the above function again to process the new links.

Here's also a working fiddle.

Okku
  • 6,358
  • 3
  • 25
  • 43
1

Add a class to the anchors with the image in them

HTML:

<a href="#" class="img">
    <img src="https://cdn0.iconfinder.com/data/icons/cosmo-medicine/40/dog-128.png">
</a>

CSS:

a {
    text-decoration: none;
    color: #3e3e3e;
    border-bottom: solid 2px #60bbd2;
}

a.img {
    border: 0;
}

a:hover{
    border-bottom: 0px none;
    color: #60bbd2;
}

http://jsfiddle.net/8L63n7nu/14/

Demnogonis
  • 2,981
  • 5
  • 28
  • 44