Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<button class="button"> tag vs <div class="button">

Is it incorrect to use the html <button> tag when designing a UI rather than using <div class="button">? In implementation I haven't noticed a difference between both approaches, and <button> seems more semantic, but I am not sure if it is only intended for usage within a form. The button would not be in a form. Using the <div> tag seems to be the most common way of making buttons (bootstrap) but I am not sure why. Are there any technical reasons for this?

like image 567
darko Avatar asked Jan 08 '13 00:01

darko


People also ask

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.

Should I use button tag or a tag?

With the button , you will need to invoke javascript `onclick. use a <button> when you don't need to navigate for example loading a modal or submitting a form, always use <a> for navigation.

What is the difference between a tag and button?

Links are contained within an HTML anchor tag with an href attribute containing the URL where the user will be directed. Buttons - A button is meant to submit information and will not necessarily redirect the user to an entirely new web page.

Can a div act as a button?

role=“button”ARIA (Accessible Rich Internet Applications) attributes provide the means to make a div appear as a button to a screen reader. Here is the intro to the button role page on MDN: Adding role=“button” will make an element appear as a button control to a screen reader.


2 Answers

It's better to use <button> rather than <div> or any other element to represent a button. The "type" attribute is important here and should be type="button", without it the default value is submit which doesn't make sense outside of a form.

And yeah by the way, having a <button> outside of a form is perfectly fine.

Often times while designing a UI, the <a> tag is the most appropriate element where it makes sense, but <button> is perfectly fine (and really, intended) for performing an action via javascript.

Using the <div> tag seems to be the most common way of making buttons (bootstrap) but I am not sure why.

I really haven't seen evidence of this, and to be honest it doesn't matter what they do. The bootstrap framework tends to disregard semantics, for example though the use of empty elements just for presentational reasons like <li class="divider"></li>.

like image 102
Wesley Murch Avatar answered Oct 10 '22 06:10

Wesley Murch


From a usability perspective, it's better to implement <button> over <div> or <a> due to the way browsers handle interactivity.

If your users are sloppy clickers and they click-and-drag by mistake, or on a touch-screen, they can end up selecting the <div> contents rather than triggering a click event. Using <button> will greatly reduce this problem.

But, non-button objects, like <div>'s, can be more flexible when applying CSS. Especially in older browsers.

like image 25
Andy Purviance Avatar answered Oct 10 '22 06:10

Andy Purviance