44

I created a HTML page with a number of tables with headers like this: Content, Main_Page, Document, Expenses, etc.

I want to create a link for the top of the page. When I click that link it should go to the specific section. So I use the below code to map the content. But it's not working for me.

<a href="#Content">Content Section</a>
Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
romi
  • 551
  • 1
  • 8
  • 16
  • do you have your area marked with `` – mattematico Sep 12 '12 at 10:35
  • 5
    I don't understand how this is not a real question. To me the question and answer makes perfect sense. – Max Goodridge Jun 06 '16 at 10:46
  • 4
    @MaxGoodridge - I agree, it's obviously a real question. Even the title is great for this question. Perhaps the closers could suggest an edit so that they would be happy to call this a "real question". That should be easy for them because if it's not a "real question", it is really close enough. OK, so let's un-close it. – Kevin Fegan Oct 10 '17 at 07:45
  • 1
    @KevinFegan Thank you - anyone do feel free to submit changes required to un-close this! – Max Goodridge Oct 14 '17 at 11:06

4 Answers4

37

You need to create an anchor for the link. The modern way of doing this is to give the appropriate element an id="Content" attribute. The older way of doing this was to use <a name="Content"></a>.

Neil
  • 50,855
  • 8
  • 54
  • 69
  • 5
    Great answer for a great question... even though some feel it's not a "real question" (I disagree) and have closed the question. I can't imagine why, other than some (wrongly) feel that if something can be found on Google, it's not deserving of being asked on SE/SO. – Kevin Fegan Oct 10 '17 at 07:51
  • Is it possible to link to an element based on other attributes other than `id`? I've tried searching online and using CSS selectors, but that didn't work. – Tiger Yu Jun 24 '20 at 09:23
  • @TigerYu There's a proposal to be able to link to specific text, so that search engines can link you to a point on the page near the search result. See https://github.com/WICG/scroll-to-text-fragment#readme – Neil Jun 24 '20 at 10:30
  • Anyone any thoughts on the difference between `name="fancy"` and `name="#fancy"`? – Ceylan Mumun Kocabaş Feb 12 '21 at 11:37
  • 1
    @CeylanMumunKocabaş You could use `name="#fancy"` but then your link would have to end with `##fancy`. – Neil Feb 12 '21 at 13:01
  • Well, i am migrating a website created for IE <9 to Edge/Chrome and i was wondering why might this `#` was used. Thanks anyway. It seemed wrong to me as the links did not contain `##` and yet it was working on IE <9. – Ceylan Mumun Kocabaş Feb 12 '21 at 14:38
32

Give the element you want to 'jump' to a clear ID, like so:

<p id="idOfTag">Your content goes here</p>

Then in the link on the top of the page, make it refer to the id of that element (mind the #):

<a href="#idOfTag">Jump</a>

Full example with multiple links:

<ul>
  <li><a href="#contentParagraph">Content</a></li>
  <li><a href="#mainPageParagraph">Main page</a></li>
  <li><a href="#documentParagraph">Document</a></li>
  <li><a href="#expensesParagraph">Expenses</a></li>
</ul>
<p id="contentParagraph">
  <h4>Content</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="mainPageParagraph">
  <h4>Main page</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="documentParagraph">
  <h4>Document</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p id="expensesParagraph">
  <h4>Expenses</h4>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
Abbas
  • 13,285
  • 6
  • 39
  • 67
  • I was planning to do so after I had given my answer but when I posted it the question had already been edited. But I'll keep your advise in mind (edited my comment too).. – Abbas Sep 12 '12 at 10:47
  • 2
    this is a better answer than the selected one – Rich Nov 07 '17 at 03:04
  • @RichJ Thanks! Seeing the answer again, I added some explanation and a code snippet to enhance the quality :) – Abbas Nov 07 '17 at 08:57
5

You can use name attribute for your anchor tag to achieve this.

Let say you have a div with id content

<div id="content">This is my div</div>

Next make sure you have a anchor tag with name attribute same as the id of the div content

<a href="#" name="content">Click to go to the top</a>

Live Demo.

Scroll down to see the link

Another approach to do this would be

<div id="content">My div</div>

Then your anchor tag's href should be #content

<a href="#content">Click to go to top</a>

Live Demo.

Priyank Patel
  • 6,532
  • 10
  • 52
  • 84
-2

Looks like the question has been answered but if you wanted to use a scrolling effect when transitioning to those elements here a little JS snipt. $(function() {

    function filterPath(string) {
        return string
        .replace(/^\//,'')
        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
        .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    var scrollElem = scrollableElement('html', 'body');

    // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
    $('a[href*=#]').each(function() {

        // Ensure it's a same-page link
        var thisPath = filterPath(this.pathname) || locationPath;
        if (  locationPath == thisPath
            && (location.hostname == this.hostname || !this.hostname)
            && this.hash.replace(/#/,'') ) {

                // Ensure target exists
                var $target = $(this.hash), target = this.hash;
                if (target) {

                    // Find location of target
                    var targetOffset = $target.offset().top;
                    $(this).click(function(event) {

                        // Prevent jump-down
                        event.preventDefault();

                        // Animate to target
                        $(scrollElem).animate({scrollTop: targetOffset}, 2000, function() {

                            // Set hash in URL after animation successful
                            location.hash = target;

                        });
                    });
                }
        }

    });

    // Use the first element that is "scrollable"  (cross-browser fix?)
    function scrollableElement(els) {
        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
            var el = arguments[i],
            $scrollElement = $(el);
            if ($scrollElement.scrollTop()> 0) {
                return el;
            } else {
                $scrollElement.scrollTop(1);
                var isScrollable = $scrollElement.scrollTop()> 0;
                $scrollElement.scrollTop(0);
                if (isScrollable) {
                    return el;
                }
            }
        }
        return [];
    }

});
Ryan Rich
  • 9,988
  • 8
  • 20
  • 29