ID attributes
To add style and action to our web pages, we need to have a way of indicating which element is to be styled or upon which element to act. We might also wish to create a hyperlink to a particular element. How is this accomplished?
An element can be given a unique identifier using the ID attribute (id
) where the valueSpecific information for an attribute. is the identifier. The ID attribute is a global attributeAn attribute that can be added to any element.. There is a lot of freedom in choosing values, other than the following restrictions:
- An ID value must contain at least one character.
- An ID value cannot contain a space.
- An ID value cannot be the same as the value of another ID in the same document.
The ID attribute can also be used to create a destination anchorA source or destination of a hyperlink. to specify which element is the destination of a hyperlink. If the destination is on the same page as the source anchor, the value is simply a number sign followed by the ID value, as in the example below, where clicking on the hyperlink will bring the reader to the third paragraph.
<a href="#thisid">Click on this hyperlink to read important news.</a>
<p>Some stuff here.</p>
<p>Some other stuff here.</p>
<p id="thisid">Important news.</p>
For a destination on another page, the URLA way of specifying a web page for a link. for the page is followed by a number sign followed by the ID value, such as href="http://something/folder.html#thisid"
.
Caution!
Notice that thisid
appears twice, once with a number sign and once without. It appears without a number sign when it is being used as a value for the ID attribute. It appears with a number sign when it is being used as a value for the hyperlink reference attributeAn attribute used to specify an address for a hyperlink., that is, as a destination anchor.