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
| Attribute | Used In | Description |
|---|---|---|
| href | <a> | Specifies the URL of a link |
| src | <img> | Specifies the path to an image |
| alt | <img> | Alternative text for an image |
| title | Any element | Gives extra information as a tooltip |
| id | Any element | Defines a unique identifier |
| class | Any element | Assigns 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.β π§