Skip to content

HTML Attributes Tutorial for Beginners

HTML attributes provide extra information about HTML elements. In this lesson, you will learn what HTML attributes are, how they work, the most commonly used HTML attributes, and best practices for writing clean, SEO-friendly HTML.

What Are HTML Attributes?

HTML attributes are special words added inside the opening tag of an HTML element. They provide extra details about how an element should behave, where it should link, what image it should display, what style it should use, or how it should be identified.

<tagname attribute="value">Content</tagname>

In most cases, an attribute has a name and a value. The attribute name describes the type of information, and the value defines the actual setting.

HTML Attribute Syntax

HTML attributes are written inside the opening tag. Attribute values are usually written inside double quotes.

<a href="https://www.example.com">Visit Example</a>

<img src="html-logo.png" alt="HTML logo" />

<p class="highlight">This paragraph uses a class attribute.</p>

In these examples, href, src, alt, and class are attributes.

Top 10 Most Used HTML Attributes

The following table shows the most commonly used HTML attributes beginners should know. These attributes are used frequently in links, images, forms, styling, accessibility, SEO, and JavaScript-based interactions.

# Attribute Used With Purpose Example
1 href <a> Defines the destination URL of a link. <a href="/tutorials/html/">HTML Tutorial</a>
2 src <img>, <script> Specifies the source file path. <img src="html-logo.png" alt="HTML logo" />
3 alt <img> Provides alternative text for images. <img src="logo.png" alt="Company logo" />
4 id Most HTML elements Defines a unique identifier for an element. <section id="about">About</section>
5 class Most HTML elements Groups elements for CSS styling or JavaScript. <p class="lead">Intro text</p>
6 style Most HTML elements Adds inline CSS styles. <p style="font-weight: bold;">Bold text</p>
7 title Most HTML elements Adds extra tooltip information. <abbr title="HyperText Markup Language | PHPKINGDOM">HTML</abbr>
8 target <a>, <form> Controls where a link or form result opens. <a href="https://example.com" target="_blank">Open Link</a>
9 type <input>, <button> Defines the input or button type. <input type="email" name="email" />
10 name <input>, <textarea>, <select> Identifies form data when submitted. <input type="text" name="username" />

Global HTML Attributes

Global attributes are attributes that can be used on most HTML elements. They are useful for styling, accessibility, scripting, identification, and user interaction.

  • id: gives an element a unique identifier.
  • class: groups elements for styling or scripting.
  • style: adds inline CSS styles.
  • title: provides additional tooltip text.
  • lang: defines the language of content.
  • hidden: hides an element from display.
  • tabindex: controls keyboard focus order.
  • data-*: stores custom data for JavaScript.

HTML Image Attributes

Image attributes help the browser load images and help search engines and screen readers understand what the image represents.

<img
  src="/images/html-attributes-example.png"
  alt="HTML attributes example code"
  width="600"
  height="400"
/>
  • src defines the image file path.
  • alt describes the image for accessibility and SEO.
  • width and height help reduce layout shift.

HTML Form Attributes

Form attributes define how user input is collected, validated, named, and submitted.

<form action="/submit" method="post">
  <label for="email">Email Address</label>
  <input id="email" type="email" name="email" required />

  <button type="submit">Submit</button>
</form>
  • action defines where the form data is submitted.
  • method defines how the data is sent.
  • type defines the input type.
  • name identifies submitted form data.
  • required makes a field mandatory.

Boolean HTML Attributes

Boolean attributes are true when they are present on an element. They do not need a value to work.

<input type="checkbox" checked />

<input type="email" required />

<button disabled>Submit</button>

Common boolean attributes include checked, disabled, required, readonly, selected, and autofocus.

Custom Data Attributes

Custom data attributes start with data-. They are commonly used to store extra information for JavaScript without affecting the visible content.

<button data-user-id="101" data-role="admin">
  View User
</button>

JavaScript can read these values using the dataset property.

const button = document.querySelector("button");

console.log(button.dataset.userId);
console.log(button.dataset.role);

SEO and Accessibility Friendly Attributes

Some HTML attributes are especially useful for SEO, accessibility, and better user experience.

  • alt: helps screen readers and image SEO.
  • title: provides additional tooltip information.
  • lang: identifies the page or content language.
  • aria-label: provides accessible labels when visible text is not enough.
  • rel: describes the relationship between linked pages.
<html lang="en">

<img src="html-guide.png" alt="HTML attributes tutorial for beginners" />

<button aria-label="Open navigation menu">
  ☰
</button>

HTML Attribute Best Practices

  • Use lowercase attribute names for consistency.
  • Wrap attribute values in double quotes.
  • Use descriptive alt text for meaningful images.
  • Use unique values for id attributes.
  • Use reusable class names for styling.
  • Avoid unnecessary inline styles when CSS classes are better.
  • Use rel="noopener noreferrer" with external links opened in a new tab.
  • Use form attributes like required, type, and name correctly.

Common HTML Attribute Mistakes

  • Using duplicate id values on the same page.
  • Forgetting alt text for important images.
  • Using vague image descriptions like image or photo.
  • Opening links with target="_blank" without rel.
  • Using inline style everywhere instead of CSS classes.
  • Forgetting the name attribute in form inputs.

Key Takeaways

  • HTML attributes provide extra information about HTML elements.
  • Attributes are written inside the opening tag.
  • Common attributes include href, src, alt, id, and class.
  • Good attribute usage improves SEO, accessibility, styling, and JavaScript behavior.
  • Always use clean, meaningful, and valid attributes in your HTML pages.

Pro Tip

The alt, href, title, lang, and aria-label attributes can directly improve accessibility and content clarity.