Today I Learned - Rocky Kev

TIL When to use a button versus an "a href" link

POSTED ON:

TAGS:

The best route to go in:

If it takes you to another route/page/website, or a part of a website, use a href.

If it's a function, maybe it sends data (a form submission) or fires an action (toggles dark mode/light mode, search box, hide/show content), a button is ideal.

The debate:

A link is focusable and can be triggered by the enter key. A link will redirect you to a new page or a section within the same page. In VoiceOver's rotator, it will also be collected within the "Links" window.

A button is focusable, too, and can be triggered by the space key. A button will trigger an action like opening or closing something. Or sending a form. In most cases JavaScript is used to do this. In VoiceOver's rotator, it will be collected within the "Form controls" window. That alone says something.

via a11ly-101.com

Don't use divs

Elements like

and are for layout only. They’re semantically meaningless, they don’t have keyboard or touch support in any browser, and they don’t communicate anything to the accessibility API. For example, never use a div or span for a button when you could use a semantically meaningful button element.

via web-accessibility.carnegiemuseums.org


Related TILs

Tagged:

TIL Gmail has a 102KB size-limit for HTML

PLACEHOLDER

TIL how Error correction works in HTML

You never get an 'Invalid Syntax' error on an HTML page. Browsers fix any invalid content and go on.

TIL what DOCTYPE means

tl;dr: DOCTYPE declaration in the first line of the HTML file, to instruct the browser to run the code in Standard mode.