Skip to main content Skip to docs navigation

Controls

Learn how to implement form controls with CSS in web development and style form controls to match site styles.

On this page

Basic native form controls

In the previous article , we marked up a functional web form example, introducing some form controls and common structural elements, and focusing on accessibility best practices. Next, we will look at the functionality of the different form controls, or widgets, in detail — studying all the different options available to collect different types of data. In this particular article, we will look at the original set of form controls, available in all browsers since the early days of the web.

Prerequisites: A basic understanding of HTML .
Objective: To understand in detail the original set of native form widgets available in browsers for collecting data, and how to implement them using HTML.

You've already met some form elements, including <form > , <fieldset > , <legend > , <textarea > , <label > , <button > , and <input > . This article covers:

Note: We cover additional, more powerful form controls in the next two articles. If you want a more advanced reference, you should consult our HTML forms element reference , and in particular our extensive <input > types reference.

Text input fields

Text <input > fields are the most basic form widgets. They are a very convenient way to let the user enter any kind of data, and we've already seen a few simple examples.

Note: HTML form text fields are simple plain text input controls. This means that you cannot use them to perform rich text editing (bold, italic, etc.). All rich text editors you'll encounter are custom widgets created with HTML, CSS, and JavaScript.

All basic text controls share some common behaviors:

  • They can be marked as readonly (the user cannot modify the input value but it is still sent with the rest of the form data) or disabled (the input value can't be modified and is never sent with the rest of the form data).
  • They can have a placeholder ; this is the text that appears inside the text input box that should be used to briefly describe the purpose of the box.
  • They can be constrained in size (the physical size of the box) and maxlength (the maximum number of characters that can be entered into the box).
  • They can benefit from spell checking (using the spellcheck attribute), if the browser supports it.

Note: The <input > element is unique amongst HTML elements because it can take many forms depending on its type attribute value. It is used for creating most types of form widgets including single line text fields, time and date controls, controls without text input like checkboxes, radio buttons, and color pickers, and buttons.

Single line text fields

A single line text field is created using an <input > element whose type attribute value is set to text , or by omitting the type attribute altogether (text is the default value). The value text for this attribute is also the fallback value if the value you specify for the type attribute is unknown by the browser (for example if you specify type="color" and the browser doesn't support native color pickers).

Note: You can find examples of all the single line text field types on GitHub at single-line-text-fields.html (see it live also ).

Here is a basic single line text field example:

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    comment"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    comment"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    I'm a text field"
                                                
                                                />
                                            
                                        
                                    

Single line text fields have only one true constraint: if you type text with line breaks, the browser removes those line breaks before sending the data to the server.

The screenshot below shows a text input in default, focused, and disabled states. Most browsers indicate the focused state using a focus ring around the control and the disabled state using grey text or a faded/semi-opaque control.

Screenshot of the default, focused and disabled states text input in Chrome on macOS

The screenshots used in this document were taken in the Chrome browser on macOS. There may be minor variations in these fields/buttons across different browsers, but the basic highlighting technique remains similar.

Note: We discuss values for the type attribute that enforce specific validation constraints including color, email, and url input types, in the next article, The HTML5 input types .

Password field

One of the original input types was the password text field type:

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    password"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    pwd"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    pwd"
                                                
                                                />
                                            
                                        
                                    

The following screenshot shows Password input field in which each input character is shown as a dot.

Password field in chrome 115 on macOS

The password value doesn't add any special constraints to the entered text, but it does obscure the value entered into the field (e.g. with dots or asterisks) so it can't be easily read by others.

Keep in mind this is just a user interface feature; unless you submit your form securely, it will get sent in plain text, which is bad for security — a malicious party could intercept your data and steal passwords, credit card details, or whatever else you've submitted. The best way to protect users from this is to host any pages involving forms over a secure connection (i.e. located at an https:// address), so the data is encrypted before it is sent.

Browsers recognize the security implications of sending form data over an insecure connection, and have warnings to deter users from using insecure forms. For more information on what Firefox implements, see Insecure passwords .

Hidden content

Another original text control is the hidden input type. This is used to create a form control that is invisible to the user, but is still sent to the server along with the rest of the form data once submitted — for example you might want to submit a timestamp to the server stating when an order was placed. Because it is hidden, the user can not see nor intentionally edit the value, it will never receive focus, and a screen reader will not notice it either.

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    hidden"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    timestamp"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    timestamp"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    1286705410"
                                                
                                                />
                                            
                                        
                                    

If you create such an element, it's required to set its name and value attributes. The value can be dynamically set via JavaScript. The hidden input type should not have an associated label.

Other text input types, like search , url , and tel , will be covered in the next tutorial, HTML5 input types .

Checkable items: checkboxes and radio buttons

Checkable items are controls whose state you can change by clicking on them or their associated labels. There are two kinds of checkable items: the checkbox and the radio button. Both use the checked attribute to indicate whether the widget is checked by default or not.

It's worth noting that these widgets do not behave exactly like other form widgets. For most form widgets, once the form is submitted all widgets that have a name attribute are sent, even if no value has been filled out. In the case of checkable items, their values are sent only if they are checked. If they are not checked, nothing is sent, not even their name. If they are checked but have no value, the name is sent with a value of on.

Note: You can find the examples from this section on GitHub as checkable-items.html (see it live also ).

For maximum usability/accessibility, you are advised to surround each list of related items in a <fieldset > , with a <legend > providing an overall description of the list. Each individual pair of <label > / <input > elements should be contained in its own list item (or similar). The associated <label > is generally placed immediately before or after the radio button or checkbox, with the instructions for the group of radio button or checkboxes generally being the content of the <legend > . See the examples linked above for structural examples.

Checkbox

A checkbox is created using the <input > element with a type attribute set to the value checkbox .

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    questionOne"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    subscribe"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    yes"
                                                
                                                checked
                                                />
                                            
                                        
                                    

Related checkbox items should use the same name attribute. Including the checked attribute makes the checkbox checked automatically when the page loads. Clicking the checkbox or its associated label toggles the checkbox on and off.

html
                                        
                                            
                                                
                                                    <
                                                    fieldset
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    legend
                                                
                                                >
                                            
                                            Choose all the vegetables you like to eat
                                            
                                                
                                                    </
                                                    legend
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    carrots"
                                                
                                                >
                                            
                                            Carrots
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    carrots"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    vegetable"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    carrots"
                                                
                                                checked
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    peas"
                                                
                                                >
                                            
                                            Peas
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    peas"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    vegetable"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    peas"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    cabbage"
                                                
                                                >
                                            
                                            Cabbage
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    checkbox"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    cabbage"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    vegetable"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    cabbage"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    fieldset
                                                
                                                >
                                            
                                        
                                    

The following screenshot shows checkboxes in the default, focused, and disabled states. Checkboxes in the default and disabled states appear checked, whereas in the focused state, the checkbox is unchecked, with focus ring around it.

Default, focused and disabled Checkboxes in chrome 115 on macOS

Note: Any checkboxes and radio buttons with the checked attribute on load match the :default pseudo-class, even if they are no longer checked. Any that are currently checked match the :checked pseudo-class.

Due to the on-off nature of checkboxes, the checkbox is considered a toggle button, with many developers and designers expanding on the default checkbox styling to create buttons that look like toggle switches. You can see an example in action here (also see the source code ).

Radio button

A radio button is created using the <input > element with its type attribute set to the value radio :

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    soup"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    meal"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    soup"
                                                
                                                checked
                                                />
                                            
                                        
                                    

Several radio buttons can be tied together. If they share the same value for their name attribute, they will be considered to be in the same group of buttons. Only one button in a given group may be checked at a time; this means that when one of them is checked all the others automatically get unchecked. When the form is sent, only the value of the checked radio button is sent. If none of them are checked, the whole pool of radio buttons is considered to be in an unknown state and no value is sent with the form. Once one of the radio buttons in a same-named group of buttons is checked, it is not possible for the user to uncheck all the buttons without resetting the form.

html
                                        
                                            
                                                
                                                    <
                                                    fieldset
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    legend
                                                
                                                >
                                            
                                            What is your favorite meal?
                                            
                                                
                                                    </
                                                    legend
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    soup"
                                                
                                                >
                                            
                                            Soup
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    soup"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    meal"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    soup"
                                                
                                                checked
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    curry"
                                                
                                                >
                                            
                                            Curry
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    curry"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    meal"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    curry"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    pizza"
                                                
                                                >
                                            
                                            Pizza
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    radio"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    pizza"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    meal"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    pizza"
                                                
                                                />
                                            
                                            
                                                
                                                    </
                                                    li
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    ul
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    fieldset
                                                
                                                >
                                            
                                        
                                    

The following screenshot shows default and disabled radio buttons in the checked state, along with a focused radio button in the unchecked state.

Default, focused and disabled Radio buttons in chrome 115 on macOS

Actual buttons

The radio button isn't actually a button, despite its name; let's move on and look at actual buttons! There are three input types that produce buttons:

submit

Sends the form data to the server. For <button > elements, omitting the type attribute (or an invalid value of type ) results in a submit button.

reset

Resets all form widgets to their default values.

button

Buttons that have no automatic effect but can be customized using JavaScript code.

Then we also have the <button > element itself. This can take a type attribute of value submit , reset , or button to mimic the behavior of the three <input > types mentioned above. The main difference between the two is that actual <button > elements are much easier to style.

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    Submit this form"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    reset"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    Reset this form"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    Do Nothing without JavaScript"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                >
                                            
                                            Submit this form
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    reset"
                                                
                                                >
                                            
                                            Reset this form
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            Do Nothing without JavaScript
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                        
                                    

Note: The image input type also renders as a button. We'll cover that later too.

Note: You can find the examples from this section on GitHub as button-examples.html (see it live also ).

Below you can find examples of each button <input > type, along with the equivalent <button > type.

submit

html
                                        
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                >
                                            
                                            This is a 
                                            
                                                
                                                    <
                                                    strong
                                                
                                                >
                                            
                                            submit button
                                            
                                                
                                                    </
                                                    strong
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    submit"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    This is a submit button"
                                                
                                                />
                                            
                                        
                                    

reset

html
                                        
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    reset"
                                                
                                                >
                                            
                                            This is a 
                                            
                                                
                                                    <
                                                    strong
                                                
                                                >
                                            
                                            reset button
                                            
                                                
                                                    </
                                                    strong
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    reset"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    This is a reset button"
                                                
                                                />
                                            
                                        
                                    

anonymous

html
                                        
                                            
                                                
                                                    <
                                                    button
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                >
                                            
                                            This is an 
                                            
                                                
                                                    <
                                                    strong
                                                
                                                >
                                            
                                            anonymous button
                                            
                                                
                                                    </
                                                    strong
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    button
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    button"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    This is an anonymous button"
                                                
                                                />
                                            
                                        
                                    

Buttons always behave the same whether you use a <button > element or an <input > element. As you can see from the examples, however, <button > elements let you use HTML in their content, which is inserted between the opening and closing <button > tags. <input > elements on the other hand are void elements ; their displayed content is inserted inside the value attribute, and therefore only accepts plain text as content.

The following screenshot shows a button in the default, focused, and disabled states. In the focused state, there is a focus ring around the button, and in the disabled state, the button is greyed out.

Default, focus, and disabled button states in chrome 115 on macOS

Image button

The image button control is rendered exactly like an <img > element, except that when the user clicks on it, it behaves like a submit button.

An image button is created using an <input > element with its type attribute set to the value image . This element supports exactly the same set of attributes as the <img > element, plus all the attributes supported by other form buttons.

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    image"
                                                
                                                alt
                                                
                                                    =
                                                    "
                                                    Click me!"
                                                
                                                src
                                                
                                                    =
                                                    "
                                                    my-img.png"
                                                
                                                width
                                                
                                                    =
                                                    "
                                                    80"
                                                
                                                height
                                                
                                                    =
                                                    "
                                                    30"
                                                
                                                />
                                            
                                        
                                    

If the image button is used to submit the form, this control doesn't submit its value — instead, the X and Y coordinates of the click on the image are submitted (the coordinates are relative to the image, meaning that the upper-left corner of the image represents the coordinate (0, 0)). The coordinates are sent as two key/value pairs:

  • The X value key is the value of the name attribute followed by the string ".x ".
  • The Y value key is the value of the name attribute followed by the string ".y ".

So for example when you click on the image at coordinate (123, 456) and it submits via the get method, you'll see the values appended to the URL as follows:

url
                                        
                                            
                                                http:
                                            
                                            
                                                //
                                                foo.com
                                            
                                            
                                                ?
                                                
                                                    pos.x
                                                    =123
                                                
                                                &
                                                
                                                    pos.y
                                                    =456
                                                
                                            
                                        
                                    

This is a very convenient way to build a "hot map". How these values are sent and retrieved is detailed in the Sending form data article.

File picker

There is one last <input > type that came to us in early HTML: the file input type. Forms are able to send files to a server (this specific action is also detailed in the Sending form data article). The file picker widget can be used to choose one or more files to send.

To create a file picker widget , you use the <input > element with its type attribute set to file . The types of files that are accepted can be constrained using the accept attribute. In addition, if you want to let the user pick more than one file, you can do so by adding the multiple attribute.

Example

In this example, a file picker is created that requests graphic image files. The user is allowed to select multiple files in this case.

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                accept
                                                
                                                    =
                                                    "
                                                    image/*"
                                                
                                                multiple
                                                />
                                            
                                        
                                    

On some mobile devices, the file picker can access photos, videos, and audio captured directly by the device's camera and microphone by adding capture information to the accept attribute like so:

html
                                        
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                accept
                                                
                                                    =
                                                    "
                                                    image/*;capture=camera"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                accept
                                                
                                                    =
                                                    "
                                                    video/*;capture=camcorder"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    file"
                                                
                                                accept
                                                
                                                    =
                                                    "
                                                    audio/*;capture=microphone"
                                                
                                                />
                                            
                                        
                                    

The following screenshot shows the file picker widget in the default, focus, and disabled states when no file is selected.

File picker widget in default, focus, and disabled states in chrome 115 on macOS

Common attributes

Many of the elements used to define form controls have some of their own specific attributes. However, there is a set of attributes common to all form elements. You've met some of these already, but below is a list of those common attributes, for your reference:

Attribute name Default value Description
autofocus false This Boolean attribute lets you specify that the element should automatically have input focus when the page loads. Only one form-associated element in a document can have this attribute specified.
disabled false This Boolean attribute indicates that the user cannot interact with the element. If this attribute is not specified, the element inherits its setting from the containing element, for example, <fieldset > ; if there is no containing element with the disabled attribute set, then the element is enabled.
form The <form > element that the widget is associated with, used if it is not nested within that form. The value of the attribute must be the id attribute of a <form > element in the same document. This lets you associate a form control with a form it is outside of, even if it is inside a different form element.
name The name of the element; this is submitted with the form data.
value The element's initial value.

Test your skills!

You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: Basic controls .

Summary

This article has covered the older input types — the original set introduced in the early days of HTML that is well-supported in all browsers. In the next section, we'll take a look at the more modern values of the type attribute.

Advanced Topics

Other form controls

We now look at the functionality of non-<input > form elements in detail, from other control types such as drop-down lists and multi-line text fields, to other useful form features such as the <output > element (which we saw in action in the previous article), and progress bars.

Prerequisites: A basic understanding of HTML .
Objective: To understand the non-<input > form features, and how to implement them using HTML.

Multi-line text fields

A multi-line text field is specified using a <textarea > element, rather than using the <input > element.

html
                                        
                                            
                                                
                                                    <
                                                    textarea
                                                
                                                cols
                                                
                                                    =
                                                    "
                                                    30"
                                                
                                                rows
                                                
                                                    =
                                                    "
                                                    8"
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    textarea
                                                
                                                >
                                            
                                        
                                    

This renders like so:

The main difference between a <textarea > and a regular single-line text field is that users are allowed to include hard line breaks (i.e. pressing return) that will be included when the data is submitted.

<textarea > also takes a closing tag; any default text you want it to contain should be put between the opening and closing tags. In contrast, the <input > is a void element with no closing tag — any default value is put inside the value attribute.

Note that even though you can put anything inside a <textarea > element (including other HTML elements, CSS, and JavaScript), because of its nature, it is all rendered as if it was plain text content. (Using contenteditable on non-form controls provides an API for capturing HTML/"rich" content instead of plain text).

Visually, the text entered wraps and the form control is by default resizable. Modern browsers provide a drag handle that you can drag to increase/decrease the size of the text area.

The following screenshots show default, focused, and disabled <textarea > elements in Firefox 71 and Safari 13 on macOS and in Edge 18, Yandex 14, Firefox 71, and Chrome 79 on Windows 10.

The default, focused, and disabled 'textarea' element in Firefox 71 and Safari 13 on Mac OSX and Edge 18, Yandex 14, Firefox and Chrome on Windows 10.

Note: You can find a slightly more interesting example of text area usage in the example we put together in the first article of the series (see the source code also ).

Controlling multi-line rendering

<textarea > accepts three attributes to control its rendering across several lines:

cols

Specifies the visible width (columns) of the text control, measured in average character widths. This is effectively the starting width, as it can be changed by resizing the <textarea > , and overridden using CSS. The default value if none is specified is 20.

rows

Specifies the number of visible text rows for the control. This is effectively the starting height, as it can be changed by resizing the <textarea > , and overridden using CSS. The default value if none is specified is 2.

wrap

Specifies how the control wraps text. The values are soft (the default value), which means the text submitted is not wrapped but the text rendered by the browser is wrapped; hard (the cols attribute must be specified when using this value), which means both the submitted and rendered texts are wrapped, and off , which stops wrapping.

Controlling textarea resizability

The ability to resize a <textarea > is controlled with the CSS resize property. Its possible values are:

  • both : The default — allows resizing horizontally and vertically.
  • horizontal : Allows resizing only horizontally.
  • vertical : Allows resizing only vertically.
  • none : Allows no resizing.
  • block and inline : Experimental values that allow resizing in the block or inline direction only (this varies depending on the directionality of your text; read Handling different text directions if you want to find out more.)

Play with the interactive example at the top of the resize reference page for a demonstration of how these work.

Drop-down controls are a simple way to let users select from many options without taking up much space in the user interface. HTML has two types of drop-down controls: the select box and the autocomplete box . The interaction is the same in both the types of drop-down controls — after the control is activated, the browser displays a list of values the user can select from.

Note: You can find examples of all the drop-down box types on GitHub at drop-down-content.html (see it live also ).

Select box

A simple select box is created with a <select > element with one or more <option > elements as its children, each of which specifies one of its possible values.

Basic example

html
                                        
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    simple"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    simple"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                selected
                                                >
                                            
                                            Cherry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                        
                                    

If required, the default value for the select box can be set using the selected attribute on the desired <option > element — this option is then preselected when the page loads.

Using optgroup

The <option > elements can be nested inside <optgroup > elements to create visually associated groups of values:

html
                                        
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    groups"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    groups"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    optgroup
                                                
                                                label
                                                
                                                    =
                                                    "
                                                    fruits"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                selected
                                                >
                                            
                                            Cherry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    optgroup
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    optgroup
                                                
                                                label
                                                
                                                    =
                                                    "
                                                    vegetables"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Carrot
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Eggplant
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Potato
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    optgroup
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                        
                                    

On the <optgroup > element, the value of the label attribute is displayed before the values of the nested options. The browser usually sets them visually apart from the options (i.e. by being bolded and at a different nesting level) so they are less likely to be confused for actual options.

Using the value attribute

If an <option > element has an explicit value attribute set on it, that value is sent when the form is submitted with that option selected. If the value attribute is omitted, as with the examples above, the content of the <option > element is used as the value. So value attributes are not needed, but you might find a reason to want to send a shortened or different value to the server than what is visually shown in the select box.

For example:

html
                                        
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    simple"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    simple"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    banana"
                                                
                                                >
                                            
                                            Big, beautiful yellow banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    cherry"
                                                
                                                >
                                            
                                            Succulent, juicy cherry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    lemon"
                                                
                                                >
                                            
                                            Sharp, powerful lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                        
                                    

By default, the height of the select box is enough to display a single value. The optional size attribute provides control over how many options are visible when the select does not have focus.

Multiple choice select box

By default, a select box lets a user select only one value. By adding the multiple attribute to the <select > element, you can allow users to select several values. Users can select multiple values by using the default mechanism provided by the operating system (e.g., on the desktop, multiple values can be clicked while holding down Cmd /Ctrl keys).

html
                                        
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    multi"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    multi"
                                                
                                                multiple
                                                size
                                                
                                                    =
                                                    "
                                                    2"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    optgroup
                                                
                                                label
                                                
                                                    =
                                                    "
                                                    fruits"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                selected
                                                >
                                            
                                            Cherry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    optgroup
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    optgroup
                                                
                                                label
                                                
                                                    =
                                                    "
                                                    vegetables"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Carrot
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Eggplant
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Potato
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    optgroup
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                        
                                    

Note: In the case of multiple choice select boxes, you'll notice that the select box no longer displays the values as drop-down content — instead, all values are displayed at once in a list, with the optional size attribute determining the height of the widget.

Note: All browsers that support the <select > element also support the multiple attribute.

Autocomplete box

You can provide suggested, automatically-completed values for form widgets using the <datalist > element with child <option > elements to specify the values to display. The <datalist > needs to be given an id .

The data list is then bound to an <input > element (e.g. a text or email input type) using the list attribute, the value of which is the id of the data list to bind.

Once a data list is affiliated with a form widget, its options are used to auto-complete text entered by the user; typically, this is presented to the user as a drop-down box listing possible matches for what they've typed into the input.

Basic example

Let's look at an example.

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    myFruit"
                                                
                                                >
                                            
                                            What's your favorite fruit?
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    myFruit"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    myFruit"
                                                
                                                list
                                                
                                                    =
                                                    "
                                                    mySuggestion"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    datalist
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    mySuggestion"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Apple
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Blackberry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Blueberry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lychee
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Peach
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Pear
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    datalist
                                                
                                                >
                                            
                                        
                                    

Datalist support and fallbacks

Almost all browsers support datalist, but if you are still supporting older browsers such as IE versions below 10, there is a trick to provide a fallback:

html
                                        
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    myFruit"
                                                
                                                >
                                            
                                            What is your favorite fruit? (With fallback)
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    input
                                                
                                                type
                                                
                                                    =
                                                    "
                                                    text"
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    myFruit"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    fruit"
                                                
                                                list
                                                
                                                    =
                                                    "
                                                    fruitList"
                                                
                                                />
                                            
                                            
                                                
                                                    <
                                                    datalist
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    fruitList"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    label
                                                
                                                for
                                                
                                                    =
                                                    "
                                                    suggestion"
                                                
                                                >
                                            
                                            or pick a fruit
                                            
                                                
                                                    </
                                                    label
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    select
                                                
                                                id
                                                
                                                    =
                                                    "
                                                    suggestion"
                                                
                                                name
                                                
                                                    =
                                                    "
                                                    altFruit"
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Apple
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Banana
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Blackberry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Blueberry
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lemon
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Lychee
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Peach
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    <
                                                    option
                                                
                                                >
                                            
                                            Pear
                                            
                                                
                                                    </
                                                    option
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    select
                                                
                                                >
                                            
                                            
                                                
                                                    </
                                                    datalist
                                                
                                                >
                                            
                                        
                                    

Browsers that support the <datalist > element will ignore all the elements that are not <option > elements, with the datalist working as expected. Old browsers that don't support the <datalist > element will display the label and the select box.

The following screenshot shows the datalist fallback as rendered in Safari 6:

Screenshot of the datalist element fallback with Safari on Mac OS

If you use this fallback, ensure the data for both the <input > and the <select > are collected server-side.

Less obvious datalist uses

According to the HTML specification , the list attribute and the <datalist > element can be used with any kind of widget requiring a user input. This leads to some uses of it that might seem a little non-obvious.

For example, in browsers that support <datalist > on range input types, a small tick mark will be displayed above the range for each datalist <option > value. You can see an implementation example of this on the <input type="range"> reference page .

And browsers that support <datalist > s and <input type="color"> should display a customized palette of colors as the default, while still making the full color palette available.

In this case, different browsers behave differently from case to case, so consider such uses as progressive enhancement, and ensure they degrade gracefully.

Other form features

There are a few other form features that are not as obvious as the ones we have already mentioned, but still useful in some situations, so we thought it would be worth giving them a brief mention.

Note: You can find the examples from this section on GitHub as other-examples.html (see it live also ).

Meters and progress bars

Meters and progress bars are visual representations of numeric values. Support for <progress > and <meter > is available in all modern browsers.

Meter

A meter bar represents a fixed value in a range delimited by max and min values. This value is visually rendered as a bar, and to know how this bar looks, we compare the value to some other set values:

  • The low and high values divide the range into the following three parts:
    • The lower part of the range is between the min and low values, inclusive.
    • The medium part of the range is between the low and high values, exclusive.
    • The higher part of the range is between the high and max values, inclusive.
  • The optimum value defines the optimum value for the <meter > element. In conjunction with the low and high value, it defines which part of the range is preferred:
    • If the optimum value is in the lower part of the range, the lower range is considered to be the preferred part, the medium range is considered to be the average part, and the higher range is considered to be the worst part.
    • If the optimum value is in the medium part of the range, the lower range is considered to be an average part, the medium range is considered to be the preferred part, and the higher range is considered to be average as well.
    • If the optimum value is in the higher part of the range, the lower range is considered to be the worst part, the medium range is considered to be the average part and the higher range is considered to be the preferred part.

All browsers that implement the <meter > element use those values to change the color of the meter bar:

  • If the current value is in the preferred part of the range, the bar is green.
  • If the current value is in the average part of the range, the bar is yellow.
  • If the current value is in the worst part of the range, the bar is red.

Such a bar is created by using the <meter > element. This is for implementing any kind of meter; for example, a bar showing the total space used on a disk, which turns red when it starts to get full.

html
                                        
                                            
                                                
                                                    <
                                                    meter
                                                
                                                min
                                                
                                                    =
                                                    "
                                                    0"
                                                
                                                max
                                                
                                                    =
                                                    "
                                                    100"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    75"
                                                
                                                low
                                                
                                                    =
                                                    "
                                                    33"
                                                
                                                high
                                                
                                                    =
                                                    "
                                                    66"
                                                
                                                optimum
                                                
                                                    =
                                                    "
                                                    0"
                                                
                                                >
                                            
                                            75
                                            
                                                
                                                    </
                                                    meter
                                                
                                                >
                                            
                                        
                                    

The content inside the <meter > element is a fallback for browsers that don't support the element and for assistive technologies to vocalize it.

Progress

A progress bar represents a value that changes over time up to a maximum value specified by the max attribute. Such a bar is created using a <progress > element.

html
                                        
                                            
                                                
                                                    <
                                                    progress
                                                
                                                max
                                                
                                                    =
                                                    "
                                                    100"
                                                
                                                value
                                                
                                                    =
                                                    "
                                                    75"
                                                
                                                >
                                            
                                            75/100
                                            
                                                
                                                    </
                                                    progress
                                                
                                                >
                                            
                                        
                                    

This is for implementing anything requiring progress reporting, such as the percentage of total files downloaded, or the number of questions filled in on a questionnaire.

The content inside the <progress > element is a fallback for browsers that don't support the element and for screen readers to vocalize it.

Test your skills!

You've reached the end of this article, but can you remember the most important information? You can find some further tests to verify that you've retained this information before you move on — see Test your skills: Other controls .

Summary

As you'll have seen in the last few articles, there are many types of form controls. You don't need to remember all of these details at once, and can return to these articles as often as you like to check up on details.

Now that you have a grasp of the HTML behind the different available form controls, we'll take a look at Styling them .

Advanced Topics

Updated on April 20, 2024 by Datarist.