HTML Attributes are special properties that provide additional information about HTML elements.
They are written inside the opening tag and consist of a name-value pair, typically formatted as attribute="value".
Attributes modify element behavior, appearance, or provide metadata that browsers and other tools can use.
Common Global Attributes:
id: Provides a unique identifier for an elementclass: Assigns CSS classes for stylingstyle: Applies inline CSS stylestitle: Adds tooltip text on hoverlang: Specifies the language of element contentdata-*: Creates custom data attributes
Element-Specific Attributes: Different HTML elements have specialized attributes:
<a>useshreffor links andtargetfor opening behavior<img>usessrcfor image source andaltfor accessibility text<input>usestype,name,placeholder, andrequired<form>usesactionandmethod
Key Attribute Types Shown:
- Structural:
id,classfor element identification and styling - Content:
src,alt,hreffor media and links - Form:
type,name,placeholder,required,min,maxfor input validation - Behavior:
target="_blank"opens links in new tabs - Custom:
data-*attributes store custom information - Table:
colspan,rowspancontrol cell spanning
Best Practices:
- Always quote attribute values
- Use semantic, descriptive names
- Include
alttext for images (accessibility) - Validate user inputs with appropriate attributes
- Use
idfor unique elements,classfor reusable styles
Attributes make HTML elements interactive, accessible, and properly structured for both browsers and assistive technologies.