How to create bookmarks in HTML
Overview
Developers use HTML bookmarks to control the flow of their web pages and direct the traffic on their web pages to relevant sections by linking them together or to other relevant web pages. They are helpful when the websites have a lot of content. This ensures a better user experience.
Steps
Creating bookmarks in HTML is a two-step process. Here are the steps to follow:
- Set a bookmark using the
idattribute of the HTML tags, such as<h2>,<p>, and so on. - Link the HTML tag created, using an anchor tag,
<a>, and thehrefattribute.
Note: It is compulsory to specify a unique
idattribute while creating a bookmark using HTML tags is compulsory.
Syntax
Here is the syntax needed to create bookmarks in HTML:
//creating a bookmark
<element id='mybookmark'>creates bookmark</element>
//creating an <a> tag to refer to the bookmark
<a href='#mybookmark'>refers to the created bookmark</a>
Code
The code example below illustrates the creation and the use of bookmarks:
Explanation
The explanation of the code is given below:
- Lines 7–10: In these lines, anchor tags
<a>are created with thehrefattribute specified to take the user to specific web page sections when clicked. - Lines 13, 25, and 37: These lines of code contain the content of the web page made using HTML tags. The
<h2>tags have the specifiedidattribute so they can be linked to the<a>tags. - Line 49: This line contains an
<img>tag with theidattribute set to the value ofeducativelogo. This is also used as a bookmark.
Note: It is important to concatenate
#with the section’sidwhen it is referenced in thehrefattribute of the<a>tag.
Free Resources