css selector to match an element without attribute x

Stephen Sorensen picture Stephen Sorensen · Oct 7, 2009 · Viewed 142k times · Source

I'm working on a CSS file and find the need to style text input boxes, however, I'm running into problems. I need a simple declaration that matches all these elements:

<input />
<input type='text' />
<input type='password' />

... but doesn't match these ones:

<input type='submit' />
<input type='button' />
<input type='image' />
<input type='file' />
<input type='checkbox' />
<input type='radio' />
<input type='reset' />

Here's what I would like to do:

input[!type], input[type='text'], input[type='password'] {
   /* styles here */
}

In the above CSS, notice the first selector is input[!type]. What I mean by this is I want to select all input boxes where the type attribute is not specified (because it defaults to text but input[type='text'] doesn't match it). Unfortunately, there is no such selector in the CSS3 spec that I could find.

Does anyone know of a way to accomplish this?

Answer

eveliotc picture eveliotc · Oct 7, 2009

:not selector:

input:not([type]), input[type='text'], input[type='password'] {
    /* style here */
}

Support: in Internet Explorer 9 and higher