HTML Attributes

dheeraj korangadheeraj koranga
2 min read

HTML attributes are special keywords that are defined inside the HTML tags to provide additional information about an element. They define properties and behavior for elements and are always included in the opening tag.

Characteristics of HTML Attributes:

  • They appear inside the opening tag.

  • They are always written as name/value pairs: name="value".

  • Some attributes are specific to certain elements, while others (like id, class, and style) can be used with many different elements.

  1. id:

    • Used to uniquely identify an element.
      <div id="header">This is the header</div>
  2. src:

    • Used with the <img> and <script> elements to specify the source of an external file.
      <img src="image.jpg" alt="Image description">
  3. href:

  4. alt:

    • Provides alternative text for an image if it cannot be displayed.

      <img src="image.jpg" alt="Description of the image">

  5. style:

    • Used to apply inline CSS directly to an element.

      <p style="color: blue; font-size: 20px;">This is styled text.</p>

  6. title:

    • Provides additional information that appears as a tooltip when the user hovers over the element.
      <button title="Click me">Hover over me</button>
  7. target:

    • Used in <a> to specify where to open the linked document.
      <a href="https://www.example.com" target="_blank">Open in new tab</a>
  8. name:

    • Used in form elements like <input>, <select>, and <textarea> to identify the form data.
      <input type="text" name="username">

      Example :

    •   <!DOCTYPE html>
        <html lang="en">
        <head>
            <a href="https://www.example.com" target="_blank" 
            title="Click to visit">Visit Example</a>
        </html>
      

0
Subscribe to my newsletter

Read articles from dheeraj koranga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

dheeraj koranga
dheeraj koranga