<legend> Tag in HTML

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

A legend tag defines the caption or title for the <field set> tag, and it is the element's first child. Because it is a caption, it normally sits on top of the frame. <Field tag> is used to create a border around the content of a particular element. It is used to wrap forms, images, and content with the help of a fieldset, which can also describe the context of the information.

Syntax

Attributes

The following are the attributes of legend tag

Tag Specific Attributes

AttributeValueDescription
alignalign, top, bottom, left, rightIt defines the alignment of the content

Note: It is not supported in HTML5

Global Attributes

These attributes are those that can be applied to any HTML element.

AttributeDescription
ididentifies an element uniquely
classDefines a CSS class for the element
styleDefines the element's style
dataIdentifies additional data that JavaScript can use.
hiddenIndicates whether an element is hidden.
titleTitles are displayed as tooltips when a hover is performed
tabindexDetermines the tab position in relation to other elements
langIdentifies the element's language
draggableIndicates whether the element can be moved or not
accesskeySpecifies a shortcut key
inputmodeConfigures the keyboard for editing.
spellcheckDetermines if spellchecking is performed or not
autocapitalizeIndicates whether the data should be capitalized
contenteditableIndicates whether the data can be edited
dirDetermines the direction of the display: right-to-left or left-to-right
isIndicates that the element behaves as a custom element.

Event Atrributes

Window Event Attributes

These are the events triggered for the window object

AttributeValueDescription
onafterprintscriptRuns after printing of the document
onbeforeprintscriptRuns before printing the document
onbeforeunloadscriptRuns prior to document unloading
onerrorscriptRuns in the event of an error
onhashchangescriptRuns a script whenever a URL's anchor part changes
onloadscriptRuns after a page is loaded
onmessagescriptRuns when a message is sent
onofflinescriptExecutes the script when the browser starts to work offline
ononlinescriptRuns when the browser begins to work online
onpagehidescriptRun when a user exits a page
onpageshowscriptRuns when a user navigates to a page
onpopstatescriptRun this script whenever the window's history changes
onresizescriptRun this script when the browser window is resized
onstoragescriptRuns whenever a Web Storage area is updated
onunloadscriptruns after a page has finished unloading

Form Events

These are the events triggered by actions inside a HTML form

AttributeValueDescription
onblurscriptRuns when an element loses focus
onchangescriptRuns the moment when the value of the element is changed
oncontextmenuscriptRuns when a context menu is triggered
onfocusscriptRuns the moment when the element gets focus
oninputscriptRuns when an element gets user input
oninvalidscriptRuns when an element is invalid
onresetscriptRuns when the Reset button in a form is clicked
onsearchscriptRuns when the user writes something in a search field
onselectscriptRuns after some text has been selected in an element
onsubmitscriptRuns when a form is submitted

Keyboard Events

AttributeValueDescription
onkeydownscriptExecutes when a key is pressed by the user
onkeypressscriptExecutes when a key is pressed
onkeyupscriptexecutes when a key is released

Mouse Events

AttributeValueDescription
onclickscriptExecutes when the mouse is clicked on an element
ondblclickscriptExecutes when the mouse is double-clicked on the element
onmousedownscriptExecutes when a mouse button is pressed down on an element
onmousemovescriptExecutes when the mouse pointer is moving while it is over an element
onmouseoutscriptExecutes when the mouse pointer moves out of an element
onmouseoverscriptExecutes when the mouse pointer moves over an element
onmouseupscriptExecutes when a mouse button is released over an element
onwheelscriptExecutes when the mouse wheel rolls up or down over an element

Drag Events

AttributeValueDescription
ondragscriptIt runs when an element is dragged
ondragendscriptIt runs at the end of a drag operation
ondragenterscriptIt runs when an element has been dragged to a valid drop target
ondragleavescriptIt runs when an element leaves a valid drop target
ondragoverscriptIt runs when an element is being dragged over a valid drop target
ondragstartscriptIt runs at the start of a drag operation
ondropscriptRuns when dragged element is being dropped
onscrollscriptRuns when an element's scrollbar is being scrolled

Clipboard Events

AttributeValueDescription
onabortscriptScript to run when an abort occurs
oncanplayscriptRuns when a file is ready to play
oncanplaythroughscriptRuns when a file can be played uninterrupted from beginning to end
oncuechangescriptExecuted if a cue changes in a <track> element
ondurationchangescriptExecutes a script every time the media length changes
onemptiedscriptScript to be executed if a file becomes unavailable for some reason
onendedscriptWhen the media reaches its end, it will run this script
onerrorscriptRuns whenever there is an error loading a file
onloadeddatascriptRuns whenever media data has been loaded
onloadedmetadatascriptIt runs when meta data are loaded
onloadstartscriptRuns before anything is loaded just as the file begins to load
onpausescriptExecutes when media is paused either by the user or by a program
onplayscriptScript that runs when media is ready to be played
onplayingscriptRuns when the media has begun playing
onprogressscriptRuns when the media data is being downloaded by the browser
onratechangescriptRuns whenever the playback rate changes
onseekedscriptThe script is run after the seeking attribute becomes false, signaling that seeking is completed
onseekingscriptRuns when seeking is active and the seeking attribute is set to true
onstalledscriptRuns whenever the browser is unable to retrieve the media data
onsuspendscriptRun once media data is fetched but not fully loaded due to some reason
ontimeupdatescriptScript that runs when the playing position changes
onvolumechangescriptScript that runs when the volume is changed
onwaitingscriptScript that runs whenever the media has been paused but is expected to resume

Usage of <legend > Tag

  1. <legend> elements add captions to a <fieldset> element.
  2. The captions enhance user understanding of input fields and forms.
  3. Within<fieldset>, the <legend> tag must be the first tag.

Examples

HTML5 uses the <fieldset> tag to create a group of related elements in the form and creates a box around the elements. An example is shown below:

Explanation:

In the above example we are grouping the related elements in a form using the <fieldset> tag. It creates a box around the element in order to group them

Output:

Group related elements in a form

Example 2: Styling the Legend Tag Using CSS Properties

CSS is used here to style the Legend caption/title color by assigning a color attribute to each. An example is shown below:

Explanation:

In the above example we are using the style attribute of CSS to change the styling of the legend tag. Here we can change the font colour, or font style of the legend tag according to our needs

Output:

Styling the legend tag using CSS properties

Example 3: Fieldset Caption Float to the Right

A fieldset element's caption is aligned using the <legend> align attribute in HTML. An example is shown below:

Explanation

In the above example we are able to fieldset caption float to the right using the legend tag align attribute. It can take values like center, left, right and justify.

Output:

fieldset caption float to the right

Accessibility Concerns

The accessibility issues are discussed with the help of an example below

Herer we are taking a yes/no question as an example. The question asks 'Do you have a passport?' with radio button options of 'yes' or 'no'. In addition to having the questions and answers visually linked, it is also essential to have them linked programmatically. An example would be an answer of "yes" or "no":

Seeing the question in this example, it is obvious that the question has something to do with the group of radio buttons. The relationship is lost, however, if you cannot see the page. When using a screen reader to choose a radio button, you will hear the form labels announced ("Yes" or "No"), but possibly not the question itself. Because the question is not set up with any of the radio buttons inside the page's code, this results in a blank response.

The HTML code of the page provides most of the information screen readers need. Assistive technologies like screen readers can use HTML code to translate it into accessible information. Using the arrow keys on your screen reader will allow you to hear the question as you navigate through the form. However, most screen reader users prefer to use the tab key instead when navigating through forms. But using tab will not announce the question - making it difficult to decide what the correct answer might be!

The <fieldset> and <legend> elements fix this problem. This combination of elements tells screen readers that a group of fields is related and gives them a label. In place of the <p> element, the <fieldset> element contains all the related fields, and the <legend> element represents the question.

After using the fieldset and legend tags, the question will appear before the first field and its label when you use the tab key to navigate through the form. Our example would sound like "Do you have a visa?" Yes, radio button".

Browser Support

  1. Chrome
  2. Firefox
  3. Safari
  4. Edge
  5. Opera
ElementDescription
<form>It gives a data entry area that contains input elements
<input>It provides an input field in which data should be entered
<label>It provides a label or brief description before the input elements
<textarea>It provides a multi-line text input control
<select>It provides a dropdown control
<button>It provides a clickable button that can contain text or an image
<datalist>It gives a list of options for a textfield (<input>) element
<fieldset>It groups the related form elements and displays a box with a legend around these elements
<legend>It defines a caption for a fieldset

Conclusion

  1. With the HTML <legend> tag, the parent element like <fieldset> can be given a title or caption.
  2. An element named <legend> must be the first child of an element named <fieldset >.
  3. A grouped form element's purpose is easily understood by using the tag with it.
  4. Whenever the <legend> tag is used, the caption will appear before the form controls in the <fieldset> and appear to float over their top borders.