Search code examples
htmlhrefelement

Why does href appear not to work with id?


<a href="http://www.website.com"><h1><a id="Didn&#39;t-answer-your-question&#63;">Didn&#39;t answer your question?</a><!--ID end--></h1></a><!--link end-->

Can I have an id element inside a href element, for the link text or does this not work, if so do people have an alternative?


Solution

  • There are several problems with your markup.

    You don't close the <h1> tag properly. Your <a> id may technically work, but I wouldn't recommend it as an element ID.

    The one that's giving you an issue though is the fact that you have an <a> tag nested inside another <a> tag. That is not valid markup.

    Your inner tag does not need to be an anchor tag, change it to a span or something and it should work fine.

    <a href="http://www.website.com">
        <h1>
            <span id="Didn&#39;t-answer-your-question?&#63;">Didn&#39;t answer your question?</span>
            <!--ID end-->
        </h1>
    </a>
    <!--link end-->