Skip to main content

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:

javascript
name="value"

Example:

html
<a href="https://example.com" target="_blank">Go</a>
  • href is the attribute name (specifies the link address)
  • "https://example.com" is the attribute value
  • target="_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:

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

AttributePurpose
idA unique identifier for the element
classA class name for CSS and JS
styleInline styling
titleA tooltip on hover
hiddenHides the element
langSpecifies the language of the content

Example:

html
<p id="intro" class="text" title="Description">Example paragraph</p>

Specific: apply to particular tags:

TagAttributesPurpose
<a>href, targetThe link and how it opens
<img>src, alt, width, heightThe image and its parameters
<input>type, value, placeholderAn input field
<video>controls, autoplay, loopVideo controls

4. Attributes with no value

Some attributes act simply by being present, with no value specified:

html
<input type="checkbox" checked> <button disabled>Button</button>

Here checked and disabled mean the element is in an active state.


Summary:

PropertyDescription
Where it's writtenIn the opening tag
Formatname="value"
PurposeSpecifies the element's properties
TypesGlobal and specific
With no valueSimply 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 ready
Premium

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