DEV Community

Cover image for Using CSS scroll-margin-top and text-wrap: balance for Calmer Reading Pages
Even
Even

Posted on

Using CSS scroll-margin-top and text-wrap: balance for Calmer Reading Pages

Two tiny CSS properties I now add to almost every article and documentation page take less than five lines of code, yet quietly fix two of the most common reading annoyances.

1. Stop sticky headers from covering anchor headings

Whenever a page has a fixed or sticky top navigation bar, jumping to a #section heading usually hides the heading right behind the header. Instead of writing JavaScript scroll offsets, one rule handles every anchor jump natively:

h2[id],
h3[id] {
  scroll-margin-top: 5.5rem;
}
Enter fullscreen mode Exit fullscreen mode

2. Prevent lonely single-word headline orphans

Longer article titles or section subheadings often wrap with one awkward trailing word on the last line. text-wrap: balance asks the browser to distribute words evenly across lines:

h1,
h2 {
  text-wrap: balance;
}

p {
  text-wrap: pretty;
}
Enter fullscreen mode Exit fullscreen mode

Both properties degrade gracefully in older browsers and add zero runtime JavaScript—easy Saturday-evening polish that immediately makes reading layouts feel calmer.

What small native CSS one-liners do you drop into every new stylesheet?

Top comments (0)