The right tag for the job: why you should use semantic HTML
localghost.dev
localghost.dev
For example, a <button> will have default tabindex=0 and respond to spacebar key presses, but you'd have to add that yourself if you put role=button on a <div>.
In short, if there is a semantic element that matches your need, use it.
Yes! This is known as The First Rule of ARIA.
Combo boxes are a fucking nightmare to style, as are checkboxes and radios. Buttons arnt as bad, but you still spend a lot of time fighting against browser defaults which differ across browsers.
Your comment below about being difficult to style is more to the point.
I'd be curious to see a somewhat-objective look at what's actually wrong with default browser styles, separating out well-established usability and design considerations from personal preferences and branding preferences. I wouldn't be at all surprised if there are near-universal things wrong with the default styles, and those might be possible to change if they can be separated out.
Default styles aren’t pretty.
That’s it. They’re superior in every other way. You always know what to click, you always know what an element will do, how it behaves, the UX is consistent with your OS, etc. Default elements are fantastic.
Buttons in Chrome on macOS look completely different than in native apps. Buttons and context menu in Chrome's native <video> controls look yet again different, following Material Design. From the things one could argue are good about native browser styles "consistency with the OS" is definitely not one of them.
Of course, it’s worth putting the effort in to both looking good and working good, but if we’re going to pick one, we should go for the second.
I had a project that had applications associated with it. Forms are the natural way to express this. There were some disclosures associated with the application, and there was a modal associated with emailing the disclosures. I needed to add some front end validations to this modal.
No matter what I tried, I could not get these validations to trigger correctly. I spent an entire work day on trying to get a library to work with this modal. It wouldn't work, because changing the modal's div to a form tag would cause errors or unexpected behavior due to the nested form tags.
TL;DR: Nothing requires it. Devs might not understand the implications.
The trade-off is that you have to specify it almost every single time, but it lets you group form elements that for UX purposes belong together, but for other reasons need different forms.
[0] https://developer.mozilla.org/en-US/docs/Web/HTML/Element/in...
onkeydown=function(event){if(event.key==="Enter"){mySubmit();}}
You are clearly overthinking the challenge.Believing you can do so and not fuck it up is sheer hubris, not to mention, wrong. Doing so because you don't like the browser edge cases, is how websites end up suffering from even more edge cases, and harks back to the bad old days of "Sorry your browser is not supported".
Financial institutions are amongst the worst offenders - ironic since pulling this kind of naïve stunt vastly increases the attack surface. I've been in a position to redesign financial institution systems and rewrite their technology policies, and when doing so this kind of off-standard NIH crap is something I seek to stamp out.
Want to submit a form? Use a damn form, with a plain old submit button, and rely on the standard behaviour. Want to put some UI polish around it? Progressively enhance the standard behaviour.
What not to do: imagine you can write better UI<>protocol interaction code than that already in Chromium and Webkit, or hope to infer and reimplement the behaviours of any more specialised user-agents.
What you said. Most of what you said is common anti patterns. The reason is web technologies are built around a few set of standards that are designed for extension, such as the DOM and WCAG. Fearing those standards for a small set of static principles is common but that doesn’t make it smart. That’s why this technology space is so hostile to originality. You mentioned NIH but the more common problem is: https://en.m.wikipedia.org/wiki/Invented_here
Stop being so afraid and hostile. Embrace how these technologies work and more easily achieve accessibility with less effort. If you are waiting for some tool, NPM package, or framework to do it for you it’s not going to happen.
Abusing standards with shoddy inner-platform reimplementations doesn’t make someone an innovator, it makes them as much a fool as the cargo-cult of the NPM ecosystem.
> I have seen less experienced developers do weird things with form tags and submit event
You say that, but I doubt that in practice. If the form is your bank login or bill pay or anything else you are locked into I really doubt you will give up because of some personal opinion on JavaScript and instead spend the next 30 minutes calling somebody to complete a similar action over the phone.
The goal is to achieve accessibility as equivalently as possible, with as little enhancement as possible, and yet often not force a page change because of form submission.
I like coding html (and css) by hand. I find it easier to do that than to deal with frameworks and tooling. So I read articles like this and often find something new (TIL about fieldset). But I've yet to see anyone else do this professionally.
So I don't think most people will ever care about this stuff, in the face of modern "best practices". It's just going to be nested div soup, from here on out.
I haven't seen much to back up your view of things
The semantics of the web and the semantics of an application are almost completely different, so those of us trying to ship actual applications over the web are torn between trying to find the closest possible HTML element and customizing it, resulting in non-standard behavior, or just rolling our own div with our own needs.
Thinking in Semantic HTML might help normalize the variances.
Technically you could make everything a div or a text element, but most frameworks you use come with certain expectations: the closer you are to using semantic tags, the less likely you'll be to violate those external expectations.
If you're not using any frameworks, I don't see why you wouldn't use semantic tags by default. Sure, you can override the onclick if an anchor with no href and call preventDefault, but why not just use a button instead?
One thing that rarely gets mentioned however is that well structured HTML, including templates and JSX, is easier to develop with.
Semantic tags at least give you common consistency for free. Adhering to some reasonable degree of consistency is the bare minimum we should aim for in respect to code readability.
Obviously this works best with simple "document" websites but it always seems so weird telling the browser how to style the document when I know very little about the user or the device. It is getting "better" in a way with media queries for information about the device and user preference (like dark/light colour scheme) but it still is relying on each site to interpret those settings themselves, and progress is slow because everything needs to be standardized.
This also opens the door to a "clean" mode with no default borders, margins, padding, sizes, styles which would help when making interfaces where you want to control every pixel and don't want to be surprised in differences in default browser style sheets.
Non-Chrome browsers still have modifiable user stylesheets (though I think only Safari for Mac still has a GUI option to do it) but more realistically, you can use a browser extension like Stylus to apply your own styles, even defining them on a per-domain basis.
Enabling the user to change the appearance of your site is much easier if you heed the advice of this article and use the appropriate semantic HTML.
> "clean" mode with no default borders, margins, padding, sizes, styles
Web developers have been starting projects with "reset" stylesheets for decades.
This doesn't solve the problem I describe because it breaks websites (unless you are very minimal with your usage). I am talking about an opt-in feature where you concede control to the browser so that it can apply "aggressive" styles such as it would for reader mode. For example changing the current usage-agent style sheet to a "dark theme" is completely infeasible to ship as it would break way too many sites.
Furthermore just being possible isn't enough. It would also need to provide nice defaults to make this a feasible proposition at all. I can't have my blog being barely readable for 99% of viewers.
> even defining them on a per-domain basis.
I also don't want to fix specific websites. I'm talking about something that works by default. (Of course site-specific tweaks are nice, but that is nearly orthogonal to what I am looking for here.)
> Web developers have been starting projects with "reset" stylesheets for decades.
Yes, and this is an unfortunate hack. It would be nice to remove the necessity to remove the need for these and remove the maintenance required (even if the cost of both is small).
> Yes, and this is an unfortunate hack. It would be nice to remove the necessity to remove the need for these and remove the maintenance required (even if the cost of both is small).
There are too many different opinions about what should or shouldn't be in base styles.
- For "content" sections the discussion is entirely removed. Each browser can do whatever it wants so there is no need to argue in the standard.
- For "clean" sections nothing! Every element has no margin:0 padding:0 display:block size:1em color:inherit... This leaves no surprises and developers can do whatever they want.
Of course the third section "legacy" is what we have today and largely shouldn't be changed from the mostly-uniform thing that browsers do today.
That would make creation of visual elements more versatile, keep the number of hacks to minimum (like the hidden input trick for checkboxes), and also make ARIA possible.
Try that with a screen reader.
[1] https://docs.google.com/document/d/1N1XaAI4ZlCUHNWJBXJUBFjxS...
Unfortunately adding semantic information to website has gone from a promising avenue for building websites usable from many different kinds to part of the SEO cat-and-mouse game played by Google et al.
Unless you're doing SEO, everything semantic is just a distraction. That said do take accessibility seriously, it will never be justified by profits but doing the right thing is a feeling no amount of money can buy.
Accessibility and SEO are (usually) desirable system effects that rely on meaningful semantic code-inputs to the system.
But that doesn't mean that only software system effects matter, and everything else is a distraction.
Consider that when you're programming your various identifiers -- the names of variables, functions, classes, types that you choose -- are largely without any semantics to your system. To the computer, they're simply keys. And yet we sometimes say that one of two the hard/important problems in computer science (besides cache invalidation) is naming things. Why?
Because the actual system isn't just the software & machine. The system includes the developers groking and shaping the software. And names and their relationship with domain concepts matter to people and help them manage their work.
I've found that the first benefit to semantic markup -- not necessarily the most important, but the first -- is helping me better conceptualize the domain and my code. It functions as an orientation anchor when I'm working with components that look different between their source and rendered as part of a sprawling document in the browser. And it helps me get reoriented faster when I've been away from it for days/weeks/months.
The heart of "semantic" anything is the idea that code is for people too. Maybe not all people, but definitely at least anyone who's writing it.
This isn’t true. Semantic HTML is also very important, often more so.
> Unless you're doing SEO, everything semantic is just a distraction.
This is also not true. Besides actively harming accessibility (or making it substantially more difficult to improve), non-semantic HTML also breaks Reader Mode. And it can also add cognitive burden to maintenance and future feature work.
Gizmodo articles should load in Reader mode but don't, I assume they're doing that intentionally.
Best practice is use to use HTML semantic tags and only add ARIA attributes if it is relevant. For example, HTML5 has a <nav> tag, therefore it is not necessary to add the ARIA attribute "role=navigation". Even the Accessibility section on MDN (Mozilla Developer Network) states: "developers should prefer using the correct semantic HTML element over using ARIA if such an element exists" (Source: https://developer.mozilla.org/en-US/docs/Web/Accessibility/A...)
> "The problem faced by semantic tags is...the information that they carry is of very limited use for a screen reader."
That's precisely why those semantic HTML tags are important - so that screen readers navigate the page more easily (among other reasons).
> "...semantic is just a distraction"
Accessibility is not difficult if you follow HTML5 semantic markup. Compared to CSS, HTML5 semantic tags are easy. If you use HTML5 semantic tags, accessibility comes built-in - you get it for free.
Where accessibility fails is when you're using a JavasScript framework that generates non-semantic markup. Or you're using a CSS framework and your HTML markup is littered with endless <divs> rather than HTML semantic tags.
I recently posted this video playlist of quick accessibility tips for websites (each tip is just 1 minute). Many websites don't follow these best practices. However, I think people might be surprised by how simple and low-effort it is to incorporate these tips into any website.
Quick accessibility tests: https://www.youtube.com/playlist?list=PLTqm2yVMMUKWTr9XWdW5h...
Why can accessibility not be justified by profits? If you sell something, lack of accessibility will cause you to lose sales. How much you will lose depends on the demographics of the potential customers.
Ok sure, it'd be _nice_ if blind people could navigate my website. But... how nice? How many screenreaders can I reasonably expect as a % of traffic? What difference will it make to their quality of life? What decision process should I use to decide if _my_ website needs to support this? Does my personal blog to support a screen reader? What about this bank login page? This motivating information is a cruicual step in actually getting people to care enough to do something different.
A contrasting example is the discourse on "bloated" webpages being slow and hard to use on shitty old phones. The upshot is that if your website needs to download and run 1MB of JavaScript then someone viewing it on a 5 year old low cost smartphone on a crappy 3G is going to have a bad time. The statistics around how many people have bad internet motivated me to try and reduce download sizes.
It would also be nice to have a guide on convincing your boss to give you time to support accessibility.
The technical details are good (and this article is quite readable) but probably aren't the key bottleneck in more people writing accessible markup.
<edited for less unnecessary snark>
Rather than ask "why should I support screen readers" maybe ask "why would I actively break them and make my website unusable?"
I would need to be very _motivated_ to change my behaviour.
I amended my comment to be less snarky I was coming on a bit to strong.
Accessible design and development improves the lives of more people than you'd expect — and it goes a long way toward guaranteeing better usability for everyone. Gov.uk is a great example to this.
Anyway, if you're working on something with any type of scale, you should be hitting a11y standards as a matter of professional standards. Activist lawsuits in the US are doubling/tripling year over year, and you can expect EU legislation pretty soon too.
If you're just dicking around on a website with minimal traffic, it a matter of personal conscience and intended audience.
Semantic HTML != Semantic Web
Accessibility is important, learn this stuff!
At some point web designers (at the time in the early 00s web development was not as involved as it has been post Ajax) learned the term "semantic" and went wild with it (made you sound smart, and you could charge a little more than the unsophisticated shop down the road), without understanding the science behind data.
Instead we should have separation of concerns, with structured raw information (you know, like what we have in our relational and document databases), served with different represenations: for accessibility, for data access, for web, for syndication, for print, for reading devices, and so on.
Of course the web being the web, it needed to make this too in the most inefficient and multi-layered way (and leave it up to the web designers/devs to have "proper" semantic tags).
The Semantic web has failed. ARIA tags are important for accessibility though, but semantic HTML has been irrelevant for a decade, I haven't seen a single successful product based on that or a single use case that yielded something interesting SEO wise.
The article makes it clear what it is about and it’s not about the goals of the semantic web.
I think it's led to a lot of confusion, especially since it's often not clear which purpose is being referred to in a context.
Also, isn't the semantic web the reason that we get things like link previews with titles and content and images and... Considering [0] is a SEO website, and it was in the top three for my search for og:title I'd say the problem is rather that SEO infected the whole thing, not that it's not useful to SEO.