
Anyone who has been working in web development for a while will be familiar with this now-legendary meme from xkcd. It illustrates just how fragile the technical infrastructure of a website or app can be.
That’s why it’s important to use fallbacks to build a robust front end, either to quietly and discreetly avoid the error or to communicate it to our users.
The issue of robustness is so important that it is actually one of the four principles of the Web Content Accessibility Guidelines (WCAG)! So how can we provide a modern front end without excluding users who use browsers and hardware that are only partially compatible or not compatible at all?
The answer lies in implementing multiple safety nets in HTML, CSS, and JavaScript, so that we are prepared for the worst-case scenario and can provide our users with the best possible experience.
The main difference between the use of fallbacks and progressive enhancement lies in the directions in which the two approaches aim.
While progressive enhancement is about automatically making certain features available as long as they are supported (forward), fallbacks are about what happens when something is not available or accessible (backward).
For example, we can offer a shiny new CSS feature that makes interacting with our website even more enjoyable. But if this feature isn’t available in some browsers, no one will notice, because a version that’s supported by older browsers is available instead. Progressive enhancement in action!
Another example: If JavaScript cannot be loaded for a critical application, we provide a fallback that either offers basic functionality or, at the very least, displays a message explaining what the problem is.
While one doesn’t necessarily exclude the other, their approaches are quite different. My personal recommendation is to always work with progressive enhancement as a guiding principle and to consider fallbacks whenever possible.
To evaluate new features intended for production, we must first determine the extent to which a feature is supported.
Since 2023, the Interop project has been in place, in which major browser vendors collaborate to release new features for the web platform more or less simultaneously. This serves as a general overview.
Related to this is the Baseline project, which shows, for specific features, the extent to which they are supported and how long they have been available. However, it is extremely important to note that it does not track browser support for accessibility features built into the web platform!
Another important resource for checking compatibility is Can I Use. Here, you can get a quick overview of browser support for various features.
Let’s start with the foundation of the World Wide Web: HTML. Modern tech stacks place a strong emphasis on client-side rendered JavaScript, which can offer many benefits to users in more complex applications. However, there can be just as many reasons why JavaScript might fail to load, so we need a strategy to address this.
In the event of a total failure, we should redirect users to an appropriate error page where they receive information about what went wrong and what not only they, but also we, can do to prevent this in the future. If you want to know how to write error messages that actually help users rather than frustrate them, check out Amy Hupe’s amazing article.
Because this kind of error feedback is so essential, the static site generator Astro, for example, offers the ability to add custom 404 and 500 error pages. You could discuss other possible error codes with your back-end team to implement additional safeguards.
But what if we also want to make individual sections or components more robust in case JavaScript isn’t available? For this, we can use the
If JavaScript is not available, we will still display the components, but explain in simple and clear terms that they cannot be loaded at the moment and why that is the case.
We give users the option to reload that specific component so they can resolve the issue themselves. We also offer them the option to contact us directly via email if the problem persists.
Here, you could add a pre-written text to the tag, which could include, for example, an internal error code specific to that component in the subject line and have a corresponding body text. This way, they would only need to click the button and could send the email directly using their email program of choice. Support would then have all the necessary information to resolve the issue without needing to ask for additional details.
No matter what your use case is, this pattern can be universally adapted and extended, and ensures that it works even if everything else goes wrong.
Let’s stick with our fictional dashboard component. Say we want to use the CSS anchor() function to display the data, which is fairly new and has solid, but not great, browser support.
Since this is a feature that can include multiple attributes, we need to check whether the ones we want to use are actually available. This is exactly where the @supports CSS rule comes into play! Here, it’s recommended to use the and operator: It should only be used if all attributes are available. If not, the more robust CSS with better browser support is used.
Here’s another fictional, simplified example:
This approach also serves as documentation: Once the feature has been around for a while and is more well-established, you can look directly for the @supports rule in your code base and check whether it is still necessary, along with its fallback.
Javascript offers us the ability to make our front end even more resilient with minimal effort. To do this, we can use the if…else statement to check whether a specific feature is available. For example, if we want to use the relatively new Navigation API, we could check for it as follows:
If we use a component-based approach, where HTML, CSS, and JavaScript are all contained in a single file, we can also use the conditional ternary operator to display this component or parts of it, or do nothing at all.
Our fictional dashboard component relies on data from the back end, and it only makes sense to display it once the data has successfully reached the front end. So, for example, we could do the following:
Depending on the use case, it makes sense to display a fallback or even nothing at all. You could also define the conditions more specifically, depending on how robust you want it to be.
My experience so far at various companies has been that the topics of fallbacks and robustness haven’t played as prominent a role as they really should have.
This is surprising, because as we’ve learned, it doesn’t take much to implement various safety nets on the front end, as long as you think ahead about what (shouldn’t) happen if something goes wrong.
HTML, CSS, and JavaScript provide us with specific elements and functions to ship robust code and thus avoid upsetting our users and, in the worst case, causing them to turn away from us.
Speaking of noscript environments… any chance of fixing the CSS Tricks stylesheet so code blocks aren’t illegible when the client-side syntax highlighting isn’t run? It’s currently white-on-off-white. Also kill the scrollbar-color which is dark mode and was never useful anyway.
Thanks! We’ve got a patch in and will ship it on next deployment.
Am I crazy or is a button to refresh in a noscript tag kind of useless because it would itself need JavaScript to function and actually refresh the page?
A link to the current page would maybe work better…
Oh and the comment previews on this site also show white text on a light yellow background (dark mode on Chrome/Android)
I was thinking the same. Yes, a link would work better probably.
form> tag — but this could lead to unintended consequences when parameters are getting removed.
The noscript dashboard pattern is handy, with one catch for anyone copying it as-is: when JavaScript is off, the second section is still plain HTML, so both sections render and two headings share the id user-statistics-heading, which breaks the aria-labelledby reference. Rendering the real section from JavaScript, or hiding it behind a class that a one-line inline script adds to the html element, avoids that. I’d also add that the most common failure we see on live sites isn’t JavaScript being turned off, it’s one blocked or slow third-party script taking the whole bundle down with it. noscript never fires in that case, so a timeout-based fallback message earns its place too.
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.
Notify me of follow-up comments by email.
Join our community of front-end nerds! You'll get into the Discord server and member-only perks and free swag.
Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.
Get your t-shirts, prints, stickers, and yes, even mousepads at the brand-spankin' new CSS-Tricks store, now open.
CSS-Tricks is powered by a small team of real-life humans.
Handwritten. Twice a month. No spam. See past issues →