Menu

HTML Legend Tag

HTML Legend Tag

In HTML, the <legend> tag is used to provide a caption to the content of its parent <fieldset> tag.

  • Basically <legend> tag is used to define a title for child content.
  • We can use <legend> tag to perform various CSS properties.

HTML <legend> Tag Syntax and Usage

The <legend> element requires the start(opening) tag and end(closing) tag.

<legend>
.....content here
</legend>

HTML5 <legend> Tag Basic Example

Here, we have a basic example for a clear understanding of the <legend> tag:

<!doctype html>
<head>
   <title>basic Legend tag example</title>
   <style>
      legend {
      background-color: #000;
      color: #fff;
      padding: 3px 6px;
      }
      .output {
      font: 1rem 'Fira Sans', sans-serif;
      }
      input {
      margin: .4rem;
      }
   </style>
</head>
<body>
   <fieldset>
      <legend>Choose your favorite Fruit</legend>
      <input type="radio" id="mango" name="mango">
      <label for="mango">Mango</label><br/>
      <input type="radio" id="Pineapple" name="Pineapple">
      <label for="Pineapple">Pineapple</label><br/>
      <input type="radio" id="Kiwi" name="Kiwi">
      <label for="Kiwi">Kiwi</label>
   </fieldset>
</body>
</html>

Using various CSS Properties with <legend> Tag

Here, we are using CSS properties with This tag.

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Legend Tag</title> 
        <style> 
            form{ 
            width: 50%; 
            } 
            legend { 
                display: block; 
                padding-left: 10px; 
                padding-right: 10px; 
                border: 3px solid blue; 
                background-color:tomato; 
                color:white;; 
            } 
            label { 
                display: inline-block; 
                float: left; 
                clear: left; 
                width: 90px; 
                margin:5px; 
                text-align: left; 
            } 
            input[type="text"] { 
                width:250px; 
                margin:5px 0px; 
            } 
            .st { 
                font-size:40px; 
                color:orange; 
                font-weight:bold; 
            } 
        </style> 
    </head> 
    <body> 
        <div class = "st">StudyTonight</div> 
        <h2>Legend tag</h2> 
        <form> 
            <fieldset> 
                <legend>STUDENT:</legend> 
                <label>Name:</label><input type="text"> 
                <br> 
                <label>Email:</label><input type="text"> 
                <br> 
                <label>Date of birth:</label><input type="text"> 
                <br> 
                <label>Address:</label><input type="text"> 
                <br> 
                <label>Admission No:</label><input type="text"> 
            </fieldset> 
        </form> 
    </body> 
</html>

HTML <legend> Tag Attributes

This element does not have any specific attributes although this element supports Global and Event attributes.

Default CSS Settings for HTML <legend> Tag

legend {
  display: block;
  padding-left: 2px;
  padding-right: 2px;
  border: none;
}

Browser Support for HTML <legend> Tag

Following browsers support this attribute:

  • Firefox 1+
  • Google Chrome 1+
  • Internet Explorer 4+
  • Apple Safari 1+
  • Opera 4+