The Difference Between Anchors, Inputs and Buttons

By  on  
HTML5 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 a src 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 its display to hidden 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!

Landon Schropp

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.

Recent Features

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    Using Opacity to Show Focus with MooTools

    I'm a huge fan of using subtle effects like link nudging (jQuery, MooTools) to enhance the user experience and increase the perceived dynamism of my websites. Trust me -- a lot of little things are what take websites to the next level.

  • By
    MooTools History Plugin

    One of the reasons I love AJAX technology so much is because it allows us to avoid unnecessary page loads.  Why download the header, footer, and other static data multiple times if that specific data never changes?  It's a waste of time, processing, and bandwidth.  Unfortunately...

Discussion

  1. 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.

  2. MaxArt

    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?

  3. 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.

  4. Nice David,

    Therefore, we must use the “button” for open the “hidden modal”. Sure? ;)

  5. Naveen

    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!

  6. Paul Ferguson

    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.

  7. Steve

    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. ;-)

  8. i have a button that is acting as an anchor, but it will only work the first time the page is loaded. Any thoughts?

  9. Shweta Gupta

    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!!!

  10. Aleks

    Thank you for good and concise explanation!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!