views:

87

answers:

2

Hi Guys (and Gals, if they exist in programming),

If i have a HTML form with the following inputs:

<input type="text" />
<input type="password" />
<input type="checkbox" />

I want to apply a style to all input's that are either type="text" or type="password".

Alternatively i would settle for all input's where type != "checkbox".

Seems i like to have to do this:

input[type='text'], input[type='password']
{
   // my css
}

Isn't there a way to do:

input[type='text',type='password']
{
   // my css
}

or

input[type!='checkbox']
{
   // my css
}

Had a look around, and it doesn't seem like there is a way to do this with a single CSS selector.

Not a big deal of course, but im just a curious cat.

Any ideas?

+1  A: 
input[type='text'], input[type='password']
{
   // my css
}

That is the correct way to do it. Sadly CSS is not a programming language.

codemonkeh
Oh well. CSS4 perhaps? =)
RPM1984
+1  A: 

CSS3 has a pseudo-class called :not()

input:not([type='checkbox'])

Here's a complete example.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;

<html>
<head>
    <style type="text/css" media="screen">
        input:not([type='checkbox']) {    
            visibility: hidden;
        }  

    </style>
</head>
<body>                  

    <p>If <code>:not()</code> is supported, you'll only see the checkbox.</p>

    <p>
    <input type="text" />
    <input type="password" />
    <input type="checkbox" />
  </p>
</body>
</html>

If you want something that's widely supported now, here's what I would do.

input {
   // styles for most inputs
}   

input[type=checkbox] {
  // revert back to the original style
} 

That's technically two selectors, but it saves you from having to think about all of the input types that aren't "checkbox."

Patrick McElhaney
nice one! thanks. is that CSS3 selector fully supported? (i only really care about IE7+, FF3+, Safari recent, Chrome recent)
RPM1984
@your edit. Agreed, and i like it. Thanks.
RPM1984