Ky -
Ky -

Reputation: 32073

Is it possible to select all elements which do not have any CSS class?

The W3C Recommendations defines the ability to select any anchor which has a defined class by using A[CLASS]{/*Code*/}, but is there an antecedent?

Can I select all anchors that do NOT have a class?

My instinct is to use A[~CLASS]{/*Code*/}, but that isn't documented, and A[CLASS=]{/*Code*/} selects all anchors which have a class name which is an empty string (for instance, <A CLASS="" HREF="http://example.com/">This anchor has a class, but it's empty.</A>).

Example usage


In this code, I only want classless links to be green.

<HTML>
<HEAD>
<LINK REL="stylesheet" TYPE="text/css" HREF="http://example.com/externalUneditableStyles.CSS" />
<STYLE>
A.deadLink{
color:#FF0000 !important;
}
A{
color:#00FF00;
}

</STYLE>
</HEAD>

<BODY>
<A HREF="http://fakesite.not/" CLASS="deadLink">This link is red, as specified by the CSS above.</A>
<A HREF="http://example.com/">This link is green, as specified by the CSS above.</A>
<A HREF="#/childpage/" CLASS="childLink">This is a link to a child page, and is styled by external uneditable CSS. That CSS doesn't specify a color, so it is green, even though that might not be the intended effect.</A>
</BODY>
</HTML>

Upvotes: 1

Views: 837

Answers (3)

harrypujols
harrypujols

Reputation: 2284

Try to play with the .foo:not(.bar)-pseudo selector. I also advise you not to code tags in caps. I believe it is allowed in HTML5, but not in XHTML. Even so, the practice is frowned upon.

Upvotes: 0

Dario Novoa
Dario Novoa

Reputation: 115

That's exactly what cascading is for, if you do:

a { color: #000; } /* this targets all anchors */

Then after this in your stylesheet you can do

a.classname { color: #ffcc00; } /* to override the color for the once with the classname class defined. */

I hope that's clarifies your question.

Upvotes: 0

Chris
Chris

Reputation: 26878

Use something like this:

a:not([class]) {
    /*some styling here*/
}

Little demo: little link.

Upvotes: 8

Related Questions