10 ms·
The Button Cheat Sheet
- corty 5y agoWhats missing is which ones work without JavaScript. And accessibility characteristics.
- FriedrichN 5y agoTrue, only <button> (if type is not "button") and <input type="button"> function within a <form> context as a button.
- extra88 5y ago<input type="image"> does as well.
- amichal 5y agowithout javascript the only meaningful thing that can be done in HTML are to navigate to a new document with HTTP GET (A[href=]) or submit data via HTTP POST or similar verbs ((BUTTON, INPUT[TYPE=BUTTON]. INPUT[TYPE=IMAGE]) where the url is in the FORM tag or the form* attributes. This is because without javascript there are no persistent stores on the client side and you have to interact with HTTP to create a stateful application.
- soperj 5y agoHonestly, most people do not care whether you're doing client side or server side. Regular people don't even notice a difference.
- riffraff 5y agoyeah, "A focusable div with a button role" should be pretty accessible compared to a div with onclick event and neither of those things, for example.
- extra88 5y agoIt is not sufficiently accessible because it still won't activate on Space Bar or Enter/Return keydown events. "Accessible" does not only mean "screen reader users can use it."
- riffraff 5y agomy point is that it's better then a div without those attributes. Switching markup completely is a larger effort than adding tabindex and role and the latter still provides a benefit.
- extra88 5y agoI'd say whether a button does something without JavaScript is a separate topic. The page repeatedly mentions accessibility characteristics, it just doesn't call them all out as such. > Not keyboard focusable > Correct button role > correct key events by default > No accessible name "Shows wrong cursor on hover" is arguably also about an accessibility characteristic.
- felipemesquita 5y agoNot included is this unbelievable button from an example in TikTok’s web api: https://developers.tiktok.com/doc/login-kit-web https://developers.tiktok.com/doc/login-kit-web
- oneeyedpigeon 5y agoI wonder if that's because the TikTok crapfest shouldn't even be a button, it should be a link.
- mmatuzo21 5y agoYes, I'll start to work on linkcheatsheet.com soon. :)
- avocadoLife 5y agoI've heard this rule of thumb: If it is supposed to be a meaningful link - be it internal or external - use a direct anchor element with an href. Styling it however you like shouldn't hurt. Otherwise, for any other interactions use a <button> element
- vidyesh 5y agoMe too! And I been following this for as long as I remember. iirc, Digital A11Y too recommends that.
- fareesh 5y agoMy rule is "anchor tag for everything unless it's a form submission", in which case, button.
- brianzelip 5y agoEven better: "anchor tag for everything that takes a user to a url, button for everything else" (forms and other action/event initiators).
- kevincox 5y agoI don't think this rule. Anchor tags should be used to go somewhere. Buttons should be used to do something. There are cases where these overlap (am I popping up a settings menu (doing something) or navigating to the settings (going somewhere)) but in many cases it is distinct. Furthermore anchors have awesome features such as being able to copy the destination or open the link in a new tab. If the target isn't actually a real location using anchors can be confusing.
- tomcooks 5y agoInteresting site, I'm not a big fan of emoji/slang/playful tone which makes me feel like I'm being taken for an idiot but that's my own problem Thanks for making this and illustrating all the cases and variations of a problem, consider making a whole site about these accessibility problems
- mmatuzo21 5y agoYeah, sorry. I understand how that's not everyones style, but I'm glad that you could still get something out of the site. You'll find more stuff like that on my other site htmhell.dev.l, if you're interested. :)
- tomcooks 5y agoNo need to be sorry it's just me being a boring old fart)
- david422 5y agoBut why would you make the Yay or Nay? buttons not all real buttons then?
- recursive 5y agoSo you can see the behavior?
- hcarvalhoalves 5y agoAm I the only one who thinks CSS + JS on top of HTML broke the conceptual model beyond hope? The fact you can make any element look and behave the almost the same way – ignoring compatibility and usability issues – is a minefield. We could create a guide like this for basically everything and demonstrate 99% of the web isn't compliant.
- Sembiance 5y agoI’m making a website right now that only uses HTML, no CSS/styles and no JavaScript and it is a breath of fresh air :)
- NoGravitas 5y agoThe only problem with this is that, for historical reasons (and lack of caring?), the default stylesheets for unstyled HTML in the leading browsers are terrible. Best to apply a simple classless style sheet once then forget about it.
- oneeyedpigeon 5y agoThere's not that much that is objectively bad about the default styles. About the only thing that I can really put my finger on is equal top/bottom margins on headings. Possibly default line height, too. I wonder if one of the browsers will (or already has?) ever change that.
- mosdl 5y agoYou can fake things in most UI systems, nothing new here.
- hcarvalhoalves 5y agoWell, that's true, but HTML wasn't invented to create general UIs in the first place, so it's even worse.
- 5y ago
- memco 5y ago> Shows wrong cursor on hover This seems a bit unfair to single out given that they gave equal styles to all the buttons. Why didn't they include `cursor: default` so they all look exactly the same but function slightly differently?
- oneeyedpigeon 5y agoThey don't apply any cursor styles to any of these 'buttons', as far as I can see. Their stylesheet (https://www.buttoncheatsheet.com/assets/style.css https://www.buttoncheatsheet.com/assets/style.css) only has one instance of "cursor", which is unrelated.
- sichtlinkair 5y agoKinda funny to me we need a website to explain you should use <button> for a button
- chaoticmass 5y agoWeird-- The first <button> example shown does not show the right cursor when hovering for me.
- exogen 5y agoThe normal arrow (not the hand) is supposed to be the correct cursor for buttons, but many people change it to the hand because they think it indicates interactivity. However, the div button is still wrong because it shows the text selection cursor in the button.
- Tabular-Iceberg 5y agoWhat about <embed src="button.swf">? Should I use that?
- modin 5y agoTry <applet code="button.class" height="60" width="240"> <param name="button" value="Yea or nay?"> <b>Sorry, you need Java to press this button.</b> </applet> I had some buttons like that back in the day, with animations, sound effects and all, geocities-esque. The computer almost grinded to a halt loading it, but pre-teen me felt it was worth it.
- Tabular-Iceberg 5y agoMine had the lake applet. It was gorgeous.
- jasonlotito 5y agoMy rule of thumb has always been a button does something, a link takes you somewhere.
- butz 5y agoAdd web component implementation for a button, just for completeness.
- dmitriid 5y agoIncluding a button inside Shadow DOM which breaks accessibility and cannot participate in form events without javascript.
- 0xTJ 5y agoTo me, a good website should be usable without any added style or scripting. CSS should make it look good, but you should be using the markup to make is usable and roughly laid out how you want, at least with a sensible default.
- nerdbaggy 5y agoHave any examples? Just using tables to lay stuff out?
- oneeyedpigeon 5y agoNo, definitely not. Tables are for data, not for laying stuff out.
- hryx 5y agoFor a fun surprise, view the HTML source of this page!
- crumpled 5y agoHTML embedded into email templates often uses tables for layout. If you get a commercial email and the content has graphic borders around the content, check the source and you'll probably see a table. Also emails with text content in columns.
- p_j_w 5y agoDo you have an example of such a website? I'm genuinely curious if such a thing exists. I've been doing some CSS+HTML tutorials lately, and the ones I've been reading, I don't see how you would without style attributes in all of your tags, which is essentially just using CSS.
- extra88 5y agoThis very site is usable for reading and writing without CSS or JavaScript. Oddly, you can vote without JavaScript but not without CSS (buttons aren't visible in layout), which could easily be corrected. Its appearance doesn't change as much as it might because it uses nested tables for layout (a Nineties-era practice) and deprecated properties like bgcolor but even without those choices it could still be usable.
- firefoxd 5y agoOne type of button is missing from the list. Input with type="image" and src="img.gif" It's a button with all the properties of an image. Haven't used it in many years but it's still around.
- deleted 5y ago[deleted]
- montroser 5y agoCheck out https://knopf.dev https://knopf.dev for a fantastic CSS button library. In any sufficiently complex UI, you really do end up needing a wide variety of button treatments, depending on the context, hierarchy, etc
- beardyw 5y agoIt seems odd there is no mention of labels. I am never sure about how accessible they are or if they can stand in for their target.