{"id":118,"date":"2025-08-11T15:08:02","date_gmt":"2025-08-11T15:08:02","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=118"},"modified":"2025-08-11T15:08:02","modified_gmt":"2025-08-11T15:08:02","slug":"incompetent-browsers-building-the-fallback-experience","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-building-the-fallback-experience\/","title":{"rendered":"Incompetent Browsers: Building the Fallback Experience"},"content":{"rendered":"<p>Previously: <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-grading\/\">Browser Grading<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-detection\/\">Historical Browser Detection<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/\">Requirements and Loaders<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-towards-a-complete-modern-solution-part-ii\/\">Detection and Fallback<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-a-complete-modern-solution\/\">The Complete Solution<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-gold-plating-the-css-loader\/\">Goldplating<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-dijon-mustard\/\">Dijon Mustard<\/a>.<\/p>\n<p>Remember when we talked about <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-grading\/\">browser grading<\/a>, Yahoo said:<\/p>\n<blockquote><p>C-grade is the base level of support, providing core content and functionality. It is sometimes called core support. Delivered via nothing more than semantic HTML, the content and experience is highly accessible, unenhanced by decoration or advanced functionality, and forward and backward compatible. Layers of style and behavior are omitted.<\/p><\/blockquote>\n<p>What does that look like in 2025?<\/p>\n<h3>Assumptions<\/h3>\n<p>Start by building your site mobile-first. The most important part of this is having a sensible order to your HTML, so that someone reading it from top to bottom can have a reasonable experience. You should also be setting your image sizes using CSS. In 2025, you probably are already doing both of these things, but if not, fix them.<\/p>\n<h3>What about my site navigation?<\/h3>\n<p>The first hurdle you&#8217;re going to hit is your site navigation. Almost all sites have navigation in their headers, and maybe they tuck it off to the side on mobile under a hamburger menu toggle, but it&#8217;s still there in the HTML, where people will have to scroll past it to get to the content. Luckily, there&#8217;s a pure HTML solution that handles this and improves your site too.<\/p>\n<p>Put a &#8220;skip navigation&#8221; link as the first thing in your page, right after the opening <tt>body<\/tt> tag. With the appropriate styles it will be invisible to most users, while still appearing if anyone is trying to navigate the page with their keyboard or a screen reader. For people in the fallback experience, they can skip the large nested lists that have links to every page on your site and go right to the content. Just make sure you put <tt>id=main<\/tt> on the tag where your content actually starts.<\/p>\n<pre>\r\n<code>\r\n&lt;div class=\"skip-navigation\"&gt;\r\n  &lt;a href=\"#main\" class=\"button\"&gt;Skip to main content&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n<\/code>\r\n<\/pre>\n<pre>\r\n<code>\r\n.skip-navigation {\r\n  a {\r\n    position: absolute;\r\n    top: 0;\r\n    transform: translateY(-100%);\r\n    transition: transform .2s ease-out;\r\n\r\n    &:focus {\r\n      z-index: 100000;\r\n      transform: translateY(0);\r\n    }\r\n  }\r\n}\r\n<\/code>\r\n<\/pre>\n<h3>The image problem<\/h3>\n<p>At this point you&#8217;ll look at your pages and see that you&#8217;re actually shipping arbitrarily sized images which don&#8217;t really flow right with your content. And if you check the fallback experience on mobile, they probably are wider than the phone screen.<\/p>\n<p>The answer is: set a width of 320 in the HTML.<\/p>\n<p>Why 320? That&#8217;s the pixel width of the original iPhone, so any smartphone will be able to display it without horizontally scrolling. If you don&#8217;t set the height, it will scale while maintaining the aspect ratio. If you&#8217;re setting image sizes with CSS in your normal presentation, it will override the size set in the HTML attribute.<\/p>\n<p>When I built this, we were using WordPress. This code got us most of the way there. (We had some custom Gutenberg Blocks that also needed to be updated to generate the right widths.)<\/p>\n<pre>\r\n<code>\r\nclass Image_Utils\r\n{\r\n    public static function get_image_fallback_sizes($id)\r\n    {\r\n        \/\/ Default to empty strings so fallback sizes are ignored if unset.\r\n        $width  = '';\r\n        $height = '';\r\n\r\n        $meta = wp_get_attachment_metadata($id);\r\n\r\n        if ($meta) {\r\n            $width  = $meta['width'];\r\n            $height = $meta['height'];\r\n        }\r\n\r\n        if (empty($width)) {\r\n            \/\/ SVG files do not get size metadata.\r\n            $width = 320;\r\n        }\r\n\r\n        if ($width &gt; 320) {\r\n            $scale  = 320 \/ $width;\r\n            $width  = (int) ($width * $scale);\r\n            $height = (int) ($height * $scale);\r\n        }\r\n\r\n        return array($width, $height);\r\n    }\r\n\r\n    public static function rewrite_image_tag($id, $html)\r\n    {\r\n        list($width, $height) = self::get_image_fallback_sizes($id);\r\n\r\n        $html = preg_replace(\r\n            '\/width=[\\'\"]?\\d*[\\'\"]?\/',\r\n            'width=\"' . $width . '\"',\r\n            $html\r\n        );\r\n        $html = preg_replace(\r\n            '\/height=[\\'\"]?\\d*[\\'\"]?\/',\r\n            'height=\"' . $height . '\"',\r\n            $html\r\n        );\r\n\r\n        return $html;\r\n    }\r\n}\r\n<\/code>\r\n<\/pre>\n<pre>\r\n<code>\r\nfunction post_thumbnail_html($html)\r\n{\r\n\t$id = get_post_thumbnail_id();\r\n\treturn Image_Utils::rewrite_image_tag($id, $html);\r\n}\r\nadd_filter(\r\n\t'post_thumbnail_html',\r\n\t'post_thumbnail_html'\r\n);\r\n<\/code>\r\n<\/pre>\n<h3>Browse happy<\/h3>\n<p>Since we&#8217;re here, we might as well tell them to update their browser. Put this right after the skip link.<\/p>\n<pre>\r\n<code>\r\n&lt;p class=\"browserupgrade\"&gt;\r\n  You are using an &lt;strong&gt;outdated&lt;\/strong&gt; browser. Please\r\n  &lt;a href=\"https:\/\/browsehappy.com\/\"&gt;upgrade your browser&lt;\/a&gt; to\r\n  improve your experience and security.\r\n&lt;\/p&gt;\r\n<\/code>\r\n<\/pre>\n<h3>Fallback styles and scripts?<\/h3>\n<p>I don&#8217;t think you should use fallback styles or scripts. Doing so means you now have three versions of your site to test. But you can. If you do, here are some hints:<\/p>\n<p>For CSS, imagine you&#8217;re targetting IE6, or Netscape 4. No CSS3 Selectors. No Grids. No Web Fonts. SCSS Features are okay though; That&#8217;s build-time<\/p>\n<p>For JavaScript, dig out the old <tt>addEvent<\/tt> fallback that registered events appropriately in IE. Don&#8217;t forget to specify the third parameter as <tt>false<\/tt> when you call <tt>addEventListener<\/tt>; Opera doesn&#8217;t like it if it&#8217;s omitted. Always check for the existence of any API you call. You probably won&#8217;t have <tt>QuerySelectorAll<\/tt> and will have to stick with <tt>getElementsByClassName<\/tt>. Remember to register events for <tt>load<\/tt>, as not all browsers have <tt>DOMContentLoaded<\/tt>. And Test, test, test.<\/p>\n<link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/styles\/default.min.css\">\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/highlight.min.js\"><\/script><br \/>\n<script>hljs.highlightAll();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously: Browser Grading, Historical Browser Detection, Requirements and Loaders, Detection and Fallback, The Complete Solution, Goldplating, Dijon Mustard. Remember when we talked about browser grading, Yahoo said: C-grade is the base level of support, providing core content and functionality. It is sometimes called core support. Delivered via nothing more than semantic HTML, the content and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":12,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/118\/revisions\/131"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}