HTML Attributes
Attributes in HTML provide additional information about elements. They are always included in the opening tag and usually come in name/value pairs like name="value"
.
Basic Syntax
An attribute is written inside the start tag of an element. Here's an example:
<a href="https://example.com">Visit Example</a>
In this case, href
is the attribute name and "https://example.com"
is the value.
Common HTML Attributes
- href: Specifies the URL in anchor tags
- src: Specifies the path to an image
- alt: Provides alternative text for images
- title: Provides additional information when hovered
- id: Assigns a unique identifier
- class: Assigns a class name for CSS styling
- style: Applies inline CSS styles
Attribute Best Practices
- Always use quotes around attribute values.
- Use lowercase for attribute names (HTML is not case-sensitive, but consistency helps).
- Use meaningful
alt
text for images to improve accessibility. - Avoid inline styles; use classes for styling.
Boolean Attributes
Boolean attributes do not require a value. Their mere presence implies truth. Example:
<input type="checkbox" checked>
Conclusion
HTML attributes add power and flexibility to your tags. By understanding and applying them correctly, you can make your pages more functional, accessible, and visually appealing.