CSS Pseudo Classes for Form Elements

April 28, 2014 | 6 Minute Read

CSS provides us with a handful of pseudo classes that are specific to HTML5 forms. They allow us to create forms that are easier to fill out by highlighting fields that are required, optional, valid, invalid, etc.

Pseudo Classes for Forms

The following pseudo classes are all related to form elements with examples on how they are used.

:default

The :default pseudo class applies to the default element in a group of similar elements within a given form. For example, if you have a form that has a submit and cancel button you could select the default button using :default.

See the Pen gsilz by Brian (@brianmtreese) on CodePen.

:default is supported in

Chrome 10 and above Fire Fox 4 and above Opera 10 and above Safari 5 and above

:required

The :required pseudo class applies to any input with the required attribute set within a given form. This can be very useful to highlight the required fields within a form.

See the Pen dynGK by Brian (@brianmtreese) on CodePen.

:required is supported in

ie10 and above Chrome 10 and above Fire Fox 4 and above Opera 10 and above Safari 5 and above

:optional

The :optional pseudo class applies to any input without the required attribute set within a given form. May or may not be useful since most of the time simply highlighting the required fields will be enough.

See the Pen vCgpo by Brian (@brianmtreese) on CodePen.

:optional is supported in

ie10 and above Chrome 10 and above Fire Fox 4 and above Opera 10 and above Safari 5 and above

:valid

The :valid pseudo class applies to any input that contains valid data within a given form. This can be very useful as it will provide the user with feedback as they are filling out the form.

See the Pen JcFhe by Brian (@brianmtreese) on CodePen.

:valid is supported in

ie10 and above Chrome 10 and above Fire Fox 4 and above Opera 10 and above Safari 5 and above

:invalid

The :invalid pseudo class applies to any input that contains invalid data within a given form. This can be very useful as it will provide the user with feedback as they are filling out the form.

See the Pen ItkGn by Brian (@brianmtreese) on CodePen.

:invalid is supported in

ie10 and above Chrome 10 and above Fire Fox 4 and above Opera 10 and above Safari 5 and above

:in-range

The :in-range pseudo class applies to any form element that contains a value within the specified range for the input within a given form. Similar to valid/invalid it can be useful for providing feedback to the user when filling out the form.

See the Pen fckHB by Brian (@brianmtreese) on CodePen.

:in-range is supported in

Chrome 10 and above Fire Fox 28 and above Opera 11 and above Safari 5.2 and above

:out-of-range

The :out-of-range pseudo class applies to any form element that contains a value outside the specified range for the input within a given form. Similar to valid/invalid it can be useful for providing feedback to the user when filling out the form.

See the Pen yirbF by Brian (@brianmtreese) on CodePen.

:out-of-range is supported in

Chrome 10 and above Fire Fox 28 and above Opera 11 and above Safari 5.2 and above

:read-only

The :read-only pseudo class applies to any input with the readonly attribute set on it in a given form. It can be useful to gray out disabled fields for forms.

See the Pen BodAt by Brian (@brianmtreese) on CodePen.

:read-only is supported in

Chrome Fire Fox Safari

:read-write

The :read-write pseudo class applies to any input without the readonly attribute set on it in a given form.

See the Pen GmAwz by Brian (@brianmtreese) on CodePen.

:read-write is supported in

Chrome Fire Fox Safari

Client Side Only

The important thing to remember when using CSS and HTML validation to style your forms is that they are only client side validation. You will still need to validate on the server as well. The role that CSS and these pseudo classes play is to simply enhance the form and make it easier for users to fill out.


Sources