views:

220

answers:

4

On my page, I have custom styled hyperlinks, and I also have alot of hyperlinked images. I don't want these custom styles to appear on the hyperlinks that only contain images. Instead of adding a separate class (i.e "nostyle") to each hyperlinked image, can I somehow target the hyperlinked images from my stylesheet?

Thanks.

+1  A: 

sure, just use

a img {
  // your style here...
}

if you want to target only the images within a certain class of links, use

a.yourclass img {}
LordOfThePigs
This achieves the OP's goal, but doesn't actually do what the title asks. It targets the image inside a link, but doesn't target the link that happens to contain an image.
Eddie
This targets any <img> element that is a descendant of an <a> element. This does not target <a> elements which have an <img> child element.
Zack Mulgrew
well, then I suggest the OP rephrases his question or title. I don't see why my post should be voted down for answering the question.
LordOfThePigs
I downvoted the answer because it is not the correct answer. The correct answer is: you cannot do what the OP is asking. However, the last part of the question is misleading so I un-downvoted.
Zack Mulgrew
I think "I don't want these custom styles to appear on the hyperlinks that only contain images" is clear. His problem is with the links, not with the images.
AmbroseChapel
+5  A: 

You cannot select the parent of a matched item in CSS directly. There are workarounds with js (e.g. Searching elements and applying class attributes to their parent nodes) but seems a bit clumsy. You would rather refactor your document structure to find out a slicker solution.

Theo.T
Typo alert: You wrote "clumpsy" rather than "clumsy"
Eddie
I blame it on clumpsiness :p thx
Theo.T
A: 

Based on what it sounds like you're asking for the answer is no. You can't go backwards in the CSS only forwards.

As Theo.T mentioned, you could have a JS work-around.

One idea is to do an element innerHTML check to see if the element has an <img> tag inside it and if it does, change the element.className = "nostyle"; but that's a messy workaround and by the time you get the syntax right in JS (and cross-browser) you could have re-factored your document.

jerebear
A: 

no need for a anchor class.

a[rel="image"] img {}
this requires you to add "rel" attribs to all the necessary links, so you might as well add a class. Attribute selectors don't even work in IE anyway.
nickf