While global attributes can be applied to any HTML element, some attributes are specific to certain elements. Let's look at two common examples:

alt attribute for images (<img>)

  • Purpose: Provides alternative text for an image. This text is displayed if the image cannot be loaded, or for users who are visually impaired and use screen readers.
  • Usage:
    HTML
    <img src="image.jpg" alt="A beautiful landscape">
    
  • Key points:
    • Essential for accessibility.
    • Should accurately describe the image content.
    • Can be left empty for purely decorative images.

href attribute for links (<a>)

  • Purpose: Specifies the URL of the linked resource.
  • Usage:
    HTML
    <a href="https://www.example.com">Visit Example.com</a>
    
  • Key points:
    • Can link to internal pages, external websites, or email addresses.
    • Other attributes like target can be used to control how the link opens (e.g., _blank for a new tab).

Example

HTML
<p>Here's an image of a cat:</p>
<img src="cat.jpg" alt="A cute cat">

<p>Click here to visit our website:</p>
<a href="https://ourwebsite.com">Visit Us</a>

Important considerations:

  • Always provide meaningful alt text for images.
  • Use clear and descriptive link text.
  • Validate links to ensure they work correctly.

By using these element-specific attributes effectively, you can create more accessible and informative web pages.