CSS Forms

CSS Styling Forms

CSS is used to style HTML forms. The look of an HTML form can be greatly improved with CSS:

     

Example:

Result:

Styling Form Input Fields

With CSS, you can style most of the different input types, like text fields, password fields, checkboxes, radio buttons, and file inputs. You can also style input labels and form buttons.

Some commonly used CSS properties for styling input fields, are:

CSS Form Inputs

Styling Form Input Fields

With CSS, you can style most of the different input types, like text fields, password fields, checkboxes, radio buttons, and file inputs. You can also style input labels and form buttons.

Some commonly used CSS properties for styling input fields, are:


Style Input Width

The width property is used to set the width of an input field.

Tip: The default width of an HTML input text field, is 20 characters.

Here we set the width to 100%:

Example:

Result:

The example above applies to all <input> elements. If you only want to style a specific input type, you can use attribute selectors:

Style Input Padding

The padding property is used to add some space inside the text field.

Tip: When you have several input fields after each other, you might also want to add some margin, to add more space around them:

  

Example:

Result:

CSS Form Focus and Icons

Style Input with Focus

By default, some browsers will add a blue outline around the input when it gets focus (clicked on). You can remove this behavior by adding outline: none; to the input.

Use the :focus selector to do something with the input field when it gets focus:

Example:

Result:

Style Input with icon/image

If you want an icon inside the input, use the background-image property and position it with the background-position property. Also notice that we add a large left padding to reserve the space of the icon:

Example:

Result:

Animated Search Input

In this example we use the CSS transition property to animate the width of the search input when it gets focus. You will learn more about the transition property later, in our CSS Transitions chapter.

Example:

Result:

Style Textarea

By default, a <textarea> can be resized with a “grabber” in the bottom right corner. To remove the grabber, set the resize property to none:

Example:

Result:

Style a Dropdown Menu

Example:

Result: