What is an attribute in HTML?
An attribute in HTML is an additional characteristic of a tag that specifies exactly how an element should look, work, or behave.
If a tag is "what this is", then an attribute is "what it is like, where it is, what it does".
Example
<img src="photo.jpg" alt="My photo" width="300">Here:
<img>is the image tag,src="photo.jpg"is the path to the image file,alt="My photo"is the alternative text if the image fails to load,width="300"is the width of the image in pixels.
Everything that goes inside the opening tag after its name is an attribute.
The structure of an attribute
An attribute consists of three parts:
name = "value"Example:
<a href="https://example.com">Link</a>- attribute name:
href - value:
"https://example.com"
Where attributes are written
Attributes are always specified inside the opening tag:
<tagname attribute1="value1" attribute2="value2">Content</tagname>There are different types of attributes
1. Standard (global)
Can be added to almost any tag:
idis a unique identifier of the element;classis a class name for applying styles;styleis inline styling;titleis a tooltip.
Example:
<p id="intro" class="text" title="Description">Example paragraph</p>2. Specific (depend on the tag)
Some attributes only apply to certain tags:
src,alt,width,heightfor<img>;href,targetfor<a>;type,value,placeholderfor<input>.
Attributes without a value
Sometimes just the presence of an attribute is enough:
<input type="checkbox" checked>Here checked means the checkbox is checked by default.
Summary:
| Concept | Description |
|---|---|
| Attribute | An additional property of a tag |
| Where it is specified | Inside the opening tag |
| Format | name="value" |
| What it is for | To specify the behavior, appearance, or content of an element |
In simple terms: An attribute is an addition to a tag that explains to the browser exactly which element to show and how it should work. Without attributes, the page would look the same everywhere, without individual settings.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.