id and name Attributes

In HTML, the name attribute may be used for the elements a, applet, form, frame, iframe, img, and map. The name attribute and the id attribute may be used in HTML to identify document fragments.

In XML, only id may be used for fragments and there may only be a single id attribute per element. XHTML documents must use id instead of name for identifying document fragments in the aforementioned elements. In fact, the name attribute for these elements has been deprecated in the XHTML 1.0 specification.

Once again, we run into an issue with browser compatibility. Some legacy browsers (namely Netscape 4) do not recognize the id attribute as an identifier for a document fragment (current standards-conformant browsers handle it just fine). If your fragment identifiers must work in Netscape 4, use both name and id. Unfortunately, this is likely to cause validation errors if you are complying to XHTML 1.0 Strict or XHTML 1.1, and therefore you should use only the id attribute when possible for fragment identifiers. The only remaining valid use of the name attribute is for form submission semantics on form control elements like input.

Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.