CSS Reset For Composable Layouts
The styles that we write are not the first styles that get applied to our app. Before a single line of our CSS is used, the browsers will apply their user-agent style sheets. Unfortunately, there is no rule that requires any of the browsers to use the same style sheet for their user-agent. For this reason, CSS Resets have been a valuable tool to help developers provide consistent styling on the web. A CSS Reset is nothing more than a style sheet you bring in before the rest of your app's styles, either as a separate style sheet or tagged onto the beginning of your style sheet. This style sheet's goal is simply to provide a base from which you can consistently apply CSS across browsers. Some resets are aggressive and remove all styles from all elements. Others try to "normalize" all the user-agent style sheets of the various browsers. Luckily, there is currently less inconsistency across the browsers than would justify aggressive resets. Still, from a layout perspective, there is a need to override the browser default styles to make compositional layout possible. It makes sense to look at what you need to reset in the browser's user-agent style sheets to achieve this goal. The first thing to set is the box-sizing property on all elements and pseudo-elements to border-box : Setting the box-sizing property to border-box on all elements and pseudo-elements allows for a more intuitive developer experience since the size of the element will be calculated from border to border instead of the default, which is content + padding + border. After that, we remove any margins from all the elements. Doing this allows elements the ability to better control the space between their child elements. It is difficult to layout items correctly if they already have a built-in margin that you have to override. From there, we remove the padding and list style from ul and ol elements. A common thing that most people do when working with lists is remove the default padding and list styles added by browsers. Since this is pretty much universally done every time one styles a list, I like to do it once and get it done with. You will notice that we are using the attribute selector to remove the padding and list style only if the class attribute is set. Doing this will allow our lists to be reset if we are actively styling the element using the class attribute. By doing it this way, our list elements retain their default styles when we use the pure ul and ol tags without styling applied. Next, we set the min-block-size of the body to be 100vh It is helpful to have the body take up the entire viewport even if its content does not. Next, we set our images to be block-level elements instead of inline-level elements and then set their max-inline-size to 100% . Setting the max-inline-size to 100% makes our images responsive by default and treats images as block-level elements, which is how most people use img tags. Finally, we set the max-inline-size of text-based tags to be 60ch . The ch unit is approximately the width of the 0 character of the font family in use. In any given font family, each character can either be very wide like the letter W or very skinny like the letter l. The 0 is neither the widest nor skinniest character in a font family and is a good proxy for a character width. We use 60ch because a large amount of research has gone into finding the optimal line lengths for readability. You can check out the work done over at material.io to read more about optimal line lengths, but for brevity, 60 characters is a good default cap on the inline size of the text. With these resets in place, we have now primed the browser to let us start building composable layouts.