What is the correct code for turning HTML elements (H1, H2 or Paragraph etc..) into a link and search engines index texts of header and link both?

which one is better:
<a href="#"><h1>heading</h1></a>

or
<h1><a href="#">heading</a></h1>

The element which displays as a block (in this case <h1>) always should surround %inline elements (such as <a>).

Another example <div> should always be outside <span>.

That is why:
<h1><a href="#">heading</a></h1>
is correct.

<a href="#"><h1>heading</h1></a>
is Wrong.

An even easier way of understanding this is that the following makes sense:

<h1><a href="#1">link</a> and <a href="#2">Another link</a></h1>

It would be highly unusual to try the inverse with multiple <h1>s inside an <a>.

Reference:

http://www.w3.org/TR/html401/struct/global.html#h-7.5.4

Share.
Unknown's avatar

Hollands Web is a web design and digital solutions agency specializing in WordPress development, web hosting, SEO, and custom digital solutions. Our team shares practical insights, guides, and strategies to help businesses build, improve, and grow their online presence.

Leave A Reply

Ready to get started?

Are you ready
Let’s Make Something
Amazing Together

Need help? Contact our experts
Tell us about your project

Groningen, Netherlands.

Kuipenstreek 1,
Oosterwolde 8431 LX

Istanbul, Turkey.

Hırka-i şerif mah.
Fatih 34091

Newsletter

Sign up to our newsletter!

© 2026, Holland’s Web. All Right Reserved.