<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matt Dawkins on Medium]]></title>
        <description><![CDATA[Stories by Matt Dawkins on Medium]]></description>
        <link>https://medium.com/@matt.dawkins?source=rss-6ab0b71ca3ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bpGkPYZCRYqIMqvf</url>
            <title>Stories by Matt Dawkins on Medium</title>
            <link>https://medium.com/@matt.dawkins?source=rss-6ab0b71ca3ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:04:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matt.dawkins/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tips on code refactoring]]></title>
            <link>https://medium.com/@matt.dawkins/tips-on-code-refactoring-8c5c1ea0bada?source=rss-6ab0b71ca3ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c5c1ea0bada</guid>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Matt Dawkins]]></dc:creator>
            <pubDate>Wed, 14 Jun 2023 12:30:40 GMT</pubDate>
            <atom:updated>2023-06-14T12:30:40.095Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bS05nlLHcziKdmgd" /><figcaption>Photo by <a href="https://unsplash.com/@kensuarez?utm_source=medium&amp;utm_medium=referral">Ken Suarez</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I needed to work on an old website project recently. It needed a few small changes making to it. And then I looked at the code, and down the rabbit-hole I tumbled…</p><h3>The problem with legacy code</h3><p>Of course, no one actually writes legacy code. They write new code. It becomes “legacy” with the inevitable passing of time.</p><p>But there’s nothing wrong with my old code, I hear you mumble nervously. After all, it was good enough first time round. It’s not like fruit that goes off if you leave it alone — computer code stays just as fresh no longer how long it’s around. Well, sort of.</p><p>The thing is, code doesn’t exist in isolation. Even the finest quality code will eventually be redundant because the system it runs in will have been updated. Operating systems improve, APIs evolve, third-party libraries get updated (or worse, abandoned). And our expectations change too — what might have looked beautiful to our eyes 10 years ago might look downright dated today.</p><p>In short, if you want your project to stay alive, you need to keep it updated. And that probably means refactoring your code.</p><h3>Identifying the smells</h3><p>The first step in refactoring an old project is to perform an audit. That’s just a fancy way of saying that we take a look at everything and figure out what’s there. I’m a web developer, so I’m thinking specifically in terms of projects that use HTML, PHP, JavaScript, that kind of thing. But the underlying principles would probably apply to other languages too. Here are some things to watch out for:</p><ul><li><strong>How big are the files?</strong> If there is too much going on in any one file, it’ll become hard to find what you’re looking for. There are exceptions, sure, but a file with more than a few hundred lines of code might well be a prime candidate for splitting up.</li><li><strong>Is the code properly formatted?</strong> “I’ll tidy that up later” is a well-meaning mantra. But all too often the end of a project is rushed, bad habits creep in, and it’s just never a priority. And then you find a problem, but you’re too ashamed of your messy code to actually ask anyone for help.</li><li><strong>How up-to-date are the dependencies?</strong> Those third-party libraries might have been the latest and greatest at the time, but they might need updating by now.</li><li><strong>Is your code repeated?</strong> Is your code repeated? Is your code repeated? If the same code appears more than twice, it could probably be improved by putting it into a loop or a reusable function or template.</li><li><strong>Do your variable names make sense?</strong> They probably seemed intuitive at the time, but do you still remember what $irefers to? What was $mk-bl3 an abbreviation of again? Naming things is hard.</li><li><strong>Are there enough comments?</strong> First time round, you probably knew what your code does. But months (or years) later, how clear is it?</li></ul><h3>Tools that’ll help</h3><p>If you use <a href="https://getcomposer.org/doc/">Composer</a>, you can quickly run a report on how out-of-date your dependencies are: composer show -oD . The output will show you what versions are installed right now, and what the latest versions available are, so you make a decision about whether you need to worry about it. All being well, a simple composer update will be all you need next, and a check to make sure nothing is unexpectedly broken.</p><p><a href="https://docs.npmjs.com/cli/v9/commands">NPM</a> has a similar command: npm outdated. If any of your packages are old, this will tell you just how old. For more information, npm list will list all your dependencies and the version currently installed, and npm audit will check to see whether any of your dependencies has reported security issues. And then a quick npm update will update everything. But don’t forget to check that it all still works.</p><p>For code formatting, there are a few options depending on the language you’re using. I like to use <a href="https://prettier.io/">Prettier</a> to automatically format my HTML, CSS and JavaScript. I simply run npx prettier --check ./src to see which files need my attention, or npx prettier --write ./src if I trust Prettier to just get on with fixing it for me. Which I normally do. And for PHP source code, I normally turn to <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP CodeSniffer</a>. Both these tools play nicely with <a href="https://code.visualstudio.com/">VSCode</a>, so they’re just a keyboard shortcut away.</p><p>To take things to the next level, you’ll probably want to turn to a static analysis tool. These interrogate your code much more deeply, not just looking at formatting but what it actually does, which can be invaluable. For PHP projects, take a look at <a href="https://phpstan.org/">PHPStan</a> or <a href="https://psalm.dev/">Psalm</a>. For JavaScript, <a href="https://eslint.org/">ESLint</a> is probably your go-to. You can learn a lot through this process too.</p><h3>Conclusion</h3><p>Refactoring is a fact of life for a programmer. It’s not something to be ashamed of. So build it into your workflow, include it in your project management, and familiarise yourself with the tools that work for you. Your code will thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c5c1ea0bada" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using data attributes instead of CSS classes]]></title>
            <link>https://medium.com/@matt.dawkins/using-data-attributes-instead-of-css-classes-78476535b111?source=rss-6ab0b71ca3ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/78476535b111</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Matt Dawkins]]></dc:creator>
            <pubDate>Fri, 21 Apr 2023 15:51:37 GMT</pubDate>
            <atom:updated>2023-05-10T13:14:16.705Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qKmYm7NkxhP1l4RR" /><figcaption>Photo by <a href="https://unsplash.com/@jacksonsophat?utm_source=medium&amp;utm_medium=referral">Jackson Sophat</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We’ve been using classes in our CSS selectors since forever. And with the rise of “utility class” frameworks like Tailwind, our HTML markup has become swollen with classes. But is there a better way?</p><p>Let’s consider something we see all the time — a button link. We might start with a simple anchor tag:</p><pre>&lt;a href=&quot;/page&quot;&gt;Go somewhere&lt;/a&gt;</pre><p>How do we make that look like a button?</p><h3>IDs</h3><p>If you know a little about CSS already, you’ll probably know that this isn’t going to be our best solution. But let’s just take a look at it, for completeness.</p><p>We can apply an id property to our link element, and use that to target it with CSS.</p><pre>&lt;a href=&quot;/page&quot; id=&quot;my-button&quot;&gt;Go somewhere&lt;/a&gt;</pre><pre>#button {<br>  /* styles go here */<br>}</pre><p>I won’t go into the actual styling of the button here, that’s a separate topic really. What I’m focusing on is the use of the id. And this works. But the definition of an id is that it should only appear once in our page’s markup. That means that none of those styles are reusable.</p><p>An id attribute can also only contain one value, with no spaces. That means an element can only have one id.</p><p>IDs have their place, but it’s not quite what we’re looking for. So how do we make reusable styles?</p><h3>Classes</h3><p>If you’ve read the title, it’ll come as no surprise that this isn’t the solution we’re heading for either. But, again, let’s look at it for a moment to understand how we should — and shouldn’t — use classes.</p><pre>&lt;a href=&quot;/page&quot; class=&quot;button&quot;&gt;Go somewhere&lt;/a&gt;</pre><pre>.button {<br>  /* styles go here */<br>}</pre><p>So far, it’s a subtle difference. The difference with the class property is that it can contain multiple values, and be used on multiple elements. So we could do something like this:</p><pre>&lt;a href=&quot;/page&quot; class=&quot;button is-blue is-rounded&quot;&gt;Go somewhere&lt;/a&gt;<br>&lt;a href=&quot;/another&quot; class=&quot;button is-orange&quot;&gt;Go elsewhere&lt;/a&gt;</pre><pre>.button {<br>  /* common styles go here */<br>}<br>.is-blue {<br>  background: blue;<br>  color: white;<br>}<br>.is-orange {<br>  background: orange;<br>  color: white;<br>}<br>.is-rounded {<br>  border-radius: 1em;<br>}</pre><p>This is a great improvement. Each element can be given the button class so they both inherit the same base styles. And then we can mix and match the other classes to provide the variations we want.</p><p>The problem is that button describes a family, whereas is-blue is intended to describe a property it has. Stuffing them both into classes works, but we lose some of the meaning. And, more importantly, there is nothing to stop us doing something silly:</p><pre>&lt;a href=&quot;/page&quot; class=&quot;is-blue is-orange&quot;&gt;What am I?&lt;/a&gt;</pre><p>The markup is valid, but that doesn’t mean it’ll work. So what’s the alternative?</p><h3>Data attributes</h3><p>Thanks for your patience. We’re here now.</p><p>HTML elements can have all sorts of attributes. So far we’ve used href , id and class . And there are lots of other attributes defined in the HTML spec. We can’t add our own willy-nilly, because that would potentially lead to conflicts and bad practice. But HTML does allow us to use custom “data attributes”, which is an attribute that starts with data- .</p><p>In our case, we could use a data attribute to set the “theme” of the button, making it clearer that each element should only have one value.</p><pre>&lt;a href=&quot;/page&quot; class=&quot;button&quot; data-theme=&quot;blue&quot;&gt;Go somewhere&lt;/a&gt;</pre><pre>.button {<br>  /* base styles go here */<br>}<br>[data-theme=&quot;blue&quot;] {<br>  background: blue;<br>  color: white;<br>}<br>[data-theme=&quot;orange&quot;] {<br>  background: orange;<br>  color: white;<br>}</pre><p>I really like this approach, because it makes it really clear in the HTML markup that button is the type of element, whereas data-theme is a property of that element.</p><p>Another cool thing about data attributes is that they don’t even need to have a value, which works great for boolean properties.</p><pre>&lt;a href=&quot;/page&quot; class=&quot;button&quot; data-theme=&quot;blue&quot; data-rounded&gt;Go somewhere&lt;/a&gt;</pre><pre>[data-rounded] {<br>  border-radius: 1em;<br>}</pre><h3>Working example</h3><p>Let’s put all that together, with a few other spicy bits thrown in…</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FabRpbJK%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DabRpbJK&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FabRpbJK&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FabRpbJK-512.jpg%3Fversion%3D1682091068&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/583c2a858d37ec9c5bd6b22dd4d42c70/href">https://medium.com/media/583c2a858d37ec9c5bd6b22dd4d42c70/href</a></iframe><p>I’ve defined two options for data-theme , which each set CSS variables for the foreground and background colours of the element. In the CSS, note that the [data-theme] selector doesn’t care what sort of element it’s applied to, so we can apply it to .button or section or anything else we fancy.</p><p>In contrast, the data-rounded property only really makes sense for certain types of element, which is why we’ve defined it as .button[data-rounded] . Even if we mistakenly used the attribute on a section , it wouldn’t apply.</p><p>There are a few other nice little touches in that demo too, like using filter for the hover and active button states. Take a look at the source code of that demo and see what else catches your eye!</p><p>—</p><h3>EDIT:</h3><p>Several people have commented about the use of the class .is-blue , specifically about how bad it is. And I agree. The example here is deliberately trivial to make it accessible and help people apply the logic to their own contexts. But given how many comments there have been, maybe my example could have been clearer…</p><p>The point I am making, i.e. my rationale for using data attributes instead of classes, is about how there are some situations where it’s better to think in terms of individual custom “properties” on an element. If we only use classes, there is nothing stopping us from applying multiple conflicting classes, for example:</p><pre>&lt;div class=&quot;is-primary is-secondary is-visible is-hidden is-blue is-orange&quot;&gt;&lt;/div&gt;</pre><p>It’s valid markup, but some of those classes ought to be mutually exclusive. An element can’t be primary and secondary at the same time. Or hidden and visible at the same time. Or blue and orange at the same time. And classes alone won’t enforce that.</p><p>That’s where I believe data attributes are helpful. They allow us to set custom properties for an object, which by design force us to pick one valid option.</p><pre>&lt;div class=&quot;thing&quot; data-theme=&quot;primary&quot; data-hidden data-accent=&quot;blue&quot;&gt;&lt;/div&gt;</pre><p>Using data attributes doesn’t solve the problem of naming conventions. It simply allows us to avoid collisions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78476535b111" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shedding some light on dark mode]]></title>
            <link>https://medium.com/@matt.dawkins/shedding-some-light-on-dark-mode-7b06bc4ae2be?source=rss-6ab0b71ca3ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b06bc4ae2be</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[dark-mode]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[colour]]></category>
            <dc:creator><![CDATA[Matt Dawkins]]></dc:creator>
            <pubDate>Fri, 14 Apr 2023 14:11:54 GMT</pubDate>
            <atom:updated>2023-04-14T14:11:54.483Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IweK1JzLFUgZ_4Zk" /><figcaption>Photo by <a href="https://unsplash.com/fr/@hg_photo?utm_source=medium&amp;utm_medium=referral">Herbert Goetsch</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>What’s the best way to build a web site with a “dark mode”? Let’s look at a couple of CSS and JavaScript solutions.</p><p>Before we get stuck in, though, it’s worth reminding ourselves of why anyone might want a dark mode.</p><ul><li><strong>Time of day</strong>. Blue light is claimed to stimulate your brain to wake up, so cutting back on those light frequencies at night might help you get a better night’s sleep.</li><li><strong>Accessibility</strong>. There are some eyesight conditions that mean reading light text on a dark background is clearer than the traditional dark text on a light background.</li><li><strong>Energy efficiency</strong>. OLED screens use less electricity to display dark pixels, so for those devices it can extend battery life to use a darker colour scheme.</li><li><strong>Personal preference</strong>. Because some of us just dig that Matrix aesthetic.</li></ul><p>So why not just make your website permanently dark? There are certainly some website that use an exclusively dark colour scheme. But that might not always be an option.</p><ul><li><strong>Time of day</strong>. Providing a dark mode at night is great, but most people use websites during the day.</li><li><strong>Accessibility</strong>. Just as there are some conditions that favour dark mode, there are others that favour light colour schemes.</li><li><strong>Contrast</strong>. If you’re in a really bright context, such as outdoors on a sunny summer’s day, a light colour scheme might be easier to see.</li><li><strong>Personal preference</strong>. Because some people never got on that Matrix bandwagon in the first place.</li></ul><h3>Detecting dark mode with CSS</h3><p>Your browser likely already has the ability to “request” a dark version of a website. It might be a setting in your browser’s preference, or it might even follow your operating system. But <a href="https://caniuse.com/?search=prefers-color-scheme">most browsers</a> will recognise this:</p><pre>@media screen and (prefers-color-scheme: dark) {<br>  /* dark colour scheme definitions go here */<br>}</pre><p>Nice and easy. All you have to do is put your colour styles in there and your website will show in dark more if the browser asks for it.</p><h3>Detecting dark mode with JavaScript</h3><p>This is almost as easy as in CSS:</p><pre>if (<br>  window.matchMedia &amp;&amp;<br>  window.matchMedia(&quot;(prefers-color-scheme: dark)&quot;).matches<br>) {<br>  // do something<br>}</pre><p>You can even listen for changes, in case the user changes their preference in the browser settings:</p><pre>window<br>  .matchMedia(&quot;(prefers-color-scheme: dark)&quot;)<br>  .addEventListener(&quot;change&quot;, (event) =&gt; {<br>    console.log(event.matches); // will be either true or false<br>  });</pre><p>This is especially handy if the browser is set to follow the operating system, as that often changes automatically with the time of day, so your site will automatically use dark mode after sunset. Though that’s not necessarily guaranteed — we’ll get to that in a moment.</p><h3>Manually toggling dark mode</h3><p>The above solutions are great for detecting if the browser requests a dark version of the site, but those settings are hidden away and a lot of users won’t even know they have those options, so we can’t always rely on that. So another solution would be to have a toggle control on your website so that people can easily choose.</p><p>My recommendation here would be to use JavaScript to watch the state of a checkbox (or other control) and toggle a CSS class on the body element.</p><pre>document.getElementById(&quot;darkmode&quot;).addEventListener(&quot;change&quot;, (event) =&gt; {<br>  if (event.target.checked) {<br>    document.body.classList.add(&quot;prefers-dark-mode&quot;);<br>  } else {<br>    document.body.classList.remove(&quot;prefers-dark-mode&quot;);<br>  }<br>});</pre><pre>.prefers-dark-mode {<br>  background-color: black;<br>  color: white;<br>}</pre><p>Or, if you’re using a reactive framework like Vue.js:</p><pre>&lt;template&gt;<br>  &lt;div :class=&quot;{&#39;prefers-dark-mode&#39;: darkMode}&quot;&gt;<br>    &lt;input type=&quot;checkbox&quot; v-model=&quot;darkMode&quot; id=&quot;dark-mode&quot;&gt;<br>    &lt;label for=&quot;dark-mode&quot;&gt;Enable dark mode&lt;/label&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>export default {<br>  data() {<br>    return {<br>      darkMode: false,<br>    };<br>  },<br>}<br>&lt;/script&gt;<br><br>&lt;style&gt;<br>  .prefers-dark-mode {<br>    background-color: black;<br>    color: white;<br>  }<br>&lt;/style&gt;</pre><h3>Detecting but providing a manual override</h3><p>Combining the solutions above gives us the best of both worlds. The manual toggle is great for keeping your visitors in control and being transparent about why the site is using a dark colour scheme, but we can use the CSS detection solution to preset it when loading the page. To demonstrate this, I’ve put together this little demo:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FBaqBvPr%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DBaqBvPr&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FBaqBvPr&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FBaqBvPr-512.jpg%3Fversion%3D1681466843&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/96236341c972ad821f4556551ace95be/href">https://medium.com/media/96236341c972ad821f4556551ace95be/href</a></iframe><h3>There’s more to dark mode than black and white</h3><p>As you’ll see in the demo above, dark mode can affect more than just inverting black and white. In all likelihood, your site’s design will include other colours too, depending on your branding or design. It’s worth remembering that those colours might need dulling down in dark mode as well, otherwise they could be overwhelming.</p><p>We’re probably used to thinking in terms of RGB (the Red, Green, and Blue components of a colour). In programming terms, we generally specify the amounts of each colour as being in a range from 0 to 255. That top value is simply because that’s the biggest number we can generate in 8 bits of binary, which is how computers count. In other contexts we might measure it in percent, which might make more intuitive sense. In fact, CSS already allows us to specify colours in RGB values with either measuring system; rgb(0, 255, 127) is the same as rgb(0%, 100%, 50%) . And of course we can use hexadecimal to shorten that to #00ff7f . But while that’s great for computers, it’s hardly intuitive for us humans.</p><p>This is why I would recommend using hsl() to define your colours in CSS. If you’ve not come across HSL before, it stands for Hue, Saturation, and Lightness. It’s just another way of defining a colour. The hue is a value from 0 to 360 that defines a position in a colour wheel, with red being at 0, green being 120, and blue at 240. The saturation is measured as a percent, with 0% meaning no saturation (i.e. grey) and 100% meaning as much colour as possible. Finally, the lightness value defines how light or dark the colour ends up, with 0% meaning black and 100% meaning white. Putting all that together, the example colour above could be written as hsl(150, 100%, 50%) , which says that we want a blue-green colour, and lots of it (full saturation, not too dark, not too light).</p><p>The advance of using HSL in the context of a light/dark mode website is that we can take our light mode colour scheme and easily tweak it to suit a dark mode. For example, a darker version of my example colour above might be hsl(150, 100%, 30%) . Note how the hue and saturation have stayed the same, we’ve just reduced the lightness value, making it really clear that it’s just a darker version of the same colour. Certainly clearer, I think, than trying to decipher the relationship between #00ff7f and #00994d .</p><p>In my demo above, I’ve used CSS variables to define my hues, and then used an appropriate saturation and lightness in my actual styles depending on the context. I’ve used Sass there, but it’s essentially the same idea as this:</p><pre>:root {<br>  --blue: 215;<br>}<br><br>.myElement {<br>  background-color: hsl(var(--blue), 100%, 50%); /* bright blue */<br>  color: hsl(var(--blue), 0%, 100%); /* that&#39;s white, BTW! */<br>}<br><br>.prefers-dark-mode .myElement {<br>  background-color: hsl(var(--blue), 50%, 30%); /* dull, dark blue */<br>}</pre><p>You might need to liaise with your designer on colour choices, as they’ll probably just think in light mode and provide you with RGB codes (or hex codes if they think they’re being helpful). But with a little collaboration you can make your entire colour scheme adaptable so that it looks fantastic on any device, at any time of day.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b06bc4ae2be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hero sections — accessible, semantic and performant]]></title>
            <link>https://medium.com/@matt.dawkins/hero-sections-accessible-semantic-and-performant-c04502e16f40?source=rss-6ab0b71ca3ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/c04502e16f40</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Matt Dawkins]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 13:52:03 GMT</pubDate>
            <atom:updated>2023-04-06T13:52:03.185Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hero sections — accessible, semantic and performant</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*h1_DfJazyc_-CxcP" /><figcaption>Photo by <a href="https://unsplash.com/@exxteban?utm_source=medium&amp;utm_medium=referral">Esteban López</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We all know about hero sections, right? Most (though not all) web pages have them, usually with a big title or heading, sometimes with a subheading or additional content, and often with either a background colour or image background. You’ll find them at the top of the page, and sometimes further down too, either introducing the page or drawing attention to something else.</p><p>And making them is pretty easy. Let’s have a go:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FVwEwpEm%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DVwEwpEm&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FVwEwpEm&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FVwEwpEm-512.jpg%3Fversion%3D1680774461&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/20caeec442b4c99febfa70213dd24e4b/href">https://medium.com/media/20caeec442b4c99febfa70213dd24e4b/href</a></iframe><p>We’ve got a background image, centred and covering the whole hero element. We’ve made the text a little larger and centred that too. And to ensure we’ve got some contrast between the background and the text, we’ve applied a subtle drop shadow to the text.</p><p>And if that’s what the design called for, then we’ve nailed it. Job done.</p><p>But could it be better? Sure.</p><h3>Semantic markup</h3><p>Sure, that &lt;div&gt; works, but it has no intrinsic meaning. A better choice might have been &lt;section&gt; , as that tells the browser something about its purpose on the page. Note that a &lt;section&gt; also needs either an aria-label=&quot;&quot; or an aria-labelledBy=&quot;&quot; to be <a href="https://www.scottohara.me/blog/2021/07/16/section.html">properly valid</a>.</p><p>We’ve also got an &lt;h4&gt; tag there for the subtitle, which gives us text at the right size. There are problems with that too. Headings are not merely presentational, they describe the hierarchy and structure of the page. We could swap it for an &lt;h2&gt; , but again I think that’s missing the point — the content we’re putting there isn’t introducing a new section of the page.</p><blockquote>“<a href="http://www.w3.org/html/wg/drafts/html/master/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements">h1</a>–<a href="http://www.w3.org/html/wg/drafts/html/master/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements">h6</a> elements <strong>must not</strong> be used to markup subheadings, subtitles, alternative titles and taglines unless intended to be the heading for a new section or subsection.” — <a href="http://html5doctor.com/howto-subheadings/">html5doctor.com</a></blockquote><p>So for semantic purposes, this might be better as a &lt;div&gt; with a suitable class, so that we can make it look how we want without sacrificing or confusing the underlying meaning.</p><p>In fact, you could make a similar case for the &lt;h1&gt; if it doesn’t introduce a new section of the page. Which in our case it doesn’t. So a good alternative might be the &lt;header&gt; element, again suitably styled to our needs.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FoNaNWNB%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DoNaNWNB&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FoNaNWNB&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FoNaNWNB-512.jpg%3Fversion%3D1680775739&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0cc7e17bf5229587c097ddb60d5b377/href">https://medium.com/media/b0cc7e17bf5229587c097ddb60d5b377/href</a></iframe><h3>Modern image formats</h3><p>So far our hero is defining its background image in the CSS. It’s a JPG file, and it’s nice and big so that it’s crisp on big screens. But we can do better.</p><p>For starters, the WEBP format is generally superior to JPG. It gives you a smaller file without sacrificing quality, and is <a href="https://caniuse.com/webp">supported on all modern browsers</a>. Now, sure, we could just replace the image in our CSS so that it’s referencing a WEBP file, but that won’t help older browsers that don’t support it. CSS can’t support multiple image sources, but HTML can, so this is where the &lt;picture&gt; element saves the day.</p><pre>&lt;picture&gt;<br>  &lt;source srcset=&quot;/path/to/image.webp&quot; type=&quot;image/webp&quot;&gt;<br>  &lt;img src=&quot;/path/to/image.jpg&quot;&gt;<br>&lt;/picture&gt;</pre><p>Pretty straightforward. If the browser supports WEBP, it’ll use that. Otherwise, it’ll fall back to the JPG.</p><p>Now we just need to position it as the background to our hero section. I’ve used position: relative; on the .hero section to define the scope, position: absolute; on the background image element, and position: relative; on everything else to ensure it appears above the image. Also note the use of object-size: cover; on the image, which allows it to do the same job as the background-size: cover; we had in our CSS before.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FYzJzVqr%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DYzJzVqr&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FYzJzVqr&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FYzJzVqr-512.jpg%3Fversion%3D1680776860&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a512a6472c397ecf0166c68e9cf479a/href">https://medium.com/media/6a512a6472c397ecf0166c68e9cf479a/href</a></iframe><p>What’s the advantage of this approach? Well, serving more efficient images means potentially less bandwidth usage and faster loading times. And because the image is an actual element, we can also include an alt attribute to tell screen readers what the picture is of.</p><p>Oh, and one little bonus aside — this approach would work equally well for using a video as a hero background. Just sayin’.</p><h3>Responsive images</h3><p>Before we finish, we ought to think about different device sizes too, seeing as there are <a href="https://gs.statcounter.com/platform-market-share/desktop-mobile-tablet">more mobile users than desktop users</a>. After all, there’s no point serving up a massive image to a mobile device that won’t be able to show all that glorious detail.</p><p>Our final iteration therefore makes use of multiple image sizes using the srcset attribute, both in the &lt;source&gt; element and the fallback &lt;img&gt; element. We can hint to the browser how wide (in pixels) the image is using something like 800w . The final piece to the puzzle is to tell the browser how wide the image is going to be shown, in our case sizes=&quot;100vw&quot; .</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fembed%2Fpreview%2FExdxwvg%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DExdxwvg&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fmatt_tbt%2Fpen%2FExdxwvg&amp;image=https%3A%2F%2Fshots.codepen.io%2Fmatt_tbt%2Fpen%2FExdxwvg-512.jpg%3Fversion%3D1680787682&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1901300653ff36d463f68ad196cf4b69/href">https://medium.com/media/1901300653ff36d463f68ad196cf4b69/href</a></iframe><p>That’s all for now. Hopefully you can see that we can easily make a hero section definitively “better”, regardless of what it looks like.</p><p>Did I miss anything? Let me know in the comments if you’d have done it differently.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c04502e16f40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Above the fold” on the web — does it matter?]]></title>
            <link>https://medium.com/@matt.dawkins/above-the-fold-on-the-web-does-it-matter-4c08442e8342?source=rss-6ab0b71ca3ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c08442e8342</guid>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Matt Dawkins]]></dc:creator>
            <pubDate>Mon, 27 Mar 2023 15:30:30 GMT</pubDate>
            <atom:updated>2023-03-27T15:40:09.232Z</atom:updated>
            <content:encoded><![CDATA[<h3>“Above the fold” on the web — does it matter?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I2uqbFbYhrt_zr9HiIy7hQ.jpeg" /></figure><p><strong>The hard truth is this — you’re more likely to read this intro than what’s at the bottom of the page. But what about that bit of content that’s just below the bottom of your screen right now?</strong></p><p>The term ​“above the fold” originates from printed newspapers that are often presented and sold folded in half. Because of that form factor, designers make careful strategic decisions about the position of content, because it’ll affect whether people pick up the newspaper at all.</p><p>What about the web? The equivalent of the ​“fold” is arguably the bottom of your screen, before you start scrolling. Where that fold point is will depend on the size of your screen. But does it matter? Should we treat websites like printed material?</p><h3>Scrolling is easier than reading</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*4vHO287gjTPujU6_s_I93w.jpeg" /></figure><p>There is a popular misconception that scrolling is hard. And anything hard should be avoided. And, to be fair, at one point in the history of technology that was very much the case. Computer mice had two buttons (three if you were lucky), and scrolling was done by clicking the up or down buttons on the scroll bar. It was laborious and cumbersome, because those buttons were small targets that were easily missed. Early mobile devices weren’t much better, relying on arrow buttons to scroll a bit at a time.</p><p>But it wasn’t long before pretty much every mouse had a scroll wheel, every touchpad supported multi-touch panning, and every mobile device sported the ​“flick” gesture. Today, scrolling is a doddle. Unless you’re still using one of the aforementioned devices, in which case verily thou art living in the past, forsooth.</p><p>Today, scrolling isn’t the barrier. It’s reading.</p><h3>Context is king</h3><p>So does it matter what content is ​“above the fold” on your website? Well, it depends. Let’s take a look at a few different examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5AobU_Q2KHYfvQJu1aU7fw.jpeg" /></figure><p>For better or worse, we’re probably all familiar with how Facebook looks and feels. And you’ll notice that there is no concept of ​“above the fold” here. Content is ordered by relevance and time (and a whole load of other factors Facebook probably won’t tell us about), but there’s an expectation that you will scroll. The bottom of your screen is not a problem.</p><p>The key here is that the page is made up of lots of little bite-sized pieces of content, rather than one long piece. The psychology at play here is ​“I’ll just read <em>one more</em>”. It delivers quickly, and urges you to keep going just a little further. And that’s the rest of your evening gone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K8IohZcpME340ehMme22Kw.jpeg" /></figure><p>The world of ecommerce and promotion is entirely different. What’s shown at the top of the page is carefully crafted to achieve the optimum conversion rate. It’s not that people don’t scroll down the page (because statistics prove they do), but rather that what they see first will form their initial opinion, and that’s what matters.</p><p>All the important information is right there, at a glance, so that <em>it’s in your brain almost before you’ve noticed it</em>. There’s also an unmissable ​“call to action” — the ​‘buy now’ button. It’s clearly identifiable, so that there’s no confusion about what you should be doing next or how to do it. Below the fold is supportive, but if what’s above the fold doesn’t do a good job it’s unlikely the rest will change your mind.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKC6KcE7W43y9SNTmaltWQ.jpeg" /></figure><p>A good story has a beginning, a middle, and an end. If your content is identifiable as a news article or blog post then visitors will interact with it accordingly. If the title doesn’t grab them, they won’t even start reading. If the introduction bores them, they’ll jump ship.</p><p>The important factor here is not so much what’s above the fold but <em>how the story is told</em>. If your content is uninteresting, it doesn’t matter how much of it is above the fold. If it’s really engaging, it doesn’t matter how long you keep scrolling.</p><h3>So should you care about the fold or not?</h3><p>Yes. No. It depends.</p><p>First, ask yourself what you’re trying to achieve. <em>What’s your end goal</em>? That will inform what content you need to use to get there, which in turn will inform what sort of layout or design is needed to convey that message most effectively.</p><p>At TBT Marketing, where I work, we care about that end goal. Everything, from design to development to hosting to analytics, should all support that end goal. And we’re happy to help you figure out what needs to be above the fold, and why. <a href="https://www.tbtmarketing.com/contact-us"><strong>Get in touch</strong></a> if you want to find out more!</p><p>And if you made it all the way to the end, I salute you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c08442e8342" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>