Can classes be used as a page anchors in a similar way to ids?

Question

Can classes be used as a page anchors in a similar way to ids? For example, can I use this anchor <a href=”.top”> ... </a> to jump to the section of the page containing this paragraph <p class=”top”> … </p>?

Answer

No. Page anchors are used to jump to a very specific region of a page. Specific regions of a page should be uniquely identified with id attributes not class attributes. What’s more, if we try to use a class name as the value of the href attribute within a page anchor, users who click on that link will see a “file not found” error in their browser.

To learn more about page anchors, check out this HTML exercise.

21 Likes

Yeah sure
Another thing if this was implemented then there will be a big problem, let me explain why it will be problem
We learned earlier that we can use classes to style multiple element imagine we have class that style up two h1 tag one in the middle of our web page and the other in the end of our webpage
and then we use that same class to link to one h1 tag in our web page, what do you think will happen ?

That will be impossible for the browser to do , our code will not be executed

1 Like