XML technologies / CSS / CSS Cookbook / Forms / Design example: a login form
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Login forms are everywhere: webmail, Amazon book orders and even paying parking tickets online.
Users see only a few parts: submit button, username and password fields and their labels. Here is the markup to style; the figure shows the unstyled form.
<form action="login.php" method="post">
<label for="uname">Benutzername</label>
<input type="text" name="uname" id="uname" value="" />
<br />
<label for="pword">Passwort</label>
<input type="text" name="pword" id="pword" value="" />
<br />
<input type="submit" name="Submit" value="Abschicken" />
</form>Figure: Unstyled login form.
The pseudo-element :after creates the character inserted after the element label’s normal text:
label:after {
content: ": ";
}Distinguish labels from fields using a CSS rule giving a gray background and heavier black text (see the next figure):
label {
background-color: gray;
color: black;
font-weight: bold;
}Figure: Labels with custom colors.
Make text uppercase and add padding (see the next figure):
label {
background-color: gray;
color: black;
font-weight: bold;
padding: 4px;
text-transform: uppercase;
}Figure: Uppercase text.
Reduce label size so it does not distract from fields, and use Verdana, a sans-serif font readable at small sizes (see the figure):
label {
background-color: gray;
color: black;
font-weight: bold;
padding: 4px;
text-transform: uppercase;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: xx-small;
}Figure: Finished text in the label element.
Now style the fields. Distinguish their two types by adding an attribute class on the submit button, allowing separate styles. Attribute selectors could do this too, but Internet Explorer 5.x and 6 for Windows do not support them. Class selectors change one element’s properties without affecting others:
<input type="submit" name="submit" value="Abschicken" class="buttonSubmit" />To separate elements more, define fields input as block elements with bottom margins (see the figure):
input {
display: block;
margin-bottom: 1.25em;
}Figure: Fields now appear below labels.
Set fields to 150 pixels wide with a one-pixel border, replacing most browsers’ default recessed appearance. Give right and bottom borders two pixels for depth (see the next figure):
input {
display: block;
margin-bottom: 1.25em;
width: 150px;
border: solid black;
border-width: 1px 2px 2px 1px;
}Figure: Modified fields.
Having styled the fields input, style the submit button with its own class selector.
Set button width to 100 pixels, two-thirds of the fields. A right margin shifts it 50 pixels right to align with them (see the next figure):
.buttonSubmit {
width: 100px;
margin-left: 50px;
}Figure: Adjusted submit button.
Give the button a green color and border. The property text-transform makes the label uppercase (see the next figure):
.buttonSubmit {
width: 100px;
margin-left: 50px;
color: green;
text-transform: uppercase;
border: 1px solid green;
}Figure: Green button with an uppercase label.
Finish by hiding elements br that add whitespace. The next figure shows the result.
br {
display: none;
}Figure: The finished login form.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top