HTML input types part 2


Input Type Color

The <input type="color"> is used for input fields that should contain a color.

Depending on browser support, a color picker can show up in the input field.



                        <form>
                          Select your favorite color:
                          <input type="color" name="favcolor">
                        </form>
      
      

Live preview

Select your favorite color:

Input Type Date

The <input type="date"> is used for input fields that should contain a date.

Depending on browser support, a date picker can show up in the input field.


                        <form>
                          Birthday:
                          <input type="date" name="bday">
                        </form>
            
      

Live preview

Birthday:

You can also use the min and max attributes to add restrictions to dates:


                      <form>
                        Enter a date before 1980-01-01:
                        <input type="date" name="bday" max="1979-12-31"><br>
                        Enter a date after 2000-01-01:
                        <input type="date" name="bday" min="2000-01-02"><br>
                      </form>
                

Live preview

Enter a date before 1980-01-01:
Enter a date after 2000-01-01:

Input Type Datetime-local

The <input type="datetime-local"> specifies a date and time input field, with no time zone.

Depending on browser support, a date picker can show up in the input field.


                      <form>
                        Birthday (date and time):
                        <input type="datetime-local" name="bdaytime">
                      </form>
                

Live preview

Birthday (date and time):

Input Type Email

The <input type="email"> is used for input fields that should contain an e-mail address.

Depending on browser support, the e-mail address can be automatically validated when submitted.

Some smartphones recognize the email type, and adds ".com" to the keyboard to match email input.


                     <form>
                        E-mail:
                        <input type="email" name="email">
                      </form>
                

Live preview

E-mail:

Input Type File

The <input type="file"> defines a file-select field and a "Browse" button for file uploads.


                      <form>
                          Select a file: <input type="file" name="myFile">
                        </form>
                  

Live preview

Select a file:

Input Type Month

The <input type="month"> allows the user to select a month and year.

Depending on browser support, a date picker can show up in the input field.


                    <form>
                        Birthday (month and year):
                        <input type="month" name="bdaymonth">
                      </form>
                

Live preview

Birthday (month and year):

Input Type Number

The <input type="number"> defines a numeric input field.

You can also set restrictions on what numbers are accepted.

The following example displays a numeric input field, where you can enter a value from 1 to 5:


                      <form>
                          Quantity (between 1 and 5):
                          <input type="number" name="quantity" min="1" max="5">
                        </form>
                  

Live preview

Quantity (between 1 and 5):

Input Type Range

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the min, max, and step attributes:


                      <form>
                          <input type="range" name="points" min="0" max="10">
                        </form>
                  

Live preview


Input Type Search

The <input type="search"> is used for search fields (a search field behaves like a regular text field).


                    <form>
                        Search Google:
                        <input type="search" name="googlesearch">
                      </form>
                

Live preview

Search Google:

Input Type Time

The <input type="time"> allows the user to select a time (no time zone).

Depending on browser support, a time picker can show up in the input field.


                    <form>
                        Select a time:
                        <input type="time" name="usr_time">
                      </form>
                

Live preview

Select a time:

Input Type Url

The <input type="url"> is used for input fields that should contain a URL address.

Depending on browser support, the url field can be automatically validated when submitted.

Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.


                            <form>
                                Add your homepage:
                                <input type="url" name="homepage">
                              </form>
                        

Live preview

Add your homepage:

Input Type Week

The <input type="week"> allows the user to select a week and year.

Depending on browser support, a date picker can show up in the input field.


                    <form>
                        Select a week:
                        <input type="week" name="week_year">
                      </form>
                

Live preview

Select a week:

Rate this lesson

Spread the word:
Do you need help? Use our support forum

About the author

User avatar
Michal Szymanski
Co-Founder at MDBootstrap & BrandFlow. Entrepreneur, web developer, UI/UX designer, marketing analyst. Dancer and nerd in one.