CSS Styling Forms


You can make an HTML form look much better by using CSS:

Try it Yourself »

Styling Input Fields

You can control how wide an input field is by using the width property.

Example

input {
  width: 100%;
}
Try it Yourself »

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

  • 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..

Padded Inputs

You can utilize the "padding" property to create some extra room within a text field.

Tip: When you have many inputs after each other, you might also want to add some margin, to add more space outside of them:

Example

input[type=text] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  box-sizing: border-box;
}
Try it Yourself »

Note that we have set the box-sizing property to border-box. This makes sure that the padding and eventually borders are included in the total width and height of the elements.


Bordered Inputs

You can modify the size and color of a border by using the "border" property. Additionally, you can create rounded corners for an element using the "border-radius" property.

Example

input[type=text] {
  border: 2px solid red;
  border-radius: 4px;
}
Try it Yourself »

If you just want to add a line at the bottom, use the border-bottom property with its existing width and height attributes.

Example

input[type=text] {
  border: none;
  border-bottom: 2px solid red;
}
Try it Yourself »

Colored Inputs

To give a color to the area behind your input, use the 'background-color' property. And if you want to change the color of the text, use the 'color' property.

Example

input[type=text] {
  background-color: #3CBC8D;
  color: white;
}
Try it Yourself »

Focused Inputs

In some web browsers, when you click on an input field, it may have a blue border around it. You can make this border go away by using this code: outline: none;.

You can apply changes to the input field when someone clicks on it by using the ":focus" selector in your HTML code.

Example

input[type=text]:focus {
  background-color: lightblue;
}
Try it Yourself »

Example

input[type=text]:focus {
  border: 3px solid #555;
}
Try it Yourself »

Input with icon/image

If you'd like to place an icon inside an input field, you can achieve this by using the 'background-image' property. You can control the icon's placement within the input using the 'background-position' property. It's important to mention that we include a significant amount of left padding to make room for the icon.

Example

input[type=text] {
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  padding-left: 40px;
}
Try it Yourself »

Animated Search Input

In this example, we're using the CSS property called "transition" to make the search input box change its width smoothly when someone clicks on it.

Example

input[type=text] {
  transition: width 0.4s ease-in-out;
}

input[type=text]:focus {
  width: 100%;
}
Try it Yourself »

Styling Textareas

Tip: Use the resize property to prevent textareas from being resized (disable the "grabber" in the bottom right corner):

Example

textarea {
  width: 100%;
  height: 150px;
  padding: 12px 20px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  background-color: #f8f8f8;
  resize: none;
}
Try it Yourself »

Styling Select Menus

Example

select {
  width: 100%;
  padding: 16px 20px;
  border: none;
  border-radius: 4px;
  background-color: #f1f1f1;
}
Try it Yourself »

Styling Input Buttons

Example

input[type=button], input[type=submit], input[type=reset] {
  background-color: #04AA6D;
  border: none;
  color: white;
  padding: 16px 32px;
  text-decoration: none;
  margin: 4px 2px;
  cursor: pointer;
}

/* Tip: use width: 100% for full-width buttons */
Try it Yourself »