Search code examples
htmlanchorstandardsdefinition

Is an anchor element required to have included text?


It is possible to define an anchor without text? I. e. without inline text.

<p>some text <a name="anchor47"></a> and further text</p>

I don't find any information in the standard. Usually I would write my HTML as

<p>some <a name="anchor47">text</a> and further text</p>

I just wanted to be sure that my code is accepted by all browsers and check programs...

One check program I used successfully (that means it does not complain if "empty" anchors are there) is that of w3


Solution

  • It's valid, but you could also add a blank space like:

    <p>some text <a name="anchor47">&nbsp;</a> and further text</p>
    

    or you can add text and set the font-size to 0

    <p>some text <a name="anchor47" style="font-size:0">anchors away!</a> and further text</p>
    

    I believe there is a benefit to adding some descriptive text in there and hiding it for screen readers. See here for info on accessibility + anchor links.

    However, I believe you should be using an alternative method like adding an ID to a span, h3 tag, etc.

    <h3 id="anchor">Do you know how to tie an anchor knot?</h3>
    

    And your link:

    <a href="#anchor">Tie an Anchor Knot</a>