tags:

views:

67

answers:

3

I want to make all text links at my website have a bottom border. I use:

a
{
    border-bottom: 1px dotted #333;
}

... but it adds a border to image links too and I don't want that.

How do I make it work for text links only?

A: 

Add this:

img {
    border: none;
}

That will get rid of borders on images.

Mike Caron
From discussion in this thread it's clear that this answer retains the border under `A`, hence it looks like `IMG` has a border (even if `IMG` itself has no border), so it doesn't work. I think, you should edit or delete your answer.
Peter Ajtai
Oh, I understand the problem now. The OP didn't state it very clearly. The problem is that the image has a text-height sized box running through it. That's a tricky one.
Mike Caron
+5  A: 
a { border-bottom:1px dotted #333; }
a img { border:0; }

Just override the inherited rule, the native css way.

Edit: Wow, I'm really not paying attention. Can you just throw a class to anchors that include images?

a.contains-image { border:0; }

This would be the only non-scripting solution without relying on CSS3's not selector.

meder
I already tried that but it doesn't work. I believe the "a img" selector manipulates the border associated with the "img" tag and not the border associated to the "a" tag that goes around it.
Emanuil
4 votes without further testing? Oo It doesn't work the way intended: your first rule will add a border on links, your second one will remove border from images (the one that is blue by default) and will do nothing to the border of its parent, the link. EDIT: @Emanuil you beat me of 17s :)
Felipe Alsacreations
@Felipe Alsacreations: Exactly!
Emanuil
A: 

Adding a border on links and removing it if it contains some img isn't possible in CSS, with or without :not()
You can't select an element depending on its descendants (CSS isn't XPath) and you can't affect parents styling.

I'd use jQuery selectors (Sizzle to be precise) and add a class depending on the presence of an image:

<style type="text/css">
  .underline { border: 4px dotted #333; }
  a img { border:0; /* remove the default blue border from images within links */}
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
  $(document).ready(function() {
    $('a:not(:has(img))').addClass('underline');
  });

If you've not only 100% text and a>img:only-child links but also text+img mixed in links, you could also target img:only-child or wrap text in links (except img) in span elements to style mixed links in a certain way, if you've these edge cases in your page.

Felipe Alsacreations
Doing important CSS with javascript is such a bad idea. To begin with, people using noScript (no js) will not see the styling you want.
Peter Ajtai
@Peter Ajtai Whether or not this is "important CSS" depends on the design of the website and how it's styled by default (without JS). The only certain thing is it isn't possible to answer the question only with CSS.
Felipe Alsacreations
@Felipe - Meder's class anchor solution works nicely.
Peter Ajtai