John Smith
John Smith

Reputation: 2332

How can I select elements that have no class?

If my document looks like this (lots and lots more of those <i...> though without any particular order)

<i class="special">abc</i>
<i>def</i>
<i>xyz</i>
<i class="another">rfd</i>

The order of the elements in the example is also just arbitrary, e.g. there is no order to them.

I want a css selector that gives me only the <i> where no class is set (not even using javascript).

How'd I do that?

Edit: I have specified the question, since they do not come ordered.

Upvotes: 0

Views: 297

Answers (3)

Cogwizzle
Cogwizzle

Reputation: 550

John, I think the best way to select that element is to use the selector:

i.special + i:not(special):not(another), i.another + i:not(special):not(another)

The plus symbol gives you adjacent siblings to the initial element selected. The comma is a separator between the objects beings selected. I hope that this helps.

Upvotes: 1

castletheperson
castletheperson

Reputation: 33466

Use the :not() selector with an attribute selector to find elements that don't have a class.

i:not([class]) {
    ...
}

Upvotes: 2

Bartek Fryzowicz
Bartek Fryzowicz

Reputation: 6674

You can use nth-child selector:

i:nth-child(2)

Upvotes: 1

Related Questions