0

I want to add a #something to an HTML page.

An example of this would be #History on http://en.wikipedia.org/wiki/Stack_Overflow_(website).

How can I do it?

Timeout
  • 7,759
  • 26
  • 38

2 Answers2

4

If you inspect the HTML on that page you'll see how it's being done.

The # sign in an anchor tag's href will tell the browser to move to the element that has a matching ID (where ID = everything that comes after the # sign).

For Example:

<a href="#MyLocation">My Link</a>

<div id="MyLocation">The anchor destination</div>

You can also include it at the end of your URL and it will take the user directly to the destination on page load.

http://en.wikipedia.org/wiki/Stack_Overflow_(website)#History

Timeout
  • 7,759
  • 26
  • 38
0

Use a name or id attribute on an <a>nchor:

<a name="foo"></a>
<h1>Foo</h1>

<a id="bar"></a>
<h1>Bar</h1>

Pointing your link to http://www.example.com/#foo or http://www.example.com/#bar will scroll the browser down to the respective <a>.

The specification of HTML4 lists name first and id second. (Notice the # in the link? You can also look at the sourcecode of the website!)

When the name or id attributes of the A element are set, the element defines an anchor that may be the destination of other links.

According to HTML Anchors with 'name' or 'id'?, in HTML5 it can be id="foo" or name="foo" with id="foo" having precedence.

Community
  • 1
  • 1
simbabque
  • 53,749
  • 8
  • 73
  • 136