HTML Attributes
HTML attributes provide additional information about HTML elements. They are always specified in the opening tag and usually come in name/value pairs like name="value".
Attribute Syntax
Attributes are added to the opening tag of an element:
<element attribute="value">Content</element>
Multiple attributes can be added:
<element attribute1="value1" attribute2="value2">Content</element>
Global Attributes
Global attributes can be used on any HTML element:
id: Unique identifier for the elementclass: Class name for styling (can be multiple classes separated by spaces)style: Inline CSS stylestitle: Extra information about the element (shown as tooltip)lang: Language of the element's contentdata-*: Custom data attributes (e.g.,data-user-id="123")
Common Element-Specific Attributes
Links (<a>)
href: URL of the linktarget: Where to open the link (_blank,_self,_parent,_top)rel: Relationship between current document and linked document
<a href="https://example.com" target="_blank" rel="noopener">Visit Example</a>
Images (<img>)
src: Source URL of the imagealt: Alternative text for the image (important for accessibility)widthandheight: Dimensions of the imageloading: Loading behavior (lazyoreager)
<img src="image.jpg" alt="A beautiful landscape" width="300" height="200" loading="lazy">
Forms (<form>)
action: URL where form data is sentmethod: HTTP method (GETorPOST)enctype: Encoding type for form data
<form action="/submit" method="POST" enctype="multipart/form-data">
Input Fields (<input>)
type: Type of input (text, password, email, number, etc.)name: Name of the input fieldvalue: Default valueplaceholder: Hint text shown when field is emptyrequired: Makes the field requireddisabled: Disables the input fieldreadonly: Makes the field read-only
<input type="email" name="user_email" placeholder="Enter your email" required>
Buttons (<button>)
type: Type of button (submit,reset,button)disabled: Disables the button
<button type="submit" disabled>Submit Form</button>
Select Elements (<select>)
name: Name of the select fieldmultiple: Allows multiple selectionssize: Number of visible options
<select name="country" multiple size="3">
<option value="us">United States</option>
<option value="ca">Canada</option>
</select>
Textarea (<textarea>)
name: Name of the textarearowsandcols: Visible dimensionsplaceholder: Hint textmaxlength: Maximum number of characters
<textarea name="message" rows="4" cols="50" placeholder="Enter your message" maxlength="500"></textarea>
Boolean Attributes
Some attributes are boolean - they don't need a value. Their presence indicates true, absence indicates false:
<input type="checkbox" checked>
<input type="text" disabled>
Custom Data Attributes
HTML5 allows custom data attributes using the data- prefix:
<div data-user-id="123" data-role="admin">User Info</div>
These can be accessed in JavaScript using element.dataset.userId.
Best Practices
- Always use lowercase for attribute names
- Always quote attribute values
- Use meaningful
idandclassnames - Provide
alttext for images - Use semantic attributes appropriately
- Avoid inline styles when possible (prefer CSS classes)
Accessibility Considerations
- Use
altattributes on images - Provide
titleattributes for additional context - Use
langattribute for content in different languages - Ensure sufficient color contrast for
titletooltips
