Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS - Buttons - When to use what

Tags:

html

css

button

I have a pretty basic question regarding buttons on HTML pages.

As we know, there are several possibilities to create them. It is possible to set display: block; on an a, so one can assign a color, width and height to it. But there is also the HTML element button and the element submit.

When to use what? For example, when creating a form, I need a submit element if I remember right. But when I have a button outside of a form, I can use a normal a. But still, I don't know then to use button.

Could you please help me out with this?

like image 836
Sven Avatar asked Aug 10 '12 10:08

Sven


People also ask

What are buttons used for in CSS?

Buttons help us to create user interaction and event processing. They are one of the widely used elements of web pages. During the form submission, to view or to get some information, we generally use buttons.

What are buttons used for in HTML?

The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.).

Should I use button or anchor?

From a high level, buttons are to be used for performing an in-page action and anchors/links are for navigating to a new page. You can read more about "why" on the Button and Anchor/Link documentation. Because a button's functionality is distinctly different from an anchor, we need to be deliberate in their use.

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.


2 Answers

Anchors (<a>) should be used when it's a link, and not a form submission.

Search engine crawlers cannot follow links which are submitted by input or button, only a. Therefore for SEO purposes, its best to use anchors for links.

If its a form, you should always use either a button or an input because these can submit the form on pressing the enter button (unlike links), and are generally better for accessibility.

I won't go into detail regarding whether to use button or input however, as there is already an indepth post regarding this:

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

like image 97
Curtis Avatar answered Sep 21 '22 12:09

Curtis


<button>

The button (<button>) HTML element represents a clickable button.

<a>

The HTML Anchor Element (<a>) defines a hyperlink, the named target destination for a hyperlink, or both.

like image 44
Ahsan Khurshid Avatar answered Sep 22 '22 12:09

Ahsan Khurshid