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.
Creating bookmarks in HTML is a two-step process. Here are the steps to follow:
id
attribute of the HTML tags, such as <h2>
, <p>
, and so on.<a>
, and the href
attribute.Note: It is compulsory to specify a unique
id
attribute while creating a bookmark using HTML tags is compulsory.
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>
The code example below illustrates the creation and the use of bookmarks:
The explanation of the code is given below:
<a>
are created with the href
attribute specified to take the user to specific web page sections when clicked.<h2>
tags have the specified id
attribute so they can be linked to the <a>
tags.<img>
tag with the id
attribute set to the value of educativelogo
. This is also used as a bookmark.Note: It is important to concatenate
#
with the section’sid
when it is referenced in thehref
attribute of the<a>
tag.
Free Resources