How to create a dropdown list in an HTML page

HTML Dropdown List

In HTML, a dropdown list is a form element that allows users to select one option from a list of predefined options.

Dropdown list is created using HTML <select> element.

Options of the dropdown list are created using HTML <option> element.

How to Create a Dropdown List

Create an HTML <select> element as shown in the example.

Add five HTML <option> elements inside the HTML <select> element.

Add "Python Language", "C# Language", "Java Language", "C++ Language", and "C Language" repectively to the 5 options. The texts will be displayed as selection options in the dropdown list.

Have a closer look at the code and its output:

Example of Dropdown List


Output of the above code example

Example of Dropdown List in a Webpage


Select a programming language


Now you can select an option from the dropdown list.


Add Value Attributes to the Dropdown Options

Create an HTML form element and add the select element inside the form.

If you do not add value to the option, the option text will be treated as value of the option.

Sometime, it is necessary to add value attribute to the <option> opening tag for form submission.

Add "Python", "C#", "Java", "C++", and "C" as values to the value attributes of options as shown in the following example.

The value attribute specifies the value that will be submitted when the form is submitted.

Example of Dropdown List


Output of the above code example

Please note that JavaScript functionality has been added to the output to display the text and value of selected dropdown list option. This JS function is added to the code of Live Code Playground.

Example of Dropdown List in a Webpage


Select a programming language


Live Code Playground

In the following HTML code editor, you can practice the above code by adding more options to the select element. Click on the 'Run Code' button to render the code; the rendered page will be displayed in the following HTML Code Output frame. Practice until you become comfortable and proficient with your code.

HTML logo HTML Online Editor
HTML Code Output