Attributes & global attributes
6 min lesson
- An attribute is extra information inside an opening tag, written as name="value", like href="page.html".
- Attributes change how an element behaves, for example where a link goes or what an image shows.
- Some attributes are specific to one element, like href on <a> or src on <img>.
- Global attributes work on any element, like id, class, title and lang.
- id must be unique on the page; class can repeat and is used to group elements for styling.
- This builds on Block vs inline elements: attributes do not change display type but add meaning and hooks.
- The example gives a paragraph an id and a class, and links a page with href; you will see the text and a working link.
- Next, Comments, whitespace & HTML entities covers the small text pieces you write between tags.
- Write attributes as name="value" inside the opening tag only.
- Use id for one unique element and class for groups.
- Quote every value, even when it looks optional.
- Check spelling: a misspelled attribute is silently ignored.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attributes</title>
</head>
<body>
<!-- id is unique; class can be shared by many elements -->
<p id="intro" class="note">This paragraph has an id and a class.</p>
<!-- href tells the link where to go -->
<a href="https://example.com" title="Go to example">Visit example</a>
<!-- lang is a global attribute; here it marks a Spanish word -->
<p>The word <span lang="es">hola</span> means hello.</p>
</body>
</html>What happens: A page showing the paragraph "This paragraph has an id and a class.", the link "Visit example" (with tooltip "Go to example" on hover), and the paragraph "The word hola means hello."
Breaking it down
id attribute
id="intro"Gives the element a unique name so CSS, JavaScript or a link can find exactly this one. Two elements must never share an id.
class attribute
class="note"Labels the element as part of a group. Many elements can share the same class, and CSS can style them all at once.
href attribute
href="https://example.com"Tells a link where to go when clicked. Without href, the <a> tag is not a working link.
title attribute
title="Go to example"Shows a small tooltip on hover. It adds helpful text but does not change layout.
lang attribute
lang="es"Marks the language of the content. Screen readers use it to pronounce words correctly.
Useful global attributes
| Attribute | Purpose |
|---|---|
| id | A unique name for one element |
| class | A group name shared by many elements |
| title | A tooltip shown on hover |
| lang | The language of the content |
| style | Inline CSS for quick tests only |
A helpful analogy
Attributes are like sticky notes on a box: the box stays the same, but the note tells you where it goes, what it contains or who owns it. Global attributes are notes you can stick on any box.
Nesting rules
- 1.Write attributes only in the opening tag, never in the closing tag.
- 2.Keep one space between attributes; do not run them together.
Common mistakes
Reusing the same id on two elements
Avoid
<p id="intro">A</p><p id="intro">B</p>Use instead
<p id="intro">A</p><p id="summary">B</p>An id must be unique. Repeating it breaks links and JavaScript that look up the element by id.
Leaving out the quotes
Avoid
<a href=https://example.com>Go</a>Use instead
<a href="https://example.com">Go</a>Unquoted values often work but can break as soon as the value contains a space or a special character.
Quick tips
- •Use the Elements panel to see all attributes the browser actually applied to an element.
- •If a style or script does nothing, check for a typo in the class or id name.