← Back to HTML Course | Chapter 1: Introduction & Basics | Lesson 5 of 11

HTML Attributes

Imagine Amit sending a parcel from Delhi to Muzaffarpur. The parcel itself is your HTML tag. But the shipping label containing the address, weight, and delivery instructions? Those are the attributes. Attributes live inside your opening HTML tag and give the browser those vital extra details. Without them, your tags are just blank boxes waiting at the post office. Think of them as the special ginger and cardamom Priya adds to her regular chai to give it that extra kick.
Syntax
markup
<tagname attribute="value">content</tagname>

The href Attribute

This attribute tells your link exactly where to go. It is like Rahul telling a Patna auto-driver his destination so he does not end up in the wrong colony.

Forgetting the href attribute turns an anchor tag into a dead link that does nothing when clicked.

Note:
  • Always use a secure address starting with https when linking to external sites.
  • Accepted attributes:
  • href — absolute URL | relative path | #fragment | mailto:address | tel:number
  • Valid on a, area and link elements
  • target — _self | _blank | _parent | _top
  • rel — noopener | noreferrer | nofollow ...
Warning: If you leave this empty, your users will get stuck on a broken page.

Example: The href Attribute

markup
<a href="https://example.com">Visit our site</a>

The src Attribute

Images cannot magically appear on a screen. The src attribute acts like a home address, guiding the browser to where the image file is actually sitting. A broken src path is one of the most common reasons images fail to display on a live site.

Note:
  • Keep your project organized by putting all images in a folder named assets.
  • Accepted attributes:
  • src — URL of the resource (absolute or relative path)
  • Valid on img, script, iframe, video, audio, source, track, embed and input type=image
  • srcset — comma-separated list of image URLs with width (w) or density (x) descriptors (img and source)
Warning: A tiny spelling mistake in your image path will display a broken image icon.

Example: The src Attribute

markup
<img src="images/photo.jpg">

The alt Attribute

Sometimes the internet in Muzaffarpur acts up, or a screen reader is helping Priya read the page. The alt attribute provides a text backup if the image fails to load.

Alt text also matters for SEO, since search engines can't see an image and rely on this text to understand its content.

Note:
  • Write short, helpful descriptions instead of generic words like banner.
  • Accepted attributes:
  • alt — short text describing the image
  • alt="" — empty value marks a purely decorative image
  • Valid on img, area and input type=image
Warning: Skipping this attribute makes your website difficult to use for visually impaired visitors.

Example: The alt Attribute

markup
<img src="photo.jpg" alt="A red bicycle leaning against a wall">

The title Attribute

Want to show a small tooltip when someone hovers their mouse? The title attribute is like Amit whispering a little hint into the user's ear.

Overusing the title attribute for essential information is a bad idea, since it's invisible on touch devices with no hover.

Note:
  • Keep this text brief and to the point.
  • Accepted values:
  • title — any advisory text, usually shown as a tooltip on hover
  • Global attribute: valid on every element
  • Not a replacement for alt or a visible label
Warning: Mobile users cannot hover, so do not put crucial instructions inside this attribute.

Example: The title Attribute

markup
<p title="Hover to see this tooltip">Hover over this text</p>

Global Attributes: class and id

class and id are global attributes available on almost every HTML element -- class groups multiple elements to share the same styling or behavior, while id uniquely identifies exactly one element on the page for CSS, JavaScript, or in-page links.

Note:
  • Accepted attributes:
  • class — space-separated list of class names
  • id — unique identifier within the document (no spaces)
  • Both are global attributes valid on every element

Example: Global Attributes: class and id

markup
<p class="highlight">First paragraph</p>
<p class="highlight">Second paragraph</p>
<p id="intro">Unique paragraph</p>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to wrap your attribute values in double quotes.
  2. Using uppercase letters for attribute names like SRC instead of lowercase.
  3. Confusing the spelling of src and accidentally writing scr instead.
Chapter Summary
  • Attributes always sit inside the opening HTML tag.
  • They use a simple name="value" format using lowercase letters.
  • The href attribute directs links, while src loads image and script files.
  • The alt attribute provides text descriptions for better web accessibility.
Browser Support

All major web browsers support these standard attributes natively out of the box.

🔒

Chapter Quiz — Complete all 11 topics to unlock

0/11 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.