Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > HTML/XHTML

  • Print
  • + Share This
This chapter is from the book

Naming Elements

You can give your HTML elements either a unique name or one that identifies them as belonging to a particular class. You can then apply styles to all elements with a given name.

To name unique elements:

Within the opening tag of the element, type id="name", where name uniquely identifies the element.

To name groups of elements:

Within the opening tag of the element, type class="name", where name is the identifying name of the class.


  • Each id in an (X)HTML document must be unique. In other words, no two elements can be named with the same id.

  • More than one element may belong to, and thus be marked with, the same class.

  • For information about applying styles to an element with a particular id or class, consult Selecting Elements by Class or ID on page 137.

  • The class and id attributes may be added to most (X)HTML elements but are particularly useful with the div and span elements (see pages 68–69).

  • The id attribute automatically turns the element into an anchor, to which you can direct a link. For more details, consult Creating Anchors on page 120.

  • Finally, the id attribute can also be used to identify elements that will be affected by a scripting language, such as JavaScript.

Find extra tips, the source code for examples, and more at

  • + Share This
  • 🔖 Save To Your Account