💄
CSS
Frontend Web DevLanguages
  • Introduction
  • Architecture
    • Default Styles
    • Nesting
    • Layers
  • Page
  • layout
    • Grid
    • Flexbox
    • Container Queries
  • Group 1
    • Pseudo
  • More modern stuff
Powered by GitBook
On this page
  • Libraries
  • Common Adjustments
  • Media
  • Form Typography Styles
  • Line-Height
  • Word Wrapping
  • Links
  • Inputs
  • Button Cursor
  • Scroll Padding
  • Attribution
  1. Architecture

Default Styles

For removing and adjusting default styling to use modern practices while staying consistent across browsers

PreviousIntroductionNextNesting

Last updated 1 year ago

Libraries

Though modern CSS and browsers provide consistent and reasonable behavior in User Agents (browsers), a few community libraries can provide a good baseline. Include them in your project, or perhaps better yet, pick and choose which rules make sense for you.

'Reset' stylesheets tend to remove most default styles at once, then reapply those that make sense. 'Normalize' stylesheets adjust styles individually with greater concern for uniform behavior across browsers. Although both provide generally good opinions, it's easier to examine what a 'Normalize' stylesheet exactly adjusts in your project.

In actual use, baseline stylesheets provide a mix of both resets and normalizations. This is particularly true for web designers and CSS design systems, which provide their own opinionated baselines.

Common Adjustments

These are mostly practical resets to improve layout, positioning, and without having to later remove unnecessary default styles.

/* Make explicit box sizes also include the border */
*, *::before, *::after {
  box-sizing: border-box;
}
/* Set all element margins to zero */
* {
  margin: 0;
}
/* Remove list styles (bullets/numbers) */
ol, ul, menu {
    list-style: none;
}
/* Remove spacing between cells in tables */
table {
    border-collapse: collapse;
}

Media

Setting media elements from a default display value of inline to block allows for adjusting size and position. Secondly, to ensure any such block-level elements' sizing will be constrained to the dimensions of their parent, we can add max sizing. Although a max-width or max-height of 100% will suffice, max-block-size and max-inline-size will better perform better for international audiences with browser settings for flowing content in varied directions.

img, picture, video, canvas, svg {
  display: block;
  max-inline-size: 100%;
  max-block-size: 100%;
}

Form Typography Styles

By default, form elements use font styles defined by each browser. This creates a disconnect from the surrounding design. To avoid having to explicitly define these elements' styles to those intended, we can set the default to use those inherited from its nearest styled parent element.

input, button, textarea, select {
  font: inherit;
}

Line-Height

The default line-height, or spacing between lines of text, has an average default value of 1.2. When specified without units, it refers to a multiple of the corresponding element's font-size. So a paragraph with a 16px font-size would have an average line-height rendered at 19.2px.

In reality, line-height should variably adjust various font sizes as appropriate.

Large text such as headings, however, will need a smaller default line-height defined, so they don't appear too loose or exaggerated.

body {
  line-height: 1.5;
}

Word Wrapping

By default, text will automatically line-wrap for long sentences by automatically splitting on white space and hyphens. But if there's still not enough space, overlaps and overflows will happen, and scrollbars may appear. By setting the overflow-wrap property to break-word, we allow greater flexibility. While this may be most appropriate for paragraphs and headings, it could be applied as needed to other elements or components too.

p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
}

Links

For default anchor links that aren't scoped to a class, we can use a regular, underlined style. The code below sets the underline to have a relative thickness while increasing the distance offset from the text. This subtle style can improve perception, particularly in visually dense contexts.

Using max()for the thickness, a browser will choose the larger of the two options: a size relative to the font size or at least 1px thick.

/* Baseline for default links */
a:not([class]) {
  /* Relatively sized thickness and offset */
  text-decoration-thickness: max(0.08em, 1px);
  text-underline-offset: 0.15em;
}

Inputs

The :focus-visible pseudo-class applies when form input elements have been activated via mouse, tap, or keyboard. Many browsers show a "focus ring" by default.

Interactive elements like links and buttons will also be styled only if activated via keyboard.

In the example below, a single custom property is defined for the selector, while styles are applied using custom properties defined in and outside the selector.

:focus-visible {
  --outline-size: max(2px, 0.15em);

  outline: var(--outline-width, var(--outline-size)) var(--outline-style, solid)
    var(--outline-color, currentColor);
  outline-offset: var(--outline-offset, var(--outline-size));
}

Button Cursor

By default, an arrow cursor may appear when hovering over a button element with a mouse. To better convey click-ability, consider changing the cursor property to pointer for buttons and any other elements as needed.

button {
  cursor: pointer;
}

Scroll Padding

This allows adequate space above or below an element when scrolled to. However, extra padding may be needed if sticky headers are in the viewport.

Below, the :target selector matches when an element is an anchor link target like:

<a href="#specials">Daily Specials</a>
<!-- mixed content or UI -->
<div name="#specials">
    <!-- content -->
</div>

scroll-padding-block-start can add space between targets and the top of the viewport.

/* Scroll padding above anchor links */
:target {
  scroll-padding-block-start: 2rem;
}

scroll-padding-block-end can add space between a focused element and the bottom of the viewport, which can help track the visible focus position.

/* Scroll padding below focused elements */
:focus {
  scroll-padding-block-end: 8vh;
}

Attribution

One or more of these recommendations have been sourced from:

The WCAG (Web Content Accessibility Guidelines) however the line-height to be a minimum of 1.5. This performs better for small text and particularly helps people with visual comprehension impairments like dyslexia.

The New CSS Reset
Modern Normalize
Josh Comeau's Reset/Baseline
recommends
Modern CSS For Dynamic Component-Based Architecture
Josh Comeau's Reset/Baseline