HTML Attributes

πŸ” What Are HTML Attributes?

HTML attributes provide additional information about elements. They are always included in the opening tag and are written as name="value" pairs.

>>β€œAttributes bring elements to life by giving them identity, style, and behavior.” 🌟

βš™οΈ Basic Syntax

An attribute is placed inside the opening tag of an HTML element:

Attribute Syntax

<elementName attributeName="attributeValue">Content</elementName>

For example:

Example

<a href="https://example.com">Visit Site</a>

Note

πŸ’‘ An element can have multiple attributes, separated by spaces.

πŸ“‹ Common HTML Attributes

AttributeUsed InDescription
href<a>Specifies the URL of a link
src<img>Specifies the path to an image
alt<img>Alternative text for an image
titleAny elementGives extra information as a tooltip
idAny elementDefines a unique identifier
classAny elementAssigns a class name (used for CSS/JS)

πŸ§ͺ Example: Attributes in Action

HTML Attributes Example

<img src="logo.png" alt="Company Logo" title="Our Brand" />
<a href="https://example.com" target="_blank" rel="noopener">Visit Us</a>
<p id="intro" class="highlight">Welcome to our site!</p>

πŸ” Special Attributes

  • target="_blank" – Opens the link in a new tab
  • rel="noopener noreferrer" – Improves security for external links
  • disabled – Disables form elements
  • readonly – Makes input fields non-editable
  • required – Marks form fields as mandatory

Note

🧠 Some attributes are boolean attributes, meaning they don’t require a value. Example: <input disabled>

🧼 Best Practices

  • βœ… Use double quotes around attribute values
  • 🧾 Keep attribute names lowercase (HTML is not case-sensitive but best practices matter)
  • πŸ›  Use semantic attributes like alt and title for accessibility

πŸ“š Learn More

>>β€œAttributes turn plain elements into powerful tools.” πŸ”§