The <legend> tag in HTML is used to define a caption for the <fieldset> element. The <fieldset> element is used to group related elements within a form, and the <legend> provides a title for the group, making the form more accessible and easier to understand.
The <legend> tag in HTML is used with the <fieldset> tag. When the <legend> tag is used, it should be the first child element of the <fieldset> tag.
In this article, we will discuss about the legend tag in HTML in depth. We will discuss its syntax, example, accessibility considerations, best practices, etc.
Syntax of Legend Tag in HTML
The syntax for the legend tag in HTML is below with an example.
<legend>Text</legend>
Example-
<fieldset>
<legend>Coding Ninjas</legend>
<label for="name">Your Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Your Email:</label>
<input type="email" id="email" name="email"><br><br>
</fieldset>
In this example, the <legend> tag provides a caption for the group of form elements that follow it. The text "Coding Ninjas" will be displayed as the caption for the group of form elements.
The <legend> tag can be used in various ways to provide captions or titles for different groups of form elements. For example, use the <legend> tag to provide a caption for a group of radio buttons, checkboxes, or select menus.
Attributes of legend tag in HTML
The <legend> tag is used within <fieldset> elements to provide a caption or description for a group of related form controls. It doesn't have any unique attributes of its own.
Tag Specific Attributes
The <legend> tag has no specific attributes; it relies on the attributes and properties of the parent <fieldset> tag for styling and behavior.
Attribute | Key | Description |
---|---|---|
align | align, top, bottom, left, right | The alignment of the content |
Global Attributes
Global attributes can be used with any HTML element, including <legend>. Examples include class, id, style, and title.
Attribute | Description |
---|---|
class | Assigns CSS classes for custom styling. |
id | Provides a unique identifier for scripting. |
style | Adds inline CSS styles for precise control. |
accesskey | Defines a keyboard shortcut for quick access. |
contenteditable | Enables content editing if set to "true." |
dir | Specifies text direction: "ltr" or "rtl". |
Event Attributes
Event attributes, like onclick and onmouseover, can be used with the <legend> tag to define JavaScript behavior that responds to user interactions with the caption.
- onabort
- onblur
- onclick
- onfocus
- onkeydown
- onkeypress
- onkeyup
- onload
- onmouseenter
Form Events
Form events like onblur and onfocus can also be applied to the <legend> tag to trigger actions when users interact with form elements associated with the fieldset.
Attribute | Description |
---|---|
onblur | Occurs when an element loses focus. |
onchange | Fires when the value of an input element changes. |
onfocus | Occurs when an element gains focus. |
oninput | Triggers when the value of an input element is entered. |
onreset | Executes when a form is reset. |
onsubmit | Occurs when a form is submitted. |
Keyboard Events
Keyboard events like onkeydown and onkeyup can capture keyboard input events related to the legend element.
Attribute | Description |
---|---|
onkeydown | Fires when a key is pressed down. |
onkeypress | Occurs when a key is pressed and released. |
onkeyup | Executes when a key is released. |
Mouse Events
Mouse events like onmousedown and onmouseup can be applied to the <legend> tag to respond to mouse actions.
Attribute | Description |
---|---|
onclick | Occurs when the user clicks the mouse button. |
ondblclick | Fired when the user double-clicks the mouse button. |
onmousedown | Fires when a mouse button is pressed down. |
onmouseup | Occurs when a mouse button is released. |
onmousemove | Executes as the mouse pointer moves over an element. |
onmouseover | Fired when the mouse pointer enters an element. |
Drag Events
Drag events like ondragstart and ondragend can be used if drag-and-drop functionality is implemented within the fieldset.
Attribute | Description |
---|---|
ondrag | Fired when an element or text selection is being dragged. |
ondragend | Occurs when the user has finished dragging an element or selection. |
ondragenter | Fires when the mouse pointer enters a draggable element. |
ondragleave | Occurs when the mouse pointer leaves a draggable element. |
Clipboard Events
Clipboard events like oncut and oncopy can be used to manage clipboard-related interactions within the <legend> element and associated form controls.
Attribute | Description |
---|---|
oncopy | Fired when the user copies content to the clipboard. |
oncut | Occurs when the user cuts content to the clipboard. |
onpaste | Fires when the user pastes content from the clipboard. |