
CSS Float Problems and How to Fix Them
12/10/2019
CSS Tools: Validation, Browser Testing, Fonts, and References
12/10/2019
The HTML tutorial covers the elements used to build forms. This lesson shows how to style those elements with CSS.
18. Styling forms with CSS
CSS can change the default appearance of a form so its fields, labels, and buttons fit the rest of the site.
Follow CSS best practices. Use descendant and type selectors where they make sense, and add classes when you need to distinguish groups of fields or limit a rule’s scope.
You can style form elements as you would other HTML elements. The following properties are particularly useful for sizing fields and making their states clear.
1. Box sizing
In the default CSS box model, declared dimensions apply to the content. Adding a border or padding increases the outer dimensions.
The box-sizing property changes which parts of the box the dimensions include:
- content-box: A 100px declared width gives the content 100px, and the border and padding make the overall box wider.
- border-box: A 100px declared width includes padding and borders. The content area shrinks to make room for them.
Try adding padding to an input set to width: 100%. Toggle box-sizing: border-box in the example and check when the field fits its container.
HTML:
<form> <label for="name">Name:</label> <input type="text" id="name" name="name"> <label for="email">Email:</label> <input type="email" id="email" name="email"> <input type="submit" value="Submit"> </form>
CSS:
input {
box-sizing: border-box;
padding: 10px;
width: 100%;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
form{max-width: 500px; margin: 0 auto;}
Try it in CodePen
Modern browsers support the unprefixed declaration. Older examples may include a vendor prefix.
box-sizing: border-box;
2. Resizing a text area
Browsers commonly allow users to resize a <textarea>. You can disable resizing with resize: none or restrict it to an axis.
Values: none, both, horizontal, and vertical.
| Values | Description |
none | The element cannot be resized |
horizontal | The element can be resized horizontally |
vertical | The element can be resized vertically |
both | The element can be resized in both directions |
Try it in CodePen
The example disables resizing. Try resize: vertical to let readers expand the writing area without changing the horizontal layout.
3. Styling inputs with :focus
Show a visible change when a field receives input focus. The indicator helps people see where their typing or selection will go.
The :focus pseudo-class matches when a field gains focus, such as when a person clicks it or reaches it with Tab. Here are several ways to style that state.
3.1. Replacing the default outline
Browsers generally show a focus outline. If you change how it looks, provide a replacement that remains visible against the surrounding colors.
input:focus {
outline: 2px solid #1754a3;
outline-offset: 2px;
}
3.2. Styling the background and border
Change the background or border to distinguish the focused field from the others.
input:focus {
background-color: #f0f0f0;
border: 1px solid #ccc;
}
3.3. Animations and transitions
A short CSS transition can soften a change in border color. The focus indicator should still appear clearly.
input {
transition: border-color 0.3s ease;
}
input:focus {
border-color: #007bff;
}
Try it in CodePen
The example combines a focus background, a border color, and a short transition.
.input-container {
margin-bottom: 20px;
}
input {
padding: 10px;
width: 100%;
border: 1px solid #ccc;
border-radius: 5px;
transition: border-color 0.3s ease; /* Transition the border color */
box-sizing: border-box;
}
input:focus {
background-color: #f0f0f0;
border-color: #007bff;
outline: 2px solid #1754a3;
outline-offset: 2px;
}
4. Styles for inputs
The next examples show several ways to customize the appearance of an input.
4.1. Basic style
Start with a border, a little rounding, and enough padding to make the field comfortable to read and use.
<input type="text" class="basic-input" placeholder="Name">
.basic-input {
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
width: 250px;
}
4.2. Rounded borders
A larger border radius creates a more rounded field. Balance the radius with the size of the input.
<input type="text" class="rounded-input" placeholder="Email address">
.rounded-input {
border: 2px solid #3498db;
border-radius: 20px;
padding: 12px;
width: 300px;
}
4.3. Shadows
A box-shadow gives a field more visual depth. Make sure the field boundary stays easy to see.
<input type="text" class="shaded-input" placeholder="Password">
.shaded-input {
background-color: #f2f2f2;
border: none;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
width: 280px;
}
4.4. Hover and focus
Use hover and focus styles to show when a field is interactive or ready for input.
<input type="text" class="hover-focus-input" placeholder="Phone number">
.hover-focus-input {
border: 1px solid #ccc;
padding: 10px;
transition: border-color 0.3s;
width: 250px;
}
.hover-focus-input:hover,
.hover-focus-input:focus {
border-color: #66afe9;
}
4.5. Icons in inputs
Icons can help distinguish fields. Add a visible label as well and treat an icon as decorative when it repeats the label.
<div class="input-container"> <i class="material-icons input-icon" aria-hidden="true">person</i> <input type="text" class="icon-input" placeholder="Name"> </div> <div class="input-container"> <i class="material-icons input-icon" aria-hidden="true">email</i> <input type="text" class="icon-input" placeholder="Email address"> </div> <div class="input-container"> <i class="material-icons input-icon" aria-hidden="true">lock</i> <input type="password" class="icon-input" placeholder="Password"> </div>
.input-container {
position: relative;
margin-bottom: 20px;
}
.icon-input {
padding: 10px 15px 10px 45px;
border: 2px solid #2196F3;
border-radius: 25px;
width: 300px;
font-size: 16px;
color: #333;
background-color: #f7f7f7;
outline: 2px solid #1754a3;
outline-offset: 2px;
}
.input-icon {
position: absolute;
top: 50%;
left: 15px;
transform: translateY(-50%);
color: #777;
font-size: 20px;
}
The example uses Google Material Icons. Include the Material Icons stylesheet in the HTML <head> to display them.
4.6. Animations in inputs
The search field expands slightly when it gains focus. Leave room for the wider field and check whether the layout still works on a narrow screen. Respect the user’s reduced-motion preference.
<input type="text" class="animated-input" placeholder="Search">
.animated-input {
padding: 10px;
width: 250px;
border: 1px solid #ccc;
transition: width 0.3s;
}
.animated-input:focus {
width: 300px;
}
5. Marking required inputs with an asterisk
An asterisk after a <label> can signal a required field. Explain what it means and add the HTML required attribute to the input so the browser can enforce the requirement.
The example uses the ::after pseudo-element and content to add an asterisk to labels with the class required.
label.required::after {
content: "*";
color: red;
margin-left: 5px;
}