My Custom CSS Reset
Whenever I start a new project, the first order of business is to sand down some of the rough edges in the CSS language. I do this with a functional set of custom baseline styles.
For a long time, I used Eric Meyer’s famous CSS Reset. It’s a solid chunk of CSS, but it’s a bit long in the tooth at this point; it hasn’t been updated in more than a decade, and a lot has changed since then!
Recently, I’ve been using my own custom CSS reset. It includes all of the little tricks I’ve discovered to improve both the user experience and the CSS authoring experience.
Like other CSS resets, it’s unopinionated when it comes to design / cosmetics. You can use this reset for any project, no matter the aesthetic you’re going for.
In this tutorial, we’ll go on a tour of my custom CSS reset. We’ll dig into each rule, and you’ll learn what it does and why you might want to use it!
Without further ado, here it is:
It’s relatively short, but there’s a lot of stuff packed into this small stylesheet. Let’s get into it!
Historically, the main goal of a CSS reset has been to ensure consistency between browsers, and to undo all default styles, creating a blank slate. My CSS reset doesn’t really do either of these things. These days, browsers don’t have massive discrepancies when it comes to layout or spacing. By and large, browsers implement the CSS specification faithfully, and things behave as you’d expect. So it isn’t as necessary anymore. I also don’t believe it’s necessary to strip away all of the browser defaults. I probably do want tags to set font-style: italic , for example! I can always make different design decisions in the individual project styles, but I see no point in stripping away common-sense defaults. My CSS reset may not fit the classical definition of a “CSS reset”, but I’m taking that creative liberty.
Pop quiz! Measuring by the visible pink border, how wide is the .box element in the following scenario, assuming no other CSS has been applied? Our .box element has width: 100% . Because its parent is 200px wide, that 100% will resolve to 200px. But where does it apply that 200px width? By default, it will apply that size to the content box. If you’re not familiar, the “content box” is the rectangle in the box model that actually holds the content, inside the border and the padding: The width: 100% declaration will set the .box ‘s content-box to 200px. The padding will add an extra 40px (20px on each side). The border adds a final 4px (2px on each side). When we do the math, the visible pink rectangle will be 244px wide. When we try and cram a 244px box into a 200px-wide parent, it overflows:
Code Playground
style>.parentwidth: 200px;border: 2px solid black;>.boxwidth: 100%;border: 2px solid hotpink;padding: 20px;>style>div class="parent">div class="box">div>div>
With this rule applied, percentages will resolve based on the border-box. In the example above, our pink box would be 200px, and the inner content-box would shrink down to 156px (200px — 40px — 4px). This is a must-have rule, in my opinion. It makes CSS significantly nicer to work with. We apply it to all elements and pseudo-elements using the wildcard selector ( * ). Contrary to popular belief, this is not bad for performance.
This can be a helpful strategy if you’re trying to migrate a large pre-existing project to use border-box. It isn’t necessary if you’re starting a brand new project from scratch. To keep things simple, I’ve omitted it from my CSS reset. Expand to see an example of when this might be helpful. Show more
Browsers make common-sense assumptions around margin. For example, an h1 will include more margin by default than a paragraph. These assumptions are reasonable within the context of a word-processing document, but they might not be accurate for a modern web application. Margin is a tricky devil, and more often than not, I find myself wishing elements didn’t have any by default. So I’ve decided to remove it all. 🔥 If/when I do want to add some margin to specific tags, I can do so in my custom project styles. The wildcard selector ( * ) has extremely low specificity, so it’ll be easy to override this rule.
line-height controls the vertical spacing between each line of text in a paragraph. The default value varies between browsers, but it tends to be around 1.2. This unitless number is a ratio based on the font size. It functions just like the em unit. With a line-height of 1.2, each line will be 20% larger than the element’s font size. Here’s the problem: for those who are dyslexic, these lines are packed too closely together, making it harder to read. The WCAG criteria states that line-height should be at least 1.5. Now, this number does tend to produce quite-large lines on headings and other elements with large type:
Code Playground
style>*line-height: 1.5;>style>p>This paragraph has a 1.5x ratiofor line-height, and it feelspretty good, right? I thinkthis text is legible and pleasant.p>h1>But it's a bit much on headings!h1>
You may wish to override this value on headings. My understanding is that the WCAG criteria is meant for «body» text, not headings.
This is a pretty advanced little snippet, and it’s beyond the scope of this blog post, but here’s a quick explanation. Show more
Alright, so this one is a bit controversial. On MacOS computers, the browser will use “subpixel antialiasing” by default. This is a technique that aims to make text easier to read, by leveraging the R/G/B lights within each pixel. In the past, this was seen as an accessibility win, because it improved text contrast. You may have read a popular blog post, Stop “Fixing” Font Smoothing, that advocates against switching to “antialiased”. Here’s the problem: that article was written in 2012, before the era of high-DPI “retina” displays. Today’s pixels are much smaller, invisible to the naked eye. The physical arrangement of pixel LEDs has changed as well. If you look at a modern monitor under a microscope, you won’t see an orderly grid of R/G/B lines anymore. In MacOS Mojave, released in 2018, Apple disabled subpixel antialiasing across the operating system. I’m guessing they realized that it was doing more harm than good on modern hardware. Confusingly, MacOS browsers like Chrome and Safari still use subpixel antialiasing by default. We need to explicitly turn it off, by setting -webkit-font-smoothing to antialiased . Here’s the difference:
Antialiasing
Subpixel Antialiasing
MacOS is the only operating system to use subpixel-antialiasing, and so this rule has no effect on Windows, Linux, or mobile devices. If you’re on a MacOS computer, you can experiment with a live render:
Code Playground
p>This paragraph uses the default subpixel antialiasing.p>p class="antialiased">This paragraph does not use subpixel antialiasing.p>
So here’s a weird thing: images are considered «inline» elements. This implies that they should be used in the middle of paragraphs, like or . This doesn’t jive with how I use images most of the time. Typically, I treat images the same way I treat paragraphs or headers or sidebars; they’re layout elements. If we try to use an inline element in our layout, though, weird things happen. If you’ve ever had a mysterious 4px gap that wasn’t margin or padding or border, it was probably the “inline magic space” that browsers add with line-height . By setting display: block on all images by default, we sidestep a whole category of funky issues. I also set max-width: 100% . This is done to keep large images from overflowing, if they’re placed in a container that isn’t wide enough to contain them. Most block-level elements will automatically grow/shrink to fit their parent, but media elements like are special: they’re known as replaced elements, and they don’t follow the same rules. If an image has a «native» size of 800×600, the
element will also be 800px wide, even if we plop it into a 500px-wide parent. This rule will prevent that image from growing beyond its container, which feels like much more sensible default behavior to me.
Here’s another weird thing: by default, buttons and inputs don’t inherit typographical styles from their parents. Instead, they have their own weird styles. For example, will use the system-default monospace font . Text inputs will use the system-default sans-serif font . And both will choose a microscopically-small font size (13.333px in Chrome). As you might imagine, it’s very hard to read 13px text on a mobile device. When we focus an input with a small font size, the browser will automatically zoom in, so that the text is easier to read. Unfortunately, this is not a good experience:
If we want to avoid this auto-zoom behavior, the inputs need to have a font-size of at least 1rem / 16px. Here’s one way to address the issue:
This fixes the auto-zoom issue, but it’s a band-aid. Let’s address the root cause instead: form inputs shouldn’t have their own typographical styles!
font is a rarely-used shorthand that sets a bunch of font-related properties, like font-size , font-weight , and font-family . By setting it to inherit , we instruct these elements to match the typography in their surrounding environment. As long as we don’t choose an obnoxiously small font size for our body text, this solves all of our issues at once. 🎉
In CSS, text will automatically line-wrap if there isn’t enough space to fit all of the characters on a single line. By default, the algorithm will look for “soft wrap” opportunities; these are the characters that the algorithm can split on. In English, the only soft wrap opportunities are whitespace and hyphens, but this varies from language to language. If a line doesn’t have any soft wrap opportunities, and it doesn’t fit, it will cause the text to overflow:
The New CSS Reset
This CSS reset is built from the understanding we don’t want to use the default style we are getting from the browsers, except the ‘display’ property.
This CSS reset removes all the default styles which we are getting on specific HTML elements except the ‘display’ property, as I already mention, and except special HTML elements like iframe, canvas, img, svg, video.
In case you want the default style of the browser of a specific HTML element back, you can revert back to the default styles of the browser. For example:
input[type="checkbox"], input[type="radio"] all: revert; >
input, textarea, select all: revert; >
Start use the-new-css-reset
How it Looks and Works
@charset "utf-8"; /*** The new CSS reset - version 1.8.5 (last updated 14.6.2023) GitHub page: https://github.com/elad2412/the-new-css-reset ***/ /* Remove all the styles of the "User-Agent-Stylesheet", except for the 'display' property - The "symbol *" part is to solve Firefox SVG sprite bug - The "html" attribute is exclud, because otherwise a bug in Chrome breaks the CSS hyphens property (https://github.com/elad2412/the-new-css-reset/issues/36) */ *:where(:not(html, iframe, canvas, img, svg, video, audio):not(svg *, symbol *)) all: unset; display: revert; > /* Preferred box-sizing value */ *, *::before, *::after box-sizing: border-box; > /* Reapply the pointer cursor for anchor tags */ a, button cursor: revert; > /* Remove list styles (bullets/numbers) */ ol, ul, menu list-style: none; > /* For images to not be able to exceed their container */ img max-inline-size: 100%; max-block-size: 100%; > /* removes spacing between cells in tables */ table border-collapse: collapse; > /* Safari - solving issue when using user-select:none on the text input doesn't working */ input, textarea -webkit-user-select: auto; > /* revert the 'white-space' property for textarea elements on Safari */ textarea white-space: revert; > /* minimum style to allow to style meter element */ meter -webkit-appearance: revert; appearance: revert; > /* preformatted text - use only for this feature */ :where(pre) all: revert; > /* reset default text opacity of input placeholder */ ::placeholder color: unset; > /* remove default dot (•) sign */ ::marker content: initial; > /* fix the feature of 'hidden' attribute. display:revert; revert to element instead of attribute */ :where([hidden]) display: none; > /* revert for bug in Chromium browsers - fix for the content editable attribute will work properly. - webkit-user-select: auto; added for Safari in case of using user-select:none on wrapper element */ :where([contenteditable]:not([contenteditable="false"])) -moz-user-modify: read-write; -webkit-user-modify: read-write; overflow-wrap: break-word; -webkit-line-break: after-white-space; -webkit-user-select: auto; > /* apply back the draggable feature - exist only in Chromium and Safari */ :where([draggable="true"]) -webkit-user-drag: element; > /* Revert Modal native behavior */ :where(dialog:modal) all: revert; >
Browser Support
88+ ✔ | 88+ ✔ | 84+ ✔ | 14+ ✔ | 75+ ✔ | 15+ ✔ |
Extensive Reading and Watching
the-new-css-reset is maintained by elad2412. This page was generated by GitHub Pages.