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. Every attribute is written as a name paired with a value inside the start tag.
What Are HTML Attributes?
Attributes provide additional information about an element and always live inside the element's opening tag, never on the closing tag. For example, the href attribute tells an <a> element where to link, the src attribute tells an <img> element which file to display, and the type attribute tells an <input> what kind of control to render.
An attribute is written as a name, an equals sign, and a value wrapped in quotes: name="value". You can add many attributes to a single element by separating them with spaces.
Syntax
Attributes follow the pattern name="value". The name comes first, then an equals sign, then the value in quotes. Multiple attributes are separated by spaces and their order does not matter.
<a href="https://myinternships.in" target="_blank">Find internships</a>Example
Run the example below to see several attributes working together on real, visible elements.
<!DOCTYPE html>
<html>
<body>
<h2>Attributes in action</h2>
<a href="https://myinternships.in" title="Opens our homepage">
Visit MyInternships.in
</a>
<p>
<img src="https://picsum.photos/id/1015/200/120"
alt="A calm river between mountains" width="200">
</p>
<label for="city">City</label>
<input type="text" id="city" name="city"
placeholder="e.g. Pune" value="Bengaluru">
</body>
</html>More Examples
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 with no spaces or special characters, but you should always quote values to avoid bugs and keep markup readable.
<!DOCTYPE html>
<html>
<body>
<!-- Recommended: always quote values -->
<input type="text" name="city" value="New Delhi">
<!-- Works but fragile: an unquoted value breaks on the first space -->
<input type=text name=city>
</body>
</html>Boolean Attributes
Some attributes are boolean: their mere presence turns a feature on and their absence turns it off, so they need no value. Examples include disabled, checked, required, readonly, selected and autofocus.
<!DOCTYPE html>
<html>
<body>
<label><input type="checkbox" checked> Subscribe to alerts</label>
<p><input type="email" placeholder="you@example.com" required></p>
<button disabled>Submit (disabled)</button>
</body>
</html>A boolean attribute is true when present in any form (disabled, disabled="" or disabled="disabled"). To make it false, remove the attribute entirely — there is no disabled="false".
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 |
Key Takeaways
- Attributes add information to an element and live only in the opening tag.
- The standard form is name="value"; always wrap values in quotes.
- Boolean attributes such as required and checked are true just by being present.
- Global attributes like id, class and style can be used on any element.
- Order of attributes does not matter, but readability does.
