Skip to content

Text Size Button Makes the Page Jump? Here's the Culprit and the Fix

By 7 min read

The three scroll anchoring traps I see most, and a small script that locks the reading position.

id
Cover for the article on pages that jump when the text size changes

A page jumps after the text size button is clicked because every line above the reader grows taller, while the scroll position stays at the same pixel value. The paragraph that was right in front of the reader ends up somewhere else.

Modern browsers actually ship a feature called scroll anchoring to correct this. But it fails easily on news sites, and Safari only added support in version 27, released in September 2026. In my experience, the most reliable fix is still to lock the reading position yourself with a bit of JavaScript.

I run into this complaint a lot when I check news sites. A reader, usually one whose eyes already need bigger text, taps A+ in the middle of an article. The story runs away.

Now they have to scroll up and down hunting for the paragraph they were on. A feature built to make reading comfortable ends up getting readers lost, like an elevator that opens its doors on the wrong floor.

The good news is that the cause can be measured and fixed, honestly without much code.

Why Does the Page Jump When the Text Size Changes?

Because scroll position is measured in pixels from the top of the page, not from the sentence being read.

Say a reader has scrolled down 2,500 pixels and is reading paragraph 29. When the text grows from 100% to 112.5%, every paragraph above it gets longer.

The page is still parked at 2,500 pixels. Except at that spot, the screen now shows paragraph 26.

When text grows from 100 to 112.5 percent, the scroll position stays at 2,500 pixels, but the paragraph in front of the reader moves back three

In my test with Chromium on a 390 pixel wide screen, a reader on paragraph 22 got thrown back to paragraph 17 after a single click. Five paragraphs gone, when all they asked for was slightly bigger letters.

Browsers Already Have Scroll Anchoring

Scroll anchoring is a browser feature that picks one element on screen as an anchor, then corrects the scroll position whenever that anchor moves because content above it changed.

It is on by default and controlled by the overflow-anchor property documented on MDN. The full rules live in the CSS Scroll Anchoring specification.

As of May 2022, Chrome has supported it since version 56 and Firefox since version 66, but Safari not at all. So every iPhone reader runs without an anchor, Chrome users included, because browsers on iPhone still run on WebKit.

Update October 2026: Safari 27.0 finally supports scroll anchoring. The three traps below still apply in every browser, though, and iPhone readers who haven't updated still run without an anchor, so locking the position yourself is still worth it.

The catch is that even where it's supported, it often gives up or picks the wrong anchor. There are three traps I find most often.

Try the demo below. Hit A+ a few times, then switch the options on one by one and watch the paragraph marked at the reading line.

Demo

Trap One: Paragraph Margins in em

The scroll anchoring spec has a list of conditions that cancel the correction. One of them is a change to margin, padding, width, height, or top on the anchor or the elements above it.

A 1em margin grows when the font size grows. The browser treats that as an intentional layout change, so it skips the correction.

In my test, paragraphs with em margins left the scroll position completely uncorrected. After switching to rem, the browser corrected it again.

The article body CSS:

CSS
.article-body p {
  margin: 0 0 1.25rem;
}

rem is calculated from the font size on the html element, not from the paragraph. So the gap between paragraphs stays the same even when the article text grows.

Trap Two: A Side Column That Comes First in the HTML

News sites almost always have a side column, for things like popular stories, ads, or a table of contents. If that column is written before the article body in the HTML, the browser can pick it as the anchor.

Elements using position: sticky are excluded from anchor selection. But the column's wrapper usually isn't sticky.

That wrapper runs the full length of the article, is visible on screen, and doesn't move when the article text grows. The browser decides its anchor is perfectly fine, so the page never gets corrected.

overflow-anchor: none makes the browser pick its anchor from the article body

Here's what happened:

When I tested with a side column on the left, the scroll position stayed put exactly like a browser with no scroll anchoring at all. The reader still got thrown several paragraphs back. Once the column got overflow-anchor: none, the browser switched to picking a paragraph in the article body as the anchor.

The side column CSS:

CSS
.side-column {
  overflow-anchor: none;
}

This property stops the side column and everything inside it from being chosen as an anchor.

Trap Three: The Smart Header Panics Too

Even when scroll anchoring works, the correction is still a scroll. A header that hides when the reader scrolls down and shows when they scroll up will read that correction as the reader moving.

So the header and toolbar hide right after the button is clicked. From the reader's side, it looks like the page jumped again.

I covered this kind of header in my smart sticky toolbar tutorial for Next.js. Same logic here: it only looks at the difference in scroll position, so it can't tell a reader's thumb from a correction made by the browser.

Why Scroll Anchoring Alone Isn't Enough

There are two reasons I still add my own position lock.

First, Safari and every other browser on iPhone only got scroll anchoring in Safari 27, and not every reader updates iOS right away. On news sites, the share of iPhone readers is rarely small.

Second, the anchor the browser picks is usually the topmost visible element, not the sentence being read. The paragraphs between the top of the screen and the reader's eyes still get longer, so the reading position still drifts.

In my test with rem margins and no side column, native scroll anchoring did work. But a reader on paragraph 22 still ended up on paragraph 21.

How to Lock the Reading Position Yourself

The idea is simple: note which paragraph is being read before the text grows, then put that paragraph back at the same spot on screen.

First, the article text size is driven by one CSS variable. The article body CSS:

CSS
.article-body {
  font-size: calc(1rem * var(--text-scale, 1));
}

Second, the function for the text size button. The file text-size.js:

JavaScript
const sizes = [1, 1.125, 1.25]
let index = 0

const findAnchor = (body) => {
  const line = window.innerHeight * 0.35
  const el = [...body.children].find((child) => child.getBoundingClientRect().bottom > line)
  if (!el) return null
  const box = el.getBoundingClientRect()
  const ratio = box.height ? Math.min(Math.max((line - box.top) / box.height, 0), 1) : 0
  return { el, ratio, point: box.top + ratio * box.height }
}

export function changeTextSize() {
  const root = document.documentElement
  const body = document.querySelector('.article-body')
  const anchor = findAnchor(body)
  index = (index + 1) % sizes.length
  root.style.overflowAnchor = 'none'
  root.style.scrollBehavior = 'auto'
  root.dataset.scrollLock = ''
  root.style.setProperty('--text-scale', String(sizes[index]))
  if (anchor) {
    const box = anchor.el.getBoundingClientRect()
    const shift = box.top + anchor.ratio * box.height - anchor.point
    if (Math.abs(shift) > 0.5) window.scrollBy(0, shift)
  }
  requestAnimationFrame(() =>
    requestAnimationFrame(() => {
      root.style.removeProperty('overflow-anchor')
      root.style.removeProperty('scroll-behavior')
      delete root.dataset.scrollLock
    }),
  )
}

findAnchor draws a line at 35% of the screen height, roughly where people tend to read. It finds the first element in the article body that crosses that line, then records how far into that element the line sits, for example 40% from the top of the paragraph.

changeTextSize turns native scroll anchoring off for a moment with overflowAnchor = 'none', so the browser doesn't apply its own correction on top and double it.

After --text-scale changes, the function measures the same paragraph again. The difference is used to shift the page with window.scrollBy().

The scrollBehavior = 'auto' line matters if your site sets scroll-behavior: smooth on the html element. Without it, the correction becomes a scroll animation that looks like the page is sliding.

The two requestAnimationFrame calls at the end wait two frames before scroll anchoring and your CSS scroll-behavior take over again, then the scrollLock flag is removed.

Third, the smart header needs to know the scroll that just happened didn't come from the reader. The file smart-header.js:

JavaScript
const root = document.documentElement
let last = window.scrollY

window.addEventListener(
  'scroll',
  () => {
    const y = window.scrollY
    if ('scrollLock' in root.dataset) {
      last = y
      return
    }
    if (y - last > 6) root.dataset.nav = 'hidden'
    if (y - last < -6) root.dataset.nav = 'shown'
    last = y
  },
  { passive: true },
)

While data-scroll-lock is present on the html element, the header only updates its last position without hiding or showing.

Finally, wire the function to the button:

JavaScript
import { changeTextSize } from './text-size.js'

document.querySelector('.text-size-button').addEventListener('click', changeTextSize)

I tested this code in Chromium at 1,280 and 390 pixels wide, with a side column on the left and scroll-behavior: smooth on the html element. After several size changes in a row, the paragraph being read stayed the same and stayed on the reading line. The header didn't move either.

How to Check Your Own Site

Open a long article on your phone or in DevTools device mode, scroll to the middle, then run this in the Console tab:

JavaScript
const line = innerHeight * 0.35
const reading = () => [...document.querySelectorAll('article p')].findIndex((p) => p.getBoundingClientRect().bottom > line) + 1
reading()

The result is the number of the paragraph sitting at the reading line. Click the text size button on your site, then run reading() once more.

If the number changed, your page jumps. It's usually the kind of bug that only shows up when the editor in chief reads on a phone while waiting for a meeting.

Start with the smallest fix: put overflow-anchor: none on the side column and switch paragraph margins to rem. If the number in the Console still changes, then add the position lock above.

Thanks for reading. Hopefully the A+ button on your site stops being a teleport button.

Hafidz Nc

Fractional Head of Digital

Discussion