<button> vs <input type="button" />
Differences
<button>
can contain HTML.<input type="button" />
on the other hand is an empty element (such asbr
,hr
,image
), therefore it cannot contain content.<button>
supports pseudo-elements such as:after
and:before
which is very useful to style the button. Whereas<input type="button" />
doesn't.By default,
<button>
has the default attributetype="submit"
. It means that if thetype
attribute isn't specified, clicking the button will submit its enclosing form.If you want the
input
to behave as a submit button, we have to change thetype
attribute tosubmit
.
Good practices
The
button
element is more semantic than theinput
with type ofbutton
. It's recommended to use thebutton
element if you want to create a clickable button.Always specific the
type
attribute for thebutton
element. The possible values are
Value | Description |
---|---|
submit | The button submits the form data to the server |
reset | Resets form inputs to initial values |
button | By default, it does nothing when pressed |