Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

HTML Tutorial

HTML HOMEHTML IntroductionHTML EditorsHTML BasicHTML ElementsHTML AttributesHTML HeadingsHTML ParagraphsHTML StylesHTML FormattingHTML QuotationsHTML CommentsHTML ColorsHTML CSSHTML LinksHTML ImagesHTML FaviconHTML Page TitleHTML TablesHTML ListsHTML Block & InlineHTML DivHTML ClassesHTML IdHTML ButtonsHTML IframesHTML JavaScriptHTML File PathsHTML HeadHTML LayoutHTML ResponsiveHTML ComputercodeHTML SemanticsHTML Style GuideHTML EntitiesHTML SymbolsHTML EmojisHTML CharsetsHTML URL EncodeHTML vs. XHTML

HTML Forms

HTML FormsHTML Form AttributesHTML Form ElementsHTML Input TypesHTML Input AttributesInput Form Attributes

HTML Graphics

HTML CanvasHTML SVG

HTML Media

HTML MediaHTML VideoHTML AudioHTML Plug-insHTML YouTube

HTML APIs

HTML Web APIsHTML GeolocationHTML Drag and DropHTML Web StorageHTML Web WorkersHTML SSE

HTML Examples

HTML ExamplesHTML EditorHTML QuizHTML ExercisesHTML WebsiteHTML SyllabusHTML Study PlanHTML Interview PrepHTML BootcampHTML CertificateHTML SummaryHTML Accessibility

HTML References

HTML Tag ListHTML AttributesHTML Global AttributesHTML Browser SupportHTML EventsHTML ColorsHTML CanvasHTML Audio/VideoHTML DoctypesHTML Character SetsHTML URL EncodeHTML Lang CodesHTTP MessagesHTTP MethodsPX to EM ConverterKeyboard Shortcuts

HTMLForm Elements


This chapter describes all the different HTML form elements.


The HTML <form> Elements

The HTML<form> element can contain one or more of the following form elements:

  • <input>
  • <label>
  • <select>
  • <textarea>
  • <button>
  • <fieldset>
  • <legend>
  • <datalist>
  • <output>
  • <option>
  • <optgroup>

The <input> Element

One of the most used form elements is the<input> element.

The<input> element can be displayed in several ways, depending on thetype attribute.

Example

<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
Try it Yourself »

All the different values of thetype attribute are covered in the next chapter:HTML Input Types.


The <label> Element

The<label> element defines a label for several form elements.

The<label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.

The<label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the<label> element, it toggles the radio button/checkbox.

Thefor attribute of the<label> tag should be equal to theid attribute of the<input> element to bind them together.


The <select> Element

The<select> element defines a drop-down list:

Example

<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
Try it Yourself »

The<option> element defines an option that can be selected.

By default, the first item in the drop-down list is selected.

To define a pre-selected option, add theselected attribute to the option:

Example

<option value="fiat" selected>Fiat</option>
Try it Yourself »

Visible Values:

Use thesize attribute to specify the number of visible values:

Example

<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="3">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
Try it Yourself »

Allow Multiple Selections:

Use themultiple attribute to allow the user to select more than one value:

Example

<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="4"multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
Try it Yourself »

The <textarea> Element

The<textarea> element defines a multi-line input field (a text area):

Example

<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
Try it Yourself »

Therows attribute specifies the visible number of lines in a text area.

Thecols attribute specifies the visible width of a text area.

This is how the HTML code above will be displayed in a browser:

You can also define the size of the text area by using CSS:

Example

<textarea name="message" style="width:200px; height:600px;">
The cat was playing in the garden.
</textarea>
Try it Yourself »


The <button> Element

The<button> element defines a clickable button:

Example

<button type="button" onclick="alert('Hello World!')">Click Me!</button>
Try it Yourself »

This is how the HTML code above will be displayed in a browser:


Note: Always specify thetype attribute for the button element. Different browsers may use different default types for the button element.


The <fieldset> and <legend> Elements

The<fieldset> element is used to group related data in a form.

The<legend> element defines a caption for the<fieldset>element.

Example

<form action="/action_page.php">
  <fieldset>
    <legend>Personalia:</legend>
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname" value="John"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value="Doe"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>
Try it Yourself »

This is how the HTML code above will be displayed in a browser:

Personalia:First name:

Last name:



The <datalist> Element

The<datalist> element specifies a list of pre-defined options for an<input> element.

Users will see a drop-down list of the pre-defined options as they input data.

Thelist attribute of the<input> element, must refer to theid attribute of the<datalist> element.

Example

<form action="/action_page.php">
  <input list="browsers">
  <datalist id="browsers">
    <option value="Edge">
   <option value="Firefox">
    <option value="Chrome">
   <option value="Opera">
    <option value="Safari">
  </datalist>
</form>
Try it Yourself »

The <output> Element

The<output> element represents the result of a calculation (like one performed by a script).

Example

Perform a calculation and show the result in an<output> element:

<form action="/action_page.php"
  oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
  <input type="range"  id="a" name="a" value="50">
  100 +
  <input type="number" id="b" name="b" value="50">
  =
  <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form>
Try it Yourself »


HTML Form Elements

TagDescription
<form> Defines an HTML form for user input
<input>Defines an input control
<textarea>Defines a multiline input control (text area)
<label>Defines a label for an <input> element
<fieldset>Groups related elements in a form
<legend>Defines a caption for a <fieldset> element
<select>Defines a drop-down list
<optgroup>Defines a group of related options in a drop-down list
<option>Defines an option in a drop-down list
<button>Defines a clickable button
<datalist>Specifies a list of pre-defined options for input controls
<output>Defines the result of a calculation

For a complete list of all available HTML tags, visit ourHTML Tag Reference.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp