Which HTML tags are self-closing?


HTML tags that self-close are ones that don't need additional closing tags. Instead, they end themselves within the opening tag with a following slash. These tags are used to insert items that are empty or solitary content. The br> element for line breaks, img> for images, and input> for form input fields are a few examples. Self-closing tags prevent browsers from having to look for closing tags, resulting in cleaner code and quicker rendering. Keep in mind to add the slash before the final angle bracket (tag />). Maintaining HTML code integrity and ensuring compatibility with various browsers and devices depend on the proper use of self-closing tags.

Methods Used

  • Line Break <br>

  • Horizontal Rule <hr>

  • Form Input Fields <input>

  • Table Column Styling <col>

  • Base URL for relative URLs <base>

  • Metadata <meta>

  • External Source Link <link>

  • Image Map Area <area>

Line Break

In HTML, the self-closing br> tag is used to insert a line break between paragraphs of text or other information. When inserted, it requires that the next line of text be blank. Vertically separating components or adding space between lines are frequently used formatting techniques.

Example

<p>This is the first line.<br>This is the second line.</p>

Horizontal Rule

The HTML <hr> tag designates the Horizontal Rule. On a webpage, it adds a horizontal line or separator. It serves to clearly distinguish between parts or elements on the page and visually split material.

Example

<p>This is some content above the horizontal rule.</p> 
<hr>
<p>This is some content below the horizontal rule.</p>

Form Input Fields

HTML form input fields, denoted by the input> tag, play a crucial part in creating interactive web forms that make it easier for users to enter data, like text, numbers, or options. They support many input forms, such as text, checkboxes, and radio buttons, and are accompanied with attribute definitions like "type." As a result, forms can be customised for precise data capture and increased user interaction. For instance, radio buttons present only one option, checkboxes allow multiple selections, and text inputs collect names or messages. Such adaptability improves user experience, boosts data collection, and encourages dynamic website engagement, resulting in a more engaging and user-friendly digital environment. Developers skillfully employ these techniques to enliven the online environment, enticing active user participation and seamless web interaction.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Form Input Fields Example</title>
</head>
<body>
   <form>
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" placeholder="Enter your name" required>

      <br>

      <label for="email">Email:</label>
      <input type="email" id="email" name="email" placeholder="Enter your email" required>

      <br>

      <label for="age">Age:</label>
      <input type="number" id="age" name="age" min="18" max="99" required>

      <br>

      <input type="submit" value="Submit">
   </form>
</body>
</html>

Table Column Styling

For formatting table columns in HTML, use the col> tag. It makes it simpler to customise the appearance of specific columns by enabling you to apply CSS styles to specific columns of a table. This tag improves the table's overall design by enhancing the visual presentation.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Table Column Styling</title>
   <style>   
      col:first-child {
         background-color: lightblue;
      }
        
      col:nth-child(2) {
         background-color: lightgreen;
      }
   </style>
</head>
<body>
   <table>
      <colgroup>
         <col>
         <col>
      </colgroup>
      <tr>
         <td>Column 1</td>
         <td>Column 2</td>
      </tr>
      <tr>
         <td>Column 1 Data</td>
         <td>Column 2 Data</td>
      </tr>
   </table>
</body>
</html>

Base Url for Relative URLs

The base URL for relative URLs inside of a document is specified via the HTML base> tag. It aids in defining the default URL to which all referencing resources and relative links are resolved. Setting the base URL will help keep the document's URLs short and simple, making the code easier to read and manage. When managing resources in many directories or multiple pages on the same website, this tag is extremely helpful. In order to ensure proper navigation and resource loading within the web page when a relative URL is encountered, the browser combines it with the base URL to determine the absolute URL.

Metadata

HTML uses the meta> metadata tag to offer details about the document, including authorship, character encoding, and page descriptions. It aids in the accurate understanding and display of the webpage by browsers, search engines, and other tools, enhancing accessibility and SEO performance.

External Resource Link

To add external files to their websites, such as stylesheets or icons, web designers must use the HTML link> tag. This link enhances the look and operation of the website. The tag creates a crucial connection between the primary content and the linked resource by indicating the file's location, optimising web design and improving user experience overall. This approach enables efficient management of resources, as multiple webpages can reference the same external file, promoting consistency throughout the website. Ultimately, the proper use of the <link> tag contributes to a seamless user experience by reducing load times and improving overall site performance.

Image Map Area

The "area" tag in HTML enables designers to specify clickable parts inside an image, resulting in an image map. These zones can have the form of rectangles, circles, or polygons, and each one can contain a distinct hyperlink or cause particular webpage behaviours. Using this potent feature, interactive and dynamic features on a web page are made possible, such as clickable map hotspots or navigation symbols. Users are taken to different URLs or perform particular activities when they click on these defined locations, which improves the user experience and creates a more engaging and interactive environment. For the creation of interactive infographics, virtual tours, and sophisticated navigation systems, image maps are especially helpful.

Conclusion

In conclusion, self-closing HTML elements are essential for building effective and well-organized web pages. Both developers and consumers gain from the use of tags like "br", "img", and "input," which ensure simpler code and quicker rendering. The base> tag also makes it easier to manage relative URLs within a page by giving all relative links and resources a single base URL.Self-closing tags increase code readability, maintainability, and device and browser compatibility when properly understood and used. Adopting these recommended practises results in user-friendly, effective websites that improve the entire online experience.

Updated on: 22-Aug-2023

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements