aboutsummaryrefslogtreecommitdiff
path: root/_posts/2021-11-09-default-html-style-updates.md
blob: bfbc1648bb31cd4b08afaef278e8bca7de74674f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
layout: post
title: "Modern Improvements for Default Browser Styles"
date: 2021-11-09
---


This website *almost* exclusively uses the browser's (whichever one that might be) default styling to render it's HTML. I firmly believe, and have [stated in a previous post](/css-js-mistake/), that the default HTML styling across all browsers is a thing of beauty. "Consistent and boring" is how I tend to refer to default browser styles - and I mean that in a *good way*.

But that doesn't mean some minor, modern improvements couldn't be made...

## Boosting Margins and Increasing Font Size

A little extra breathing room for a website's content never hurts. Browser defaults set the inner content too close to the main window borders, creating mild eye strain to focus on the far edges of the screen when reading. Pair this with a typeface set too small and you've got a recipe for disaster (in terms of user experience and accessibility). Luckily for us, adding two basic CSS properties fixes all of our readability woes. All that is required is a simple boost to the existing `margin` property set on the `body` element (I personally lean towards a very specific `1.5em`) and overriding the default `font-size` to `18px`[^1]:


    body {
        font-size:18px;
        margin:1.5em;
    }


There is one *small* caveat with setting the `font-size` across the whole `body` element: code elements set in `monospace`. They will stand out larger than the other fonts found in the document (due to variations in different typeface heights, spacing etc.) so we will need to target these elements specifically:


    code {
        font-size:14px;
        /* Word wrap is optional if you plan to have long inline code snippets */
        word-wrap:break-word;
    }


## Code & Pre Tags

Since we've mentioned `code` elements, let's fix those as well. The existing styling for inline code snippets and larger pre-formatted text sections leave a lot to be desired. They don't provide any means to wrap their inner content or make use of `overflow` properties to avoid vertically scrolling on smaller device screens. Sharing code examples becomes quite a pain when your webpage's flow and layout is broken just by including them. Browsers could fix this easily enough by defaulting to:


    pre {
        overflow:auto;
    }


## Basic Dark Mode Support

Barebones styling in current web browsers have no sane defaults[^2] for system-level dark mode. What a huge letdown. This is where the most "drastic" changes will be implemented with our browser default updates. We will need the browser to change the main `background-color`, along with resetting both the text and anchor link `color` for improved accessibility. Browser defaults for anchor link color in "light mode" are blue/purple - so I've opted towards using gold, orange and orangered in dark mode respectively:


    /* Dark mode */
    @media (prefers-color-scheme: dark) {
        @media not print {
            html {background:#0e0e0e;color:#e1e1e1; }
            a {color: gold;}
            a:visited {color: orange;}
            a:hover,a:focus{color: orangered;}
        }
    }


That is probably the most streamlined dark mode on the web...

## The "Reading Length" Debate

Proper reading length tends to be quite the point of contention on the web. Hell, even I've [written about it quite a bit](/character-unit/) in the past (and many of my side projects follow that standard). The main problem I have with this is lack of *user control*. I don't think the browser (or designers for that matter) should determine the best reading length for my own personal reading preferences. UX testing and group feedback has (somewhat) agreed upon 66-75 characters per line to be the most optimal reading experience. That is good to know. I *still* believe it should come down to user preference.

Do you want to know an incredible feature built into browsers? *Window resizing*. Abandon the idea that you "know better" than your users and give them the power to adjust as they see fit. The web was meant to be personal and flexible.

## Conclusion

There isn't much else to say, really. I think these tiny tweaks would greatly improve the default browser experience and maybe even convince others to just *use* these defaults instead of falling down the CSS rabbit hole (as fun as that might be sometimes). For easier convenience, I'll leave the full set of CSS changes below:


    body {
        font-size:18px;
        margin:1.5em;
    }
    code {
        font-size:14px;
    }
    pre {
        overflow:auto;
    }
    @media (prefers-color-scheme: dark) {
        @media not print {
            html {background:#0e0e0e;color:#e1e1e1; }
            a {color: gold;}
            a:visited {color: orange;}
            a:hover,a:focus{color: orangered;}
        }
    }


[^1]: `18px` seems to be the perfect sweet spot between "almost too large, yet not small enough to strain my eyes"

[^2]: At the time of this article's publish date