0

Part of HTMl code uses a button that is used to navigate to a different route in flask hence I wrap it in an anchor tag like so:

 <a href="/deleteAccount" id="deleteAccount"><button>Delete Account</button></a>

When passing my HTML through the W3C validator I get the following error:
"The element button must not appear as a descendant of the a element."

What is the reason for this error? Writing my code this way achieves the desired effect of navigating through the use a button without having to instead style an anchor tag or use javascript unnecessarily.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Pzet
  • 470
  • 2
  • 4
  • 11
  • What's the point of the button element if it's within a link? If you need the look of a button, simply use CSS to style your anchor – j08691 Mar 11 '22 at 17:16

1 Answers1

3

In blunt terms:

Because the HTML spec forbids it.


In logical terms:

A link is an interactive control for navigating to a new page.

A button is an interactive control for submitting a form (unless you change the type in which case it becomes a control for resetting a form or just triggering a JS click event).

It doesn't make sense for one click to do both of those things at once.


If you want a link that looks like a button. Then style a link to look like a button. (But think hard about if that is a good idea, because a button is a UI control that users expect to do something and not just navigate somewhere.)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335