XML technologies / CSS / CSS Cookbook / Forms / Different styles for different input elements in one form

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

The input elements (input) in one form should have different styles.

Solution

Use two or more class selectors to assign the different styles.

First, give the input elements class attributes:

<label for="fmname">Name</label>
<input type="text" name="fmname" class="fmname" />
<label for="fmemail">Email</label>
<input type="text" name="fmemail" class="fmemail" />

You can then target the input elements in the stylesheet using the value of class:

.fmname {
 text-align: left;
}
.fmemail {
 text-align: center;
}

Discussion

Using class attributes to assign the same styles to several elements on a page works in most browsers.

Another way to assign custom styles to particular elements is available in browsers that already understand the CSS 3 specification for attribute selectors, as discussed in the recipe Adjusting link colours.

See also

The recipe Styling buttons on defining different styles for different buttons in one form.

  

 

 

 

back to top