Skip to main content

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

html
<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:

javascript
name = "value"

Example:

html
<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:

html
<tagname attribute1="value1" attribute2="value2">Content</tagname>

There are different types of attributes

1. Standard (global)

Can be added to almost any tag:

  • id is a unique identifier of the element;
  • class is a class name for applying styles;
  • style is inline styling;
  • title is a tooltip.

Example:

html
<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, height for <img>;
  • href, target for <a>;
  • type, value, placeholder for <input>.

Attributes without a value

Sometimes just the presence of an attribute is enough:

html
<input type="checkbox" checked>

Here checked means the checkbox is checked by default.


Summary:

ConceptDescription
AttributeAn additional property of a tag
Where it is specifiedInside the opening tag
Formatname="value"
What it is forTo 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.