If you run a business website, you’ll probably want to collect visitor responses through forms. Forms are an excellent way to gather feedback, order information, personal information, or any other type of data your visitors can provide.

表格是如此有用,以至于HTMLeven has a collection of special elements for collecting responses on the front-end. So, if you’re building a site from scratch or designing a custom feedback page, you can build forms to your exact specifications.

One form element you’ll encounter often is the HTML “select” element, which adds a dropdown form field to a page. There’s a good chance you’ve encountered a select element on a website recently — they’re intuitive, accessible, and a staple of form design. In this post, we’ll show you how to make one in HTML.

Download Now: Free Intro Guide to HTML & CSS

The HTML select element consists of an opening and closingelement.

Next is thepairs the select element with the prior label element.

We have threetags nested in thetag, the user’s browser will automatically focus on the select element on page load.

disabled

When added to thetag with a

页面上的元素。此属性对于放置一个很有用, users must select a response from the dropdown list before submitting the form.

size

If yourautomatically selects the firstnested inside it. If you want to set a different item as the default option, add theselectedattribute to the desiredtag. Below, I’ve made the second option the default response.

See the PenHTML Select: selected attributeby Christina Perricone (@hubspot) onCodePen.

Add Placeholder Text to HTML select

A placeholder prevents a list item from being selected by default — instead, you can make the display text a prompt, such as “Choose one…”, etc.

To add a placeholder, write your placeholder text as anelement, and add both thedisabledandselectedattributes to this element.

See the PenHTML Select: placeholderby Christina Perricone (@hubspot) onCodePen.

Group HTML select Items

You can also separate response items into groups, which comes in handy for particularly large dropdowns. Nest(option group) tags inside your maintags nested inside thetag are included in the form, and responses are sent when the user clicks the submit button. Here’s what an HTML form containing a submit element looks like:

See the PenHTML Select: forms 1by Christina Perricone (@hubspot) onCodePen.

You can also place a