Skip to main content

Styling Form Input Fields

Most input types accept CSS styling, among them text fields, password fields, checkboxes, radio buttons, and file inputs. Input labels and form buttons can be styled too.

The properties you will reach for most often on input fields include width, padding, margin, border, border-radius, background-color, color, and font-size.


Style Input Width

To decide how wide an input field is, set its width property.

Tip: If you leave it alone, an HTML text input is 20 characters wide.

Example

In this example the input field is set to a width of 100%:

Prompt
Create an HTML page with a form containing a "First Name" label and a text input field. Use CSS to set the input width to 100%.
Output: complete HTML page

Attribute selectors

That rule reaches every <input> element. To target only one input type, switch to attribute selectors instead:

styles.css
input[type=text] - will only select text fields
input[type=password] - will only select password fields
input[type=number] - will only select number fields
etc..

Style Input Padding

If a text field needs some space inside it, padding is the property that adds it.

Tip: When input fields follow one another, a little margin can also give them extra room on the outside.

Example

Note: With box-sizing set to border-box, the padding and any borders count toward the total width and height of the elements. The CSS Box Sizing chapter explains this property in more detail.

Prompt
Create an HTML page with a form containing "First Name" and "Last Name" labeled text inputs. Use CSS to set width to 100%, padding to 12px, margin to 10px 0, and box-sizing to border-box on the text inputs.
Output: complete HTML page

Style Input Border

Two properties shape the edge of a field: border changes its size and color, and border-radius rounds the corners.

Rounded border

Prompt
Create an HTML page with a form containing a "First Name" labeled text input. Use CSS to give the text input a 2px solid red border and an 8px border-radius.
Output: complete HTML page

Bottom border only

For a border that appears on the bottom edge alone, reach for border-bottom:

Prompt
Create an HTML page with a form containing a "First Name" labeled text input. Use CSS to remove all borders from the text input and add only a 1px solid red bottom border.
Output: complete HTML page

Style Input Background Color and Color

An input takes its text color from the color property, while background-color adds a color behind it.

Example

Prompt
Create an HTML page with a form containing a text input field. Use CSS to set the text input background color to #3CBC8D and the text color to white.
Output: complete HTML page