HTML chapters

Chapter 4 of 17

Attributes, identifiers and data

Try this in our HTML Compiler →

The idea

HTML has no variable declarations, reassignment or function scope. Attributes describe elements. An id identifies a single element in a document; a class groups elements for styling; data-* attributes hold text that scripts can read. None of these is a substitute for a JavaScript variable.

Worked example

<article id="lesson-one" class="lesson" data-level="beginner">
  <h2>Getting started</h2>
  <p>Practice a little every day.</p>
</article>

What to check

The data-level value is a string attached to this article. It does not perform calculations. Repeating an id makes links and element lookup ambiguous.

Practice task

Add another article with a different id and the same class. Give it a data-level of intermediate.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on