Search code examples
htmlhyperlinkdeep-linking

How do I create a deeplink to a subsection of a web page?


Does anyone know how I can create a deeplink to a subsection of an individual web page?

Wiki seem to have it cracked but I can't seem to find the answers anywhere on the web.

PS: keep it simple!


Solution

  • Linking to a specific location or element on a page is possible only if the destination contains markup that constitutes a “destination anchor” that you can use in a fragment identifier (starting with #) in a link. The following create such anchors:

    • An id attribute (on any element)
    • A name attribute on an a element

    The former is the modern, preferable way and lets you refer to an element, not just a location (point) or just an inline element. For example, if the destination page http://www.example.com/stuff.html contains

    <div class="section" id="sec7">
    <h2>Foo bar</h2>
    <p>Some text.</p>
    Any other content
    </div>
    

    then you can use a link like

    <a href="http://www.example.com/stuff.html#sec7">Foo bar</a>