Can we include an anchor tag inside a button or vice-versa in HTML?

Hello, say I need to style a link that will direct me to another web page. My question is can we have a button and inside that button have an anchor tag? Or we need the anchor tag to stand alone and style it?
4 Replies
Faker
FakerOPβ€’3mo ago
Like doing something as follows:
<button><a href="#">link </a></button>
<button><a href="#">link </a></button>
or a button nested into an anchor tag
Chooβ™šπ•‚π•šπ•Ÿπ•˜
This is absolutely not allowed. No "interactive content" is allowed to be a child of a button. That prohibits links and other buttons. Aside from the fact that this is expressly prohibited in the HTML Standard, it would be very confusing, since you would not know which interaction you are doing when you click. https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element
Chooβ™šπ•‚π•šπ•Ÿπ•˜
Also, you can use an HTML validator to determine if this kind of thing is allowed.
No description
Faker
FakerOPβ€’3mo ago
Yep I see , thanks !

Did you find this page helpful?