What is an attribute in HTML?
An attribute in HTML is an additional parameter that supplements a tag and specifies its behavior, appearance, or content. It gives the browser extra information about exactly how to display or process the element.
1. The structure of an attribute
An attribute is always written inside the opening tag and has the format:
name="value"Example:
<a href="https://example.com" target="_blank">Go</a>hrefis the attribute name (specifies the link address)"https://example.com"is the attribute valuetarget="_blank"is the second attribute; it opens the link in a new tab
2. Where attributes are used
Almost every HTML tag can have attributes. They set options or provide extra information.
Examples:
<img src="photo.jpg" alt="Photo"> <!-- the picture's path and description -->
<input type="text" placeholder="Name"> <!-- the field's type and hint -->
<button disabled>Submit</button> <!-- the button is disabled -->3. Types of attributes
Global (common): apply to all tags:
| Attribute | Purpose |
|---|---|
id | A unique identifier for the element |
class | A class name for CSS and JS |
style | Inline styling |
title | A tooltip on hover |
hidden | Hides the element |
lang | Specifies the language of the content |
Example:
<p id="intro" class="text" title="Description">Example paragraph</p>Specific: apply to particular tags:
| Tag | Attributes | Purpose |
|---|---|---|
<a> | href, target | The link and how it opens |
<img> | src, alt, width, height | The image and its parameters |
<input> | type, value, placeholder | An input field |
<video> | controls, autoplay, loop | Video controls |
4. Attributes with no value
Some attributes act simply by being present, with no value specified:
<input type="checkbox" checked>
<button disabled>Button</button>Here checked and disabled mean the element is in an active state.
Summary:
| Property | Description |
|---|---|
| Where it's written | In the opening tag |
| Format | name="value" |
| Purpose | Specifies the element's properties |
| Types | Global and specific |
| With no value | Simply indicates a state (checked, disabled) |
In simple terms: An attribute is a "tag setting" that tells the browser: where to get the data, how to show it, or how the element should behave.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.