Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Every Form Element Except Buttons with CSS3

Is there a way with CSS3 to select every input-related element that does not have a type attribute of button, reset, submit, or a tag of button? This is what I've gotten so far:

input:not([type="submit"]),
textarea {

    /* ... */

}

input:not([type="submit"]):focus,
textarea:focus {

    /* ... */

}

input:not([type="submit"]):hover,
textarea:hover {

    /* ... */

}

... but it's not as universal as it could be. I'm wanting to apply some nice new transitions to solely input-related fields. If these certain styles get applied to any buttons on the page, it begins to look strange. Any help is appreciated.

like image 369
Gabriel Ryan Nahmias Avatar asked May 31 '12 08:05

Gabriel Ryan Nahmias


People also ask

How do I apply CSS to all elements except one?

Approach: Use the :not(selector), also known as negation pseudo-class which takes a simple selector as an argument and allows you to style all the elements except the element specified by the selector.

How do you exclude an element in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

Can I use CSS not selector?

The :not() property in CSS is a negation pseudo class and accepts a simple selector or a selector list as an argument. It matches an element that is not represented by the argument. The passed argument may not contain additional selectors or any pseudo-element selectors.


1 Answers

Simply chain multiple :not selectors together:

/* Fields */
input:not([type=button]):not([type=reset]):not([type=submit]):not([type=image]),
textarea {
  outline: 3px solid green;
}

/* Buttons */
input[type=button],
input[type=reset],
input[type=submit],
input[type=image],
button {
  outline: 3px solid red;
}

/* Both */
input,
button,
textarea {
  margin: 6px;
}
<h1>Form elements</h1>
<p>Buttons should be outlined in red, fields in green.</p>
<button>Button</button>
<input type="button" value="Input Button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image" src="//placehold.it/100x40/ffff00/0000ff/?text=foo">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
<textarea></textarea>
like image 158
0b10011 Avatar answered Sep 22 '22 15:09

0b10011