On this page10 sections
  1. Learning Objectives
  2. 1. How Attributes Are Written
  3. 2. Several Attributes on One Element
  4. 3. The title Attribute
  5. 4. id and class: Names for Elements
  6. 5. The style Attribute
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 004: Attributes

Category1: Getting Started
Lecture004 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

A tag says what an element is. An attribute adds extra detail: where a link goes, which image to show, or a name you can use later to style the element.


Learning Objectives

  • Write attributes with the correct name="value" syntax
  • Use the common attributes href, src, alt, title, id, class and style
  • Explain the difference between id and class

1. How Attributes Are Written

Attributes always go in the opening tag, as name="value".

output
<a href="https://example.com">Visit</a>
   │    │
   │    └── value (in quotes)
   └─────── attribute name

Example

html
<a href="https://developer.mozilla.org">Read the MDN docs</a>

href tells the link where to go. Without it, the text looks like a link but goes nowhere.


2. Several Attributes on One Element

Separate them with spaces. The order doesn't matter.

Example

html
<img src="https://picsum.photos/id/1080/300/180" alt="A bowl of strawberries" width="300">
AttributePurpose
srcThe image file to show
altText read by screen readers, and shown if the image fails to load
widthThe display width in pixels

3. The title Attribute

title adds a small tooltip when the mouse rests on an element.

Example

html
<p title="Hypertext Markup Language">Hover over this sentence about HTML.</p>

4. id and class: Names for Elements

Both give an element a name, so CSS and JavaScript can find it.

  • id: unique. Only one element on the page may have a given id.
  • class: shared. Many elements can have the same class.

Example

html
<h1 id="page-title">Daily Deals</h1>
<p class="deal">Shoes: 20% off</p>
<p class="deal">Bags: 15% off</p>
<p>Prices include tax.</p>

Nothing looks different yet. Classes and ids pay off in the CSS course, where one rule can style every .deal at once.


5. The style Attribute

style adds CSS directly to one element. It's handy for a quick test.

Example

html
<p style="color: crimson; font-size: 20px;">Sale ends tonight!</p>

For real pages, put styles in CSS rules instead. You'll learn how in CSS Lecture 001.


Common Beginner Mistakes

1. Putting attributes in the closing tag. </a href="..."> doesn't work. Attributes belong in the opening tag only.

2. Missing quotes. alt=A red car breaks at the first space. Always quote values: alt="A red car".

3. Reusing an id. Two elements with id="title" is invalid. Use a class for repeated names.

4. Skipping alt on images. Screen-reader users hear nothing useful, and search engines don't know what the image shows.


Practice Questions

  1. Where in an element do attributes go?
  2. Write a link with the text "Search" that goes to https://www.google.com.
  3. What is the difference between id and class?
  4. Why should every image have an alt attribute?
  5. What does the title attribute do?
  6. Make a paragraph whose text is blue using the style attribute.

Practice Answers

  1. In the opening tag, after the tag name.
  2. <a href="https://www.google.com">Search</a>
  3. An id must be unique on the page; a class can be shared by many elements.
  4. It describes the image for screen readers and appears if the image can't load.
  5. It shows a tooltip when the mouse hovers over the element.
  6. <p style="color: blue;">Blue text</p>

Key Takeaways

  • Attributes add details to an element: name="value" in the opening tag.
  • href for links, src + alt for images, title for tooltips.
  • id = one unique element; class = a group of elements.
  • style works, but CSS rules are the better long-term home for styles.