Color field

A text field with a colour, and the picker beside it

The color of body text.

An empty text field sends no colour, and the picker shows none

A three digit value sets the picker too

disabled on both inputs dims the field

The form hears input when a colour is picked or typed, and sends the text field

#0a7f6e

With opacity

Add data-alpha to accept an opacity. The text may then be #rgba, #rrggbbaa or rgb() / rgba(), and the slider in the last span sets it. The picker shows the colour and its swatch fades with the opacity. The text is written as #rrggbb, or #rrggbbaa when the opacity is below 100%. A browser's colour dialog has no opacity of its own.

Type #rgba, #rrggbbaa or rgba(26, 115, 232, 0.5), or use the opacity slider.

API

The colour field has no methods and sends no events of its own: the text field sends the browser's own input and change, and they bubble. It starts every .color-field on the page, also one added later. A form reset puts the original text back and the picker follows.

Events

Event On Description
input the input[type=text], bubbles Sent when a colour is picked, as it is when you type in the text field.
change the input[type=text], bubbles Sent when the browser's colour dialog closes with a new colour, or when the text field loses focus after typing.

Keyboard

Key Description
Tab Moves from the picker to the text field. This is the browser's own.
Enter, Space On the picker, opens the browser's colour dialog. This is the browser's own.

Attributes

Attribute / element Values Description
.color-field on an .input-group Marks the group as a colour field. It holds a span with the picker, then the text field.
input[type=color] in the first span The browser's picker. Give it an aria-label; the text field has the visible label. Add disabled together with the text field.
input[type=text] value: #rgb, #rrggbb or empty; name The colour a form sends. The picker shows it. Empty sends no colour. Add placeholder to say what an empty field means.
data-alpha present or absent, on .color-field Accepts an opacity: the text may be #rgba, #rrggbbaa or rgb() / rgba(). Without it only #rgb and #rrggbb are colours. The text is written as #rrggbbaa when the opacity is below 100%.
input[type=range] class slider, in a last span, with data-alpha The opacity from 0 to 100. Give it an aria-label. Add disabled together with the other inputs.
data-empty present or absent, set by the script Present on the group while the text field is empty. The picker then shows no colour.
aria-invalid true On the text field: the group gets a red border, as in an input group.