On this page10 sections
Lecture 004: Attributes
| Category | 1: Getting Started |
| Lecture | 004 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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,classandstyle - Explain the difference between
idandclass
1. How Attributes Are Written
Attributes always go in the opening tag, as name="value".
<a href="https://example.com">Visit</a>
│ │
│ └── value (in quotes)
└─────── attribute nameExample
<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
<img src="https://picsum.photos/id/1080/300/180" alt="A bowl of strawberries" width="300">| Attribute | Purpose |
|---|---|
src | The image file to show |
alt | Text read by screen readers, and shown if the image fails to load |
width | The display width in pixels |
3. The title Attribute
title adds a small tooltip when the mouse rests on an element.
Example
<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
<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
<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
- Where in an element do attributes go?
- Write a link with the text "Search" that goes to
https://www.google.com. - What is the difference between
idandclass? - Why should every image have an
altattribute? - What does the
titleattribute do? - Make a paragraph whose text is blue using the
styleattribute.
Practice Answers
- In the opening tag, after the tag name.
<a href="https://www.google.com">Search</a>- An
idmust be unique on the page; aclasscan be shared by many elements. - It describes the image for screen readers and appears if the image can't load.
- It shows a tooltip when the mouse hovers over the element.
<p style="color: blue;">Blue text</p>
Key Takeaways
- Attributes add details to an element:
name="value"in the opening tag. hreffor links,src+altfor images,titlefor tooltips.id= one unique element;class= a group of elements.styleworks, but CSS rules are the better long-term home for styles.