common html structure for a form
example1:
<form>
<fieldset>
<legend>Fruit juice size</legend>
<p>
<input type="radio" name="size" id="size_1" value="small">
<label for="size_1">Small</label>
</p>
<p>
<input type="radio" name="size" id="size_2" value="medium">
<label for="size_2">Medium</label>
</p>
<p>
<input type="radio" name="size" id="size_3" value="large">
<label for="size_3">Large</label>
</p>
</fieldset>
</form>
example2:
<form>
<p>
<input type="checkbox" id="taste_1" name="taste_cherry" value="cherry">
<label for="taste_1">I like cherry</label>
</p>
<p>
<input type="checkbox" id="taste_2" name="taste_banana" value="banana">
<label for="taste_2">I like banana</label>
</p>
</form>
Beyond the structures specific to web forms, it's good to remember that form markup is just HTML. This means that you can use all the power of HTML to structure a web form.
As you can see in the examples, it's common practice to wrap a label and its widget with a <li>
element within a <ul>
or <ol>
list. <p>
and <div>
elements are also commonly used. Lists are recommended for structuring multiple checkboxes or radio buttons.
In addition to the <fieldset>
element, it's also common practice to use HTML titles (e.g. <h1>
, <h2>
) and sectioning (e.g. <section>
) to structure complex forms.
Above all, it is up to you to find a comfortable coding style that results in accessible, usable forms. Each separate section of functionality should be contained in a separate <section>
element, with <fieldset>
elements to contain radio buttons.
Comments
Post a Comment