CHAPTER 07 · HTML

Attributes: give an element useful extra information

An element tells the browser what a part of the page is. Attributes give that element more information or settings. In this chapter, you will read name-and-value pairs, quote them correctly and learn why not every attribute works like ordinary text.

Download example files (ZIP)

What you will learn

  • Write attributes in opening tags.
  • Separate attribute names, values and visible content.
  • Use quotation marks and more than one attribute safely.
  • Recognise common global and element-specific attributes.
  • Understand boolean attributes and the limits of title tooltips.

Start with a familiar label

A classroom notice can identify its language, topic or destination. These details help people use the notice without becoming the main message. An attribute plays a similar supporting role for an element.

HTML fragment — part of a document
<p lang="en" title="Session reminder">Bring a notebook.</p>

The element is a paragraph. Lang identifies its language. Title supplies extra advisory text that some browsers may display when a pointer rests over the element. The visible sentence is still Bring a notebook.

The advisory title attribute is different from the title element in head. One is an attribute on an element; the other names the document.

Read the syntax one piece at a time

Syntax means the writing pattern. A usual attribute pattern is name="value" inside a start tag.

PieceExampleJob
Element namepIdentify a paragraph
Attribute namelangIdentify the setting
Equals sign=Connect the name with its value
Quoted value"en"Supply the setting's value
Content after the start tagBring a notebook.Supply the paragraph's words

Use spaces between separate attributes. A normal end tag is just the end tag; it does not repeat the attributes.

HTML fragment — part of a document
<p lang="en" title="Session reminder">Bring a notebook.</p>

Attribute order usually does not change the meaning here. Writing title before lang would still identify the same two settings. A consistent order can still help you read your own code.

One element, extra settings, readable content
  1. 1p: paragraph element
  2. 2lang: attribute name
  3. 3en: the language value
  4. 4Sentence after >: content

Ordinary attributes live in the opening tag. Boolean attributes have separate presence/absence rules.

Quote values, especially when they contain spaces

Double quotes and single quotes can both enclose an HTML attribute value. We normally use double quotes. If a value contains an apostrophe, it can stay inside double quotes:

HTML fragment — part of a document
<p title="Today's practice task">Explain one paragraph.</p>

For a value containing double quotes, you can use single quotes outside:

HTML fragment — part of a document
<p title='Read the "Setup" chapter first'>Start with the saved example.</p>

Do not start with a double quote and end with a single quote. Curly quotation marks copied from a word processor are not substitutes for the straight quote characters used to delimit HTML values.

HTML permits some unquoted attribute values, but their allowed characters are restricted. Quoting all our ordinary values avoids a common error: a value containing spaces being split into unintended attributes. Later, character entities will show how to represent characters that need escaping.

Common attributes and what they are for

Some attributes are global, meaning they are generally available across HTML elements. Others belong to particular kinds of elements. Being able to write a name does not mean it is valid or meaningful on every element.

AttributeTypical useImportant point
langMain language of contentIt does not translate the words
idA unique identifier within the documentDo not repeat the same ID for different elements
classGroup elements for styling or scriptsA class name can be shared; it does not style itself
titleAdvisory informationDo not hide essential instructions only in a tooltip
hrefDestination of a linkUsed on suitable elements such as a
srcLocation of an image or other resourceUsed where a resource source is supported
altText alternative for an imageChoose its text for the image's purpose
width and heightDimensions on elements that support themNot a universal sizing attribute for every element
styleCSS declarations on an elementIts value uses CSS rules, not another HTML tag
data-*Author-defined informationIt does not automatically create application behaviour

We will study links, images, classes and IDs in their own chapters. This table helps you recognise their attributes without pretending that one introductory example teaches every rule.

Complete example: identify a practice task

practice-attributes.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Practice Task Details</title>
</head>
<body>
  <h1 id="practice-title">Today's practice task</h1>
  <p class="task-note" data-topic="html">Explain one HTML paragraph to a friend.</p>
  <p title="Save your file before checking it">Save first, then refresh the browser.</p>
</body>
</html>
Browser previewDocument title: Practice Task Details

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The h1 has an ID, and the first paragraph has a class and a custom data attribute. These attributes are in the source but do not automatically print as extra words in the preview. Without CSS using task-note, the class does not change the paragraph's appearance.

Data-topic stores a piece of author-defined information on that element. A later script could read it, but no such script is present. The second paragraph keeps its essential instruction visible even though it also has advisory title text.

Boolean attributes: presence can be the setting

A boolean attribute is an HTML setting whose presence means true and whose absence means false. True and false mean yes and no for that setting.

Disabled on a button is an example. A disabled button cannot be activated normally. Removing the attribute enables the button again, unless another rule such as a disabled parent fieldset applies.

HTML fragment — part of a document
<button type="button" disabled>Registration closed</button>
<button type="button">View session details</button>

Do not write disabled="false" to enable a button. The attribute is still present, so the button remains disabled. That value is not the conforming way to spell a false boolean setting. Remove disabled instead.

Not every attribute is boolean. For example, some attributes accept a fixed set of words, and some data attributes merely contain your own text. Learn the rule for each setting instead of treating every value named false as a universal off switch.

A second complete example: two button states

button-states.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Button State Practice</title>
</head>
<body>
  <h1>Two button states</h1>
  <p>The first button is disabled. The second is enabled but has no action attached.</p>
  <button type="button" disabled>Registration closed</button>
  <button type="button">View session details</button>
</body>
</html>
Browser previewDocument title: Button State Practice

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

This is a state demonstration, not a working registration system. Type button prevents an unintended form-submit default if the button is later placed in a form. We have not attached a script or navigation action to the second button, so activating it has no application effect.

Compare the disabled and enabled buttons in the preview. Their appearance can vary with the browser. Inspect the source to understand the rule rather than depending only on a grey shade.

Case, duplicate attributes and spelling

Built-in attribute names are case-insensitive in HTML syntax, but we write them in lowercase. Values follow their own rules. A file path or an ID can be case-sensitive; do not change its spelling casually.

Do not repeat the same attribute name on one start tag. If you need several CSS classes, put space-separated class names in one class value. Do not add a second class attribute as a shortcut.

A misspelled attribute may have no intended effect while the page still displays. Check the attribute name, its allowed element and its value when a setting appears not to work.

Problem and solution: instructions only appear on hover

Problem: a teacher puts the submission deadline only in a title attribute. A student using a touch screen or keyboard does not reliably discover it.

Solution: write the deadline as visible page content. Use title only as optional advisory information when appropriate. Essential instructions should not depend on one browser's tooltip behaviour or a particular input device.

Common mistakes and useful tips

  • Putting attributes in closing tags: keep ordinary settings in the start tag.
  • Leaving a multiword value unquoted: use matching straight quotation marks.
  • Using class as if it creates a design by itself: CSS must supply that design.
  • Expecting disabled="false" to enable a button: remove the boolean attribute.
  • Repeating an attribute name on one element: combine values only where its rules allow that.
  • Hiding essential information in title: keep instructions visible and accessible.
  • Treating hidden or data attributes as secret storage: visitors can inspect delivered HTML.

Practice questions

  1. Identify the attribute names and values in <p lang="en" title="Read carefully">Check the date.</p>.
  2. Why should the value Session reminder be quoted?
  3. Does class="task-note" make a paragraph green without CSS?
  4. A button has disabled="false". How should you actually enable it?
  5. Why should a deadline be visible rather than stored only in title?
  6. Add an ID to one heading and the same class to two paragraphs in your own complete page. Keep both paragraphs readable without a script.
Check your practice answers
  1. Lang has the value en; title has the value Read carefully. Check the date. is the paragraph's content.
  2. It contains a space. Quotes keep it together as one value and make the intended boundary clear.
  3. No. The name identifies a class; styling rules must define any appearance change.
  4. Remove disabled from the start tag. A present boolean attribute is not turned off by the text false.
  5. Tooltips are not reliably available to every reader, especially on touch or through all keyboard interactions. Essential information belongs in accessible content.
  6. Use one unique ID for the heading and one shared class name on both paragraphs. The text should remain useful before any CSS or JavaScript is added.

Interview questions

What is an HTML attribute?

It provides information or a setting on an element, usually as a name-and-value pair in the opening tag. Some attributes have special rules, including boolean attributes.

Can every attribute be used meaningfully on every element?

No. Global attributes are generally available, while others are allowed only on particular elements. Check both the element and the attribute's value rules.

How do boolean attributes work?

Presence means true and absence means false. For disabled, remove the attribute to represent false; the string false does not undo its presence.

How does the title attribute differ from the title element?

The title element in head names the document. A title attribute supplies advisory information about its element and must not be the only source of essential instructions.

Quick revision

  • Attributes belong in opening tags and have their own value rules.
  • Use matching quotes and separate attributes with spaces.
  • Class, ID and data attributes do not automatically create a visual design.
  • Boolean attributes depend on presence, not an ordinary true/false string switch.
  • Learn element-specific rules and keep essential instructions visible.
  • Attribute values in delivered HTML are not secret.