Home > Web dev > Use the fieldset and legend elements to group HTML form controls

Use the fieldset and legend elements to group HTML form controls

October 4th, 2009

The fieldset and legend elements, which should always be used together, allow you to create and name groups of related input fields in HTML forms. By doing this you help users understand how the input fields are related.

How the grouping is conveyed to the user depends mainly on whether the user is sighted or not. Most graphical web browsers draw a border around fieldset elements and render the legend element on top of the border, while screen readers may speak the legend text at the start of each fieldset or before each form control within a fieldset.

Since some screen readers will announce the legend text before each form control, it is important to keep the legend text concise and consider how the legend and label texts will work when put together.

Here is a simple example of a fieldset element used to group related radio buttons:

<fieldset>
<legend>Favourite colour</legend>
<input type="radio" name="fav-col" id="fav-col-1" value="red">
<label for="fav-col-1">Red</label>
<input type="radio" name="fav-col" id="fav-col-2" value="green">
<label for="fav-col-2">Green</label>
<input type="radio" name="fav-col" id="fav-col-3" value="blue">
<label for="fav-col-3">Blue</label>
</fieldset>

In summary: Do not use the fieldset and legend elements if all you want to do is create a graphical border around some content on a page. Only use them to group logically related form controls, always use both elements together, and keep legend texts short.

share save 171 16 Use the fieldset and legend elements to group HTML form controls

Web dev Tags:, , , , ,

Related posts » Top jQuery plugins and tutorials in 2011
» 50 Awesome Animations using CSS3
» 25 Useful Html5 Cheat Sheets and Tutorials For Web Developer
» 7 Best Mobile Web HTML5 Framework For Mobile App Development
» 10 Common Useful PHP Regular Expression
  • http://polprav.blogspot.com/ Polprav

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  • http://code24h.net admin

    Polprav :
    Hello from Russia!
    Can I quote a post in your blog with the link to you?

    You’re welcome :)