Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<a> vs <button> vs <div> vs <input type='button">

Tags:

html

I've seen so many posts along the lines of "I'd like to one-specific-thing for my one-specific-situation when it comes to buttons in HTML.

If I may, I'd like to get a professional take all in one post, here:

When making buttons in HTML, what situations lead to:

  • <a> being the best answer?
  • <button> being the best answer?
  • <div> being the best answer?
  • <input type='button'> being the best answer?
like image 821
Mark Puchala II Avatar asked May 10 '16 19:05

Mark Puchala II


People also ask

Is input and button the same?

input suggests that the control is editable, or can be edited by the user; button is far more explicit in terms of the purpose it serves. Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases.

What is the difference between button and input type submit?

A 'button' is just that, a button, to which you can add additional functionality using Javascript. A 'submit' input type has the default functionality of submitting the form it's placed in (though, of course, you can still add additional functionality using Javascript).

Should I use a button or a div?

You should use <a> or <button> , not <div> . an anchor with an href attribute will trigger page reload. Without an href, it is not "tabable", and it still does not carry the button semantic.

What is the difference between button and input type button?

The difference is that <button> can have content, whereas <input> cannot (it is a null element). While the button-text of an <input> can be specified, you cannot add markup to the text or insert a picture.


1 Answers

There's a good article that summarizes the differences nicely

In short:

|                       |       General usage        | Complex designs | Can be disabled | |-----------------------|----------------------------|-----------------|-----------------| | <button>              | General button purpose.    | Yes             | Yes             | |                       | Used in most cases         |                 |                 | | --------------------- | -------------------------- | --------------- | --------------- | | <input type="button"> | Usually used inside        | No              | Yes             | |                       | forms                      |                 |                 | | --------------------- | -------------------------- | --------------- | --------------- | | <a>                   | Used to navigate to        | Yes             | No              | |                       | pages and resources        |                 |                 | | --------------------- | -------------------------- | --------------- | --------------- | | <div>                 | Can be used for clickable  | Yes             | No              | |                       | area which is not button   |                 |                 | |                       | or link by definition      |                 |                 | 
like image 191
Jaqen H'ghar Avatar answered Sep 17 '22 12:09

Jaqen H'ghar