0

In the navigation bar of a website I'm working on, I have the following code:

<a href="#">Menu</a>

For some reason, clicking on then 'Menu' link jumps me back to the top of the page. I'm not sure why this happens.

mdf427
  • 27
  • 3

3 Answers3

1

Use this :

<a href="javascript:void(0);">Menu</a>

Linux Packet
  • 186
  • 1
  • 1
  • 7
0

The # specifies an anchor. You can name an <a> tag (the "a" stands for "anchor"), and then you can use a link like #section1 to jump to the a tag named "section1".

Because you didn't specify an anchor to jump to, it goes to the top of the page.

You can use this to link to parts of other pages as well. The link <a href="anotherpage.htm#someSectionOfAnotherPage" will link to the a tag named someSectionOfAnotherPage on the page anotherPage.htm.

James Westman
  • 2,680
  • 1
  • 15
  • 20
0

# used in href attribute of the <a> tag specifies the id of an HTML element to where the page needs to scroll to.

i.e., clicking on the link <a href="#abc">Goto abc</a> will scroll the page to display the HTML element with id="abc" on top.

So, when we give # only (no id specified), it will take us to the top of the page.

See the demo

See this question also

Community
  • 1
  • 1
Anoop S
  • 141
  • 1
  • 12