HTML Attributes

HTML attributes provide additional information about HTML elements. They are written inside the opening tag and are used to control an element’s behavior, provide extra information, connect HTML with CSS and JavaScript, and define how browsers should handle certain elements.

For example:<a href=”https://web4learners.com”>Visit Web4Learners</a>

Here, href is an HTML attribute that specifies the destination of the link.

In this tutorial, you will learn what HTML attributes are, how they work, common attributes, form attributes, input attributes, global attributes, boolean attributes, data attributes, ARIA attributes, and HTML attribute best practices.

What Are HTML Attributes?

HTML attributes are additional properties added to HTML elements. They provide information about an element that cannot be expressed by the element name alone.

  • For example:<img src=”html-logo.png” alt=”HTML Logo”>
  • The <img> element tells the browser that an image should be displayed.
  • The src attribute tells the browser where the image is located, while the alt attribute provides alternative text for the image.

The general structure is:<element attribute=”value”>Content</element>

For example:<p class=”intro”>Welcome to Web4Learners.</p>

Here:

  • <p> is the HTML element.
  • class is the attribute.
  • "intro" is the attribute value.
  • Welcome to Web4Learners. is the content.

Why Are HTML Attributes Used?

HTML attributes are used for many purposes.

They can:

  • Specify the destination of a link.
  • Specify the location of an image.
  • Provide alternative text for images.
  • Add CSS classes.
  • Identify individual elements.
  • Add inline styles.
  • Configure forms.
  • Define input types.
  • Control whether an element is enabled or disabled.
  • Provide additional information.
  • Store custom data.
  • Improve accessibility.

For example:

<button id="loginButton" class="primary-button">
    Login
</button>

The id identifies the button, while the class can be used to style it with CSS or target it with JavaScript.

HTML Attribute Syntax

  • The basic syntax of an HTML attribute is: attribute=”value”
  • An attribute is placed inside the opening tag.
  • For example: <p class=”description”>Learn HTML step by step.</p>
  • The syntax can be understood as: attribute = “value”
  • The attribute name is class, and its value is description.

Where Are Attributes Written?

  • HTML attributes are written inside the opening tag.
  • Correct: <p class=”intro”>Hello World</p>
  • Incorrect: <p>Hello World</p class=”intro”>
  • Attributes belong to the opening tag, not the closing tag.

Attribute Names and Values

Most HTML attributes consist of an attribute name and an attribute value.

For example: <a href=”about.html”>About Us</a>

Here:

PartValue
Elementa
Attributehref
Attribute valueabout.html

The browser uses the attribute value to determine how the element should behave.

HTML Attributes Example

Consider this example: <img src=”computer.jpg” alt=”Laptop Computer” width=”500″>

This element contains three attributes:

  • src specifies the image file.
  • alt provides alternative text.
  • width specifies the image width.

Multiple attributes are separated by spaces.

Attributes and HTML Elements

Attributes are used with HTML elements to provide additional information.

  • For example: <h1 id=”main-heading”>HTML Tutorial</h1>
  • The <h1> element creates a heading, while the id attribute gives the heading a unique identifier.
  • Another example: <p class=”intro”>HTML is easy to learn.</p>
  • The class attribute assigns a class to the paragraph.

Common HTML Attributes

HTML provides many attributes that are frequently used when creating websites.

Some of the most common include:

AttributePurpose
hrefSpecifies a link destination
srcSpecifies a resource location
altProvides alternative text
widthSpecifies width
heightSpecifies height
titleProvides additional information
styleAdds inline CSS
idProvides a unique identifier
classAssigns one or more classes
langSpecifies the language
dirSpecifies text direction

href Attribute

The href attribute specifies the destination of a hyperlink.

It is commonly used with the <a> element.

<a href="https://web4learners.com">
    Visit Web4Learners
</a>

The browser uses the URL in the href attribute when the user clicks the link.

It can also point to another page on the same website:

<a href="/html/html-elements/">
    Learn HTML Elements
</a>

src Attribute

  • The src attribute specifies the location of an external resource.
  • It is commonly used with images: <img src=”html-logo.png” alt=”HTML Logo”>
  • It can also be used with other elements, such as <script>: <script src=”script.js”></script>

alt Attribute

The alt attribute provides alternative text for an image. <img src=”html-logo.png” alt=”HTML Logo”>

  • Alternative text is useful when an image cannot be displayed.
  • It also helps accessibility tools describe images to users.
  • A descriptive alt value is better than a generic value such as:
    • alt=”image”
  • For example:
    • alt=”HTML5 logo”
  • provides more useful information.

width and height Attributes

The width and height attributes can specify the dimensions of an image.

<img
    src="html-logo.png"
    alt="HTML Logo"
    width="300"
    height="200"
>

Here, the image has a specified width of 300 pixels and a height of 200 pixels.

title Attribute

The title attribute provides additional information about an element.

<p title="This is additional information">
    Move your mouse over this text.
</p>

Browsers may display the title value as a tooltip when the user points to the element.

style Attribute

The style attribute allows you to apply CSS directly to an HTML element.

<p style="color: blue;">
    This text is blue.
</p>

The CSS is written directly inside the style attribute.

For larger websites, however, using a stylesheet is usually easier to maintain.

id Attribute

The id attribute provides a unique identifier for an HTML element.

<h1 id="main-heading">
    HTML Tutorial
</h1>

An id can be used with CSS, JavaScript, and links.

For example:

<a href="#main-heading">
    Go to Heading
</a>

An id should normally be unique within the page.

class Attribute

The class attribute assigns one or more classes to an element.

<p class="intro">
    Welcome to HTML.
</p>

CSS can then target the class:

.intro {
    color: blue;
}

The same class can be used on multiple elements.

lang Attribute

The lang attribute specifies the language of an element’s content.

  • For example: <html lang=”en”>
  • This tells browsers and accessibility tools that the document is written in English.
  • Another example is: <p lang=”fr”>Bonjour tout le monde.</p>

dir Attribute

The dir attribute specifies the direction of text.

Common values include:

  • ltr for left-to-right.
  • rtl for right-to-left.
  • auto to let the browser determine the direction.

Example:

<p dir="rtl">
    This text is displayed from right to left.
</p>

HTML Attributes for Links

Several attributes can be used with HTML links.

href

Specifies the destination: <a href=”/html/”>HTML Tutorial</a>

target

Specifies where the linked document should open.

<a href="https://example.com" target="_blank">
    Open Website
</a>

_blank generally opens the link in a new browsing context.

download

The download attribute tells the browser that the linked resource is intended to be downloaded.

<a href="document.pdf" download>
Download PDF
</a>

rel

The rel attribute describes the relationship between the current document and the linked resource.

For example:

<a
    href="https://example.com"
    target="_blank"
    rel="noopener"
>
    Visit Website
</a>

HTML Attributes for Images

Images commonly use the following attributes.

src

Specifies the image source: <img src=”photo.jpg”>

alt

Provides alternative text: <img src=”photo.jpg” alt=”Mountain landscape”>

width

Specifies the width: <img src=”photo.jpg” width=”500″>

height

Specifies the height: <img src=”photo.jpg” height=”300″>

loading

The loading attribute can control when an image is loaded.

For example:

<img
    src="photo.jpg"
    alt="Mountain landscape"
    loading="lazy"
>

lazy allows the browser to defer loading the image until it is closer to the user’s viewport.

HTML Attributes for Forms

HTML form elements use many attributes to control how forms work.

action

The action attribute specifies where form data should be submitted. <form action=”/submit-form”>

method

The method attribute specifies the HTTP method used to submit the form. <form action=”/submit-form” method=”post”>

Common methods include get and post.

name

The name attribute identifies form controls when form data is submitted.

<input type=”text” name=”username”>

value

The value attribute specifies an element’s value.

<input type=”text” value=”Karimulla”>

placeholder

The placeholder attribute provides a short hint inside an input.

<input
    type="text"
    placeholder="Enter your name"
>

required

The required attribute makes a form control required.

<input type=”email” required>

disabled

The disabled attribute prevents interaction with a form control.

<input type=”text” disabled>

readonly

The readonly attribute prevents the user from editing the value.

<input type=”text” value=”HTML” readonly>

checked

The checked attribute selects a checkbox or radio button by default.

<input type=”checkbox” checked>

selected

The selected attribute specifies the initially selected option in a <select> element.

<select>
    <option>HTML</option>
    <option selected>CSS</option>
    <option>JavaScript</option>
</select>

autocomplete

The autocomplete attribute provides the browser with information about whether and how it can offer autocomplete assistance.

<input
    type="email"
    name="email"
    autocomplete="email"
>

HTML Input Attributes

The <input> element supports many useful attributes.

type

Defines the type of input:

<input type=”email”>

Other values include text, password, number, date, checkbox, and more.

id

Provides a unique identifier:

<input type=”text” id=”username”>

name

Provides the name of the form control:

<input type=”text” name=”username”>

value

Specifies an initial value:

<input type=”text” value=”John”>

placeholder

Provides a hint:

<input type=”text” placeholder=”Enter username”>

min and max

Define minimum and maximum permitted values for applicable input types.

<input type=”number” min=”1″ max=”100″>

minlength

Specifies the minimum number of characters for applicable text controls.

<input type=”text” minlength=”5″>

maxlength

Specifies the maximum number of characters.

<input type=”text” maxlength=”20″>

pattern

Specifies a pattern that the input value must match for constraint validation.

<input
type=”text”
pattern=”[A-Za-z]+”
>

required

Makes the input required:

<input type=”email” required>

readonly

Prevents editing while keeping the control usable for submission:

<input type=”text” value=”HTML” readonly>

disabled

Disables the input:

Global HTML Attributes

<input type=”text” disabled>

Global attributes can be used on many HTML elements.

Common global attributes include:

  • id
  • class
  • style
  • title
  • lang
  • hidden
  • tabindex
  • contenteditable
  • draggable
  • spellcheck

For example:

<p
    id="intro"
    class="content"
    title="Introduction"
>
    Welcome to HTML.
</p>

hidden Attribute

The hidden attribute indicates that an element is not currently relevant for presentation.

<p hidden>This content is hidden.</p>

tabindex Attribute

The tabindex attribute controls whether an element can receive keyboard focus and, in some cases, its focus order.

<button tabindex="0">
    Click Me
</button>

It should be used carefully, particularly when creating custom keyboard navigation.

contenteditable Attribute

The contenteditable attribute indicates whether an element’s content can be edited by the user.

<p contenteditable="true">
    You can edit this text.
</p>

draggable Attribute

The draggable attribute indicates whether an element can be dragged.

<div draggable="true">
    Drag this element
</div>

spellcheck Attribute

The spellcheck attribute indicates whether the browser should check spelling and grammar for editable content.

<p contenteditable="true" spellcheck="true">
    Edit this text.
</p>

Boolean Attributes

Boolean attributes represent a true or false state.

The presence of a boolean attribute generally means that the feature is enabled.

For example:

  • <input type=”text” disabled>
  • The input is disabled because the disabled attribute is present.

Common boolean attributes include:

  • disabled
  • required
  • readonly
  • checked
  • selected
  • multiple
  • autofocus
  • hidden

Quotation Marks in Attributes

Attribute values are normally written inside quotation marks.

For example:

<a href=”about.html”>About</a>

  • Double quotation marks are commonly used in HTML.
  • Using quotation marks makes the attribute value clear and helps avoid problems when values contain spaces.

Single Quotes vs Double Quotes

HTML supports both single and double quotation marks for attribute values.

  • Double quotes: <p class=”intro”>Hello</p>
  • Single quotes: <p class=’intro’>Hello</p>

Both are valid.

For consistency, it is generally best to choose one style and use it consistently throughout your project. Double quotes are the common convention in HTML examples.

Case Sensitivity in Attributes

HTML attribute names are generally treated as case-insensitive in HTML syntax, but using lowercase attribute names is the recommended style.

Use: <img src=”photo.jpg” alt=”Photo”>

rather than: <IMG SRC=”photo.jpg” ALT=”Photo”>

Lowercase HTML is easier to read and follows modern HTML conventions.

HTML Attributes and CSS

HTML attributes such as class and id are commonly used to connect HTML elements with CSS.

HTML:

<p class="highlight">
    Important information
</p>

CSS:

.highlight {
    color: blue;
    font-weight: bold;
}

The class attribute allows CSS to target the element.

HTML Attributes and JavaScript

JavaScript can also use HTML attributes to find and manipulate elements.

For example:

<button id="myButton">
    Click Me
</button>

JavaScript can select the element using its ID:

const button = document.getElementById("myButton");

The button can then be modified or used to respond to user actions.

Data Attributes

HTML provides data-* attributes for storing custom data on elements.

For example:

<button data-product="html-course">
    Buy Course
</button>

The data-product attribute stores custom information associated with the button.

JavaScript can access this data.

const button = document.querySelector("button");
console.log(button.dataset.product);

This can be useful when building interactive web pages.

ARIA Attributes

ARIA attributes provide additional accessibility information to assistive technologies.

They are especially useful when building custom interactive interfaces.

Common ARIA attributes include:

aria-label

Provides an accessible name when visible text is not sufficient.

<button aria-label="Close menu">
    X
</button>

aria-hidden

Indicates whether an element should be exposed to assistive technologies.

<span aria-hidden=”true”>★</span>

aria-describedby

Associates an element with another element that provides a description.

<input
    type="password"
    aria-describedby="passwordHelp"
>

<p id="passwordHelp">
    Your password must contain at least 8 characters.
</p>

ARIA should complement semantic HTML rather than replace appropriate native HTML elements.

HTML Attribute Values

Attribute values provide information that the browser uses when processing an element.

For example:

<a href=”contact.html”>Contact</a>

The attribute is:

href

The value is:

contact.html

Different attributes accept different types of values. For example, href accepts a URL or URL reference, while width can specify an applicable numeric dimension for an image.

Common Mistakes With HTML Attributes

Beginners often make a few common mistakes when working with attributes.

Forgetting Quotation Marks

Avoid:

<a href=about.html>About</a>

Prefer:

<a href=”about.html”>About</a>

Putting Attributes in the Closing Tag

Incorrect:

<p>Hello</p class=”intro”>

Correct:

<p class=”intro”>Hello</p>

Using the Wrong Attribute

For example, a hyperlink uses href, not src:

<a href=”about.html”>About</a>

An image uses src:

<img src=”photo.jpg” alt=”Photo”>

Reusing the Same ID

  • Avoid assigning the same unique id to multiple elements.
  • Instead, use a class when multiple elements need the same classification or styling.

HTML Attributes Quiz

Test your knowledge of HTML attributes.

4 thoughts on “HTML Attributes”

  1. Pingback: HTML Comments -
  2. Pingback: HTML Links -

Comments are closed.