HTML Attributes
HTML Attributes
HTML attributes are extra pieces of information you add to an element's opening tag to configure its behaviour or appearance.
What Are HTML Attributes?
Attributes provide additional information about an element. They are always written inside the element's opening tag and never appear on the closing tag. For example, the href attribute tells an <a> element where to link, and the src attribute tells an <img> element which file to display.
Syntax
Attributes follow the pattern name="value". The name comes first, then an equals sign, then the value wrapped in quotes. Multiple attributes are separated by spaces.
<a href="https://myinternships.in" target="_blank">Find internships</a>Quoting Rules
Values may use double quotes or single quotes, and double quotes are the most common convention. Quotes are technically optional for simple values that contain no spaces or special characters, but you should always quote attribute values to avoid bugs and keep markup readable.
<!-- Recommended -->
<input type="text" name="city">
<!-- Works but fragile: unquoted value breaks with spaces -->
<input type=text name=city>Boolean Attributes
Some attributes are boolean: their mere presence turns a feature on, and their absence turns it off. You do not need to give them a value. Examples include disabled, checked, required, readonly, selected and autofocus.
<input type="checkbox" checked>
<button disabled>Submit</button>
<input type="email" required>A boolean attribute is true when present in any form (disabled, disabled="" or disabled="disabled"). To make it false, remove the attribute entirely.
Common Global Attributes
| Attribute | Description |
|---|---|
| id | A unique identifier for the element within the document |
| class | One or more space-separated class names used by CSS and JavaScript |
| style | Inline CSS rules applied to the element |
| title | Advisory text shown as a tooltip on hover |
| lang | The language of the element's content, e.g. en or hi |
| hidden | Boolean attribute that hides the element from rendering |
| data-* | Custom author-defined data attributes for scripting |
