Overview
CSS can be used to style the labels, fields, buttons, checkboxes, etc. in an HTML form.
Note
The following are basic examples. Towards the bottom of this page you’ll find a complete list of all applicable properties, where you can find more information on the properties discussed, and sometimes find more involved properties not discussed on this page.
Using CSS, each element of the form can be styled. For instance, in the example below we wrap the entire form in a thin border, make sure that each input falls on its own line by making the width 100%, we are giving each input a set padding and margin, applies a border and border radius, and styles the submit button.
It also changes the border color of an input and textarea when the user clicks into them.
form {
padding: 8px;
border: #eaeaea solid thin;
}
input[type="text"], input[type="email"] {
width: 100%;
padding: 10px 8px;
margin: 8px;
display: inline-block;
border: #bababa solid thin;
border-radius: 4px;
}
textarea {
width: 100%;
height: 80px;
border: #bababa solid thin;
}
input:focus, textarea:focus {
border-color: blue;
}
input[type="submit"] {
background-color: #ff0000;
border: none;
color: white;
padding: 8px 26px;
text-decoration: none;
cursor: pointer;
}
This is just an example of a few of the elements that can be styled and a few of the properties typically applied.
Any element in a form, including the form element itself can be styled to give the form an appealing look and to make it easier to use.
Selectors to Style Form Elements
For the most part, to style the form elements, you just target the various tags used to make up the form (form, label, input, textarea, select, option, etc.). But the following selectors can be added to style a form element based on the state of the element or things like whether its required, disabled, checked, etc.
- CSS – :checked SelectorThe :checked selector matches every checked input element for radio buttons, checkboxes, and option elements.
- CSS – :default SelectorThe :default selector selects the default form element in a group of related elements, and can only be used on button, input type=”checkbox”, input type=”radio”, and option elements.
- CSS – :disabled SelectorThe :disabled selector matches all disabled form elements.
- CSS – :enabled SelectorThe :enabled selector matches all enabled form elements.
- CSS – :focus SelectorThe :focus selector selects the element that currently has focus.
- CSS – :in-range SelectorThe :in-range selector selects elements with a value that is within a specified range, and only works for input elements with min/max attributes.
- CSS – :indeterminate SelectorThe :indeterminate selector selects form elements that are in an indeterminate state, and can only be used on input type=”checkbox”, input type=”radio”, and progress elements.
- CSS – :invalid SelectorThe :invalid selector selects form elements with a value that does not validate according to the element’s settings.
- CSS – :optional SelectorThe :optional selector selects form elements which are optional (those without the required attribute).
- CSS – :out-of-range SelectorThe :out-of-range selector selects elements with a value that is NOT within a specified range, and only works for input elements with min/max attributes.
- CSS – ::placeholder SelectorThe ::placeholder selector selects form inputs with placeholder text.
- CSS – :read-only SelectorThe :read-only selector selects form inputs which are “readonly” (those with a “readonly” attribute).
- CSS – :read-write SelectorThe :read-write selector selects form inputs which are “readable” and “writeable” (those with no “readonly” or “disabled” attributes).
- CSS – :required SelectorThe :required selector selects form inputs which are required (those with the required attribute).
- CSS – :valid SelectorThe :valid selector selects form inputs with a value that properly validates according to the element’s settings.
CSS Notes:
- The “inherit”, “initial” and “unset” keywords can be used with any CSS property to set its value
- In CSS there are many ways to express a color value in a property
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.