I have a pretty basic question regarding buttons on HTML pages.
As we know, there are several possibilities to create them. It is possible to set display: block; on an a, so one can assign a color, width and height to it. But there is also the HTML element button and the element submit.
When to use what? For example, when creating a form, I need a submit element if I remember right. But when I have a button outside of a form, I can use a normal a. But still, I don’t know then to use button.
Could you please help me out with this?
Anchors (
<a>) should be used when it’s a link, and not a form submission.Search engine crawlers cannot follow links which are submitted by
inputorbutton, onlya. Therefore for SEO purposes, its best to use anchors for links.If its a form, you should always use either a
buttonor aninputbecause these can submit the form on pressing the enter button (unlike links), and are generally better for accessibility.I won’t go into detail regarding whether to use
buttonorinputhowever, as there is already an indepth post regarding this:<button> vs. <input type="button" />. Which to use?