<button> vs. <input type="button" />. Which to use?
Asked Answered
U

17

1893

When looking at most sites (including SO), most of them use:

<input type="button" />

instead of:

<button></button>
  • What are the main differences between the two, if any?
  • Are there valid reasons to use one instead of the other?
  • Are there valid reasons to use combine them?
  • Does using <button> come with compatibility issues, seeing it is not very widely used?
Unreadable answered 22/1, 2009 at 13:14 Comment(3)
Citing the documentation as of Oct 2020: While <input> elements of type button are still perfectly valid HTML, the newer <button> element is now the favored way to create buttons. Given that a <button>’s label text is inserted between the opening and closing tags, you can include HTML in the label, even images.Campground
@Campground I understand MDN is a decent reference, but I could not really find similar recommendations in HTML5 spec or any RFC-like docs.Barbate
@Campground Mozilla Developer Network is not "the documentation", it's a publicly (volunteer) driven resource that functions as more of an [convenient] "appendix" to broadly accepted specifications like the one published by WHATWG for HTML 5 at html.spec.whatwg.org.Assemblyman
S
730
  • Here's a page describing the differences (basically you can put html into a <button></button>)
  • And another page describing why people avoid <button></button> (Hint: IE6)

Another IE problem when using <button />:

And while we're talking about IE, it's got a couple of bugs related to the width of buttons. It'll mysteriously add extra padding when you're trying to add styles, meaning you have to add a tiny hack to get things under control.

Sedgemoor answered 22/1, 2009 at 13:20 Comment(3)
This answer was in 2009, since IE6 is dead now I assume there is no reason not to use <button> now?Loudspeaker
If they want to pirate , let them get a sucky version of your site. Drop IE6, drop IE7, unfortunately IE8 still needs support.Allineallis
According to Microsoft's IE6 Countdown page, China's IE6 usage is still clocking in (as of Jan 2014) at about 22%. ie6death.com notes that IE6 support is ending April 8, 2014.Shrub
G
414

Just as a side note, <button> will implicitly submit, which can cause problems if you want to use a button in a form without it submitting. Thus, another reason to use <input type="button"> (or <button type="button">)

Edit - more details

Without a type, button implicitly receives type of submit. It does not matter how many submit buttons or inputs there are in the form, any one of them which is explicitly or implicitly typed as submit, when clicked, will submit the form.

There are 3 supported types for a button

submit ||  "submits the form when clicked (default)"
reset  ||  "resets the fields in the form when clicked"
button ||  "clickable, but without any event handler until one is assigned"
Gunn answered 9/4, 2012 at 20:13 Comment(0)
B
190

This article seems to offer a pretty good overview of the difference.

From the page:

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to “image”, but the BUTTON element type allows content.

The Button Element - W3C

Benito answered 22/1, 2009 at 13:19 Comment(0)
I
58

Inside a <button> element you can put content, like text or images.

<button type="button">Click Me!</button> 

This is the difference between this element and buttons created with the <input> element.

Interoffice answered 28/3, 2012 at 4:27 Comment(0)
D
46

Quote

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.

From : http://www.w3schools.com/tags/tag_button.asp

If I understand correctly, the answer is compatibility and input consistency from browser to browser

Daggett answered 22/1, 2009 at 13:18 Comment(0)
T
34

I will quote the article The Difference Between Anchors, Inputs and Buttons:

Anchors (the <a> element) represent hyperlinks, resources a person can navigate to or download in a browser. If you want to allow your user to move to a new page or download a file, then use an anchor.

An input (<input>) represents a data field: so some user data you mean to send to server. There are several input types related to buttons:

  • <input type="submit">
  • <input type="image">
  • <input type="file">
  • <input type="reset">
  • <input type="button">

Each of them has a meaning, for example "file" is used to upload a file, "reset" clears a form, and "submit" sends the data to the server. Check W3 reference on MDN or on W3Schools.

The button (<button>) element is quite versatile:

  • you can nest elements within a button, such as images, paragraphs, or headers;
  • buttons can also contain ::before and ::after pseudo-elements;
  • buttons support the disabled attribute. This makes it easy to turn them on and off.

Again, check W3 reference for <button> tag on MDN or on W3Schools.

Transported answered 8/11, 2014 at 14:31 Comment(1)
<input> elements also support ::before, ::after, and disabled, so those aren't really benefits for using a <button>.Pique
P
25

Although this is a very old question and might not be relevant anymore, please keep in mind that most of the problems that the <button> tag used to have don't exist anymore and therefore is highly advisable to use it.

In case you cannot do so for various reasons, just keep in mind to add the attribute role=”button” in your tag as of accessibility. This article is quite informative: https://www.deque.com/blog/accessible-aria-buttons/

Prothesis answered 11/3, 2020 at 14:54 Comment(0)
M
18

Quoting the Forms Page in the HTML manual:

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content.

Muniment answered 22/1, 2009 at 13:19 Comment(0)
S
18

Use button from input element if you want to create button in a form. And use button tag if you want to create button for an action.

Sawtelle answered 1/3, 2011 at 8:17 Comment(1)
For client side scripting. <input type="button"> has no function in HTML.Volsci
A
18
<button>
  • by default behaves like if it had a "type="submit" attribute
  • can be used without a form as well as in forms.
  • text or html content allowed
  • css pseudo elements allowed (like :before)
  • tag name is usually unique to a single form

vs.

<input type='button'>
  • type should be set to 'submit' to behave as a submitting element
  • can only be used in forms.
  • only text content allowed
  • no css pseudo elements
  • same tag name as most of the forms elements (inputs)

--
in modern browsers, both elements are easily styleable with css but in most cases, button element is preferred as you can style more with inner html and pseudo elements

Aphasia answered 31/10, 2017 at 8:46 Comment(0)
L
11

As far as CSS styling is concerned the <button type="submit" class="Btn">Example</button> is better as it gives you the ability to use CSS :before and :after pseudo classes which can help.

Due to the <input type="button"> visually rendering different to an <a> or <span> when styled with classes in certain situations I avoid them.

It's very worth noting the current top answer was written in 2009. IE6 isn't a concern now days so <button type="submit">Wins</button> styling consistency in my eyes comes out on top.

Lomond answered 30/7, 2015 at 4:33 Comment(0)
K
11

I just want to add something to the rest of the answers here. Input elements are considered empty or void elements (other empty elements are area , base , br , col , hr , img , input , link , meta , and param. You can also check here), meaning they cannot have any content. In addition to not having any content, empty elements cannot have any pseudo-elements like ::after and ::before, which I consider a major drawback.

Katharina answered 19/6, 2017 at 10:20 Comment(0)
D
9

There is a big difference if you are using jQuery. jQuery is aware of more events on inputs than it does on buttons. On buttons, jQuery is only aware of 'click' events. On inputs, jQuery is aware of 'click', 'focus', and 'blur' events.

You could always bind events to your buttons as needed, but just be aware that the events that jQuery automatically is aware of are different. For example, if you created a function that was executed whenever there was a 'focusin' event on your page, an input would trigger the function but a button would not.

Dercy answered 21/12, 2015 at 17:12 Comment(2)
re: KjetilNordin - We had an application that was aware of any focusin events on a page, and one occurred, an action took place. So if using an input, you could watch all of the elements for a single event. Not saying you should, but you could. The risk is that someone changes the input to a button, and then the focusin does not occur, and then your action does not occur, and then your app goes boom. That's what happened to us. :)Dercy
use focus events when an element is currently targeted via mouse/keyboard. they show the user where they are in the document.Accede
F
7

<button> is flexible in that it can contain HTML. Moreover, it is much easier to style using CSS, and the styling actually gets applied across all browsers. However, there are some drawbacks regarding Internet Explorer (Eww! IE!). Internet Explorer does not detect the value attribute properly, using the tag's content as the value. All of the values in a form are sent to the server-side, regardless of whether or not the button is clicked. This makes using it as a <button type="submit"> tricky and a pain.

<input type="submit"> on the other hand doesn't have any value or detection issues, but you can't, however, add HTML like you can with <button>. It's also harder to style, and the styling doesn't always respond well across all browsers. Hope this helped.

Floorage answered 27/6, 2014 at 22:51 Comment(0)
G
4

in addition, one of the differences can come from provider of the library, and what they code. for example here i'm using cordova platform in combination with mobile angular ui, and while input/div/etc tags work well with ng-click, the button can cause Visual Studio debugger to crash, surely by differences, that the programmer caused; note that MattC answer point to the same issue, the jQuery is just a lib, and the provider didn't think of some functionality on one element, that s/he provides on another. so when you are using a library, you may face an issue with one element, which you won't face with another. and simply the popular one like input, will mostly be the fixed one, just because it's more popular.

Gao answered 2/1, 2016 at 8:23 Comment(0)
O
1

It's also worth mentioning , that the disabled attribute doesn't work well on button for ios- safari (see also - @ Khris Vandal comment ). This happened to me as well .

Obstetric answered 25/1, 2023 at 17:13 Comment(0)
N
-1

According to the purpose of its use , If you used for php advise you to use the input to make when the input submit work and what makes after click on it.

Numbing answered 2/5, 2023 at 20:41 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMessy

© 2022 - 2024 — McMap. All rights reserved.