The Difference Between Anchors, Inputs and Buttons
One of the things I love about CSS is how easy it is to make one element look like another. In the example above, the first element is an anchor, the second is a button and the third is an input. I've overridden the click
behavior of all three so they do the same thing.
If all three elements look and behave the same, does it matter which one you use? In this article, I'll explain the difference between anchors, inputs and buttons, and I'll show you when to use each one.
Semantics
The question of which element to use boils down to semantics, which is using markup that reflects the meaning of the content. Using semantic HTML makes your content explicit, which gives you better browser compatibility, accessibility and SEO.
Anchors
Anchors (the <a>
element) represent hyperlinks. What's a hyperlink? A hyperlink is a resource 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.
Inputs
An input (<input>
) represents a data field. The type
attribute tells the browser which type of data the input controls. There are five input types related to buttons.
<input type="submit">
: This is the most common button input. It's a button that, when clicked, submits a form.<input type="image">
: Like an<input type="submit">
, this input submits a form. However, it also takes asrc
attribute and is displayed as an image.<input type="file">
: This control is used to upload files and is shown as a label and a button. There's no good cross-browser way to style file inputs, so you'll usually set itsdisplay
tohidden
and use a second button to trigger it.<input type="reset">
: This is a button that resets a form.<input type="button">
: This is a button with no default behavior. You can use to it add non-standard behavior to a form using JavaScript.
Buttons
The <button>
element represents a button! Buttons do the same things as the inputs mentioned above. Buttons were introduced into HTML as an alternative to inputs that are much easier to style. Unlike inputs, a button's label is determined by its content. This means you can nest elements within a button
, such as images, paragraphs, or headers. Buttons can also contain ::before
and ::after
pseudo-elements.
Like an input, a button has a type
attribute. This attribute can be set to submit
, reset
or button
and does the same thing as the input's type. By default, the type
is submit
. If you place a button in a form and don't set its type
, when it's clicked it will submit that form! If you don't want this behavior, set the type
to button
.
One nifty feature of inputs and buttons is they support the disabled
attribute. This makes it easy to turn them on and off. Sadly, anchors don't have this capability.
Which one?
So should you use an anchor, input or button? When you're navigating the user to a page or resource, use an anchor. Otherwise, both inputs and buttons are valid. Personally, I prefer to use inputs for submitting and resetting forms and buttons for custom behavior because I think it makes the intent clearer. However, the element you use is entirely up to you. Go nuts!
About Landon Schropp
Landon is a developer, designer and entrepreneur based in Kansas City. He's the author of the Unraveling Flexbox. He's passionate about building simple apps people love to use.
To me it feels like a missed opportunity to not use the button to refer to, you know, an actual button! We already have a submit button.
So, what’s the best use for inputs of type “submit”, “reset” and “button”, if they are completely be superseded by buttons of the same type? I guess none, isn’t it?
yes, concerns should be separated!
1. It is weird to be able to middle-click on a “button” and be navigated to other tab/window.
2. In case of bootstrap it is confusing to have “disabled button” created from , and also have additional JS logic to prevent it from actually being clicked.
Nice David,
Therefore, we must use the “button” for open the “hidden modal”. Sure? ;)
I expected a little more on use cases of these three types like whether a link/page could be crawled via input/button or not? What are the SEO implications of all the three options? And many more details which I am not aware of!
I may be wrong, but I heard there is also a distinction that a < button will fire on space bar while an < input type=submit can be called on enter.
It’s also worth noting that although the button element is likely the best element for a button we couldn’t use it widespread until IE8 (running in Standards Mode) was the minimum IE version we needed to support due to a long running IE bug where IE would submit the innerHTML when you clicked a button vs. the value attribute: http://webbugtrack.blogspot.ca/2007/10/bug-341-button-element-submits-wrong.html In addition they also rendered horribly on Windows XP due to an OS bug: http://webbugtrack.blogspot.ca/2007/08/bug-101-buttons-render-stretched-and.html beyond that, button element is my choice for an actual button. ;-)
i have a button that is acting as an anchor, but it will only work the first time the page is loaded. Any thoughts?
I’m a beginner in learning HTML and I was really confused among the working of anchor, input and button tags. Now my doubts have cleared up. Great post!!!
Thank you for good and concise explanation!