# Modals cut off at the top and bottom when zoomed in

Source: https://easeweb.dev/learn/modals-cut-off
Topics: Zoom, reflow and text spacing > Content fits a 320px screen without sideways scrolling (WCAG 1.4.10)
The fix: Let the dialog scroll when it is taller than the window. Either cap its height at the window's less a margin and give it overflow-y: auto, or let the backdrop scroll with the dialog inside it, or open a native dialog with showModal(), whose browser styles already cap it.
Test with: zoom, keyboard
References: https://www.w3.org/WAI/WCAG22/Understanding/reflow.html https://www.w3.org/WAI/WCAG22/Techniques/failures/F102 https://html.spec.whatwg.org/multipage/rendering.html#flow-content-3 https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/overflow-y https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/length

This is a recreated teaching example, not a finding about a named client. The code is a starting pattern: test it in your own product. No completed assistive-technology test is claimed here.

## What happens

A bike workshop's page offers a full service for $85, with a Book a service button. The button opens a dialog over the page, dimmed behind it: a × to close it in the top corner, the heading "Book a full service", the date and the price, two short paragraphs about dropping the bike off and changing the booking, and Cancel and Confirm booking at the bottom. On a laptop it sits in the middle of the window with plenty of room around it, and on a phone it fills most of the screen and fits.

Someone with low vision zooms the page to 400% in a window 1280 by 1024 pixels, which leaves it 320 by 256 CSS pixels, and books a service. At that width, with the shop's 16-pixel text and a line height of 1.5, the dialog's text wraps onto more lines and the dialog grows to over 400 pixels tall, in a window 256 tall. It is centred on the window, so more than 80 pixels of it run past the top edge, and as many past the bottom edge: the × and the heading are above the window, and Cancel and Confirm booking below it. The mouse wheel scrolls the page behind, and the dialog doesn't move. Tab reaches Confirm booking, but the button stays out of sight. The only way to book is to zoom back out until the dialog fits.

## Why it fails

The dialog is placed with `position: fixed`, `top: 50%`, `left: 50%` and `transform: translate(-50%, -50%)`, a common way to centre a box. It puts the dialog's middle at the window's middle whatever its height, and nothing caps that height at the window's. A fixed box stays where it is when the page scrolls, and this one has no scroll of its own, so what lies past the window's edges can't be brought into view, by the wheel, by touch or by moving focus there. Even the page itself could never scroll to the part above the top edge.

Success criterion 1.4.10 asks that content can be presented without loss of information or functionality at a width of 320 CSS pixels. The Understanding page explains 320 CSS pixels as a window 1280 pixels wide at 400% zoom, and in a window 1024 pixels tall, the same zoom leaves 256 pixels of height. In that window the content is still on the page, drawn past the window's edges, but the person can't reach it: the dialog's heading and its buttons are lost, and so is the booking. The same Understanding page says that moving content is not a loss, so long as people can still get to it: a dialog that scrolls is enough. The closest failure W3C documents is F102, content no longer available after the page has reflowed to 320 pixels.

## Who is affected

Anyone whose window is short in CSS pixels, or whose text is large enough to make the dialog taller than the window: zoomed in on a desktop, with a large display size on a phone, with the browser's text size turned up, or on a phone held on its side, where the window is wide but short, and a dialog of ordinary length can run past both edges.

## What automation and AI miss

Scanners load the page once, at desktop size, and don't open its dialogs; when they do, the dialog fits the window. axe has no rule for it. EaseWeb's audit now looks for it: at 320 by 256 CSS pixels it opens up to eight buttons that are marked as opening something (with `aria-haspopup`, `aria-expanded`, `aria-controls` or `popovertarget`, for example) or that appear only at that width, and reports a fixed element whose text or controls lie past the top or bottom of the window with nothing around them that scrolls. This page's Book a service button carries none of those marks, so the audit doesn't open it, and a person has to. Design reviews look at the dialog in a laptop frame, where it is short. An AI assistant asked for a centred modal can write this exact transform, with no `max-height`; react-modal's README shows the same centring as its example of custom styles, and its issue tracker has the question "How to allow scrolling on modal if window is too small".

## Before: a dialog centred with a transform

```html
<button type="button" id="book">Book a service</button>
…
<div class="backdrop" hidden>
  <div class="booking" role="dialog" aria-modal="true" aria-labelledby="booking-title">
    <button type="button" class="close" aria-label="Close">×</button>
    <h2 id="booking-title">Book a full service</h2>
    <p>Thursday 12 November, 10:00</p>
    <p>$85, paid when you collect the bike</p>
    <p>Bring the bike in by 10:00. We’ll email you when it is ready, usually the same afternoon.</p>
    <p>Plans change? Move or cancel the booking for free up to 24 hours before.</p>
    <button type="button" class="cancel">Cancel</button>
    <button type="button" class="confirm">Confirm booking</button>
  </div>
</div>
```

```css
.backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.5);
}
.booking {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(90%, 28rem);
  box-sizing: border-box;
  padding: 1.5rem;
  border-radius: 1rem;
  background: #fff;
}
.close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}
```

```js
const book = document.querySelector('#book');
const backdrop = document.querySelector('.backdrop');
book.addEventListener('click', () => {
  backdrop.hidden = false;
  backdrop.querySelector('.close').focus();
});
// … Close, Cancel and Escape hide the backdrop and focus Book a service again.
```

At 400% zoom the dialog is over 400 pixels tall, in a window 256 pixels tall:

- The transform centres it on the window, so more than 80 pixels run past the top edge, the × and the heading with them, and as many past the bottom edge, with Cancel and Confirm booking.
- The dialog has no `max-height` and no `overflow`, so nothing inside it scrolls.
- It is fixed, so scrolling the page moves only the page behind it.

## After: a dialog capped at the window's height

```html
<button type="button" id="book">Book a service</button>
…
<div class="backdrop" hidden>
  <div class="booking" role="dialog" aria-modal="true" aria-labelledby="booking-title">
    <button type="button" class="close" aria-label="Close">×</button>
    <h2 id="booking-title">Book a full service</h2>
    <p>Thursday 12 November, 10:00</p>
    <p>$85, paid when you collect the bike</p>
    <p>Bring the bike in by 10:00. We’ll email you when it is ready, usually the same afternoon.</p>
    <p>Plans change? Move or cancel the booking for free up to 24 hours before.</p>
    <button type="button" class="cancel">Cancel</button>
    <button type="button" class="confirm">Confirm booking</button>
  </div>
</div>
```

```css
.backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.5);
}
.booking {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(90%, 28rem);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  box-sizing: border-box;
  padding: 1.5rem;
  border-radius: 1rem;
  background: #fff;
}
.close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}
```

```js
const book = document.querySelector('#book');
const backdrop = document.querySelector('.backdrop');
book.addEventListener('click', () => {
  backdrop.hidden = false;
  backdrop.querySelector('.close').focus();
});
// … Close, Cancel and Escape hide the backdrop and focus Book a service again.
```

1. Give the dialog `max-height: calc(100dvh - 2rem)`. It is never taller than the window less 1rem above and below, and the transform still centres it, so it sits whole inside the window.
2. Give it `overflow-y: auto`. When its content is taller than that, the dialog scrolls: the wheel, touch and the arrow keys move its content, and Tab scrolls each button into view as it takes focus.

This fixes the Before and passes 1.4.10. It is the smallest change, two lines, and the dialog looks the same wherever it fits. At 400% the × and the heading scroll away as the person reads down to the buttons, and back as they scroll up.

## After: a backdrop that scrolls

```html
<button type="button" id="book">Book a service</button>
…
<div class="backdrop" hidden>
  <div class="booking" role="dialog" aria-modal="true" aria-labelledby="booking-title">
    <button type="button" class="close" aria-label="Close">×</button>
    <h2 id="booking-title">Book a full service</h2>
    <p>Thursday 12 November, 10:00</p>
    <p>$85, paid when you collect the bike</p>
    <p>Bring the bike in by 10:00. We’ll email you when it is ready, usually the same afternoon.</p>
    <p>Plans change? Move or cancel the booking for free up to 24 hours before.</p>
    <button type="button" class="cancel">Cancel</button>
    <button type="button" class="confirm">Confirm booking</button>
  </div>
</div>
```

```css
.backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  padding: 1rem;
  overflow-y: auto;
  background: rgb(0 0 0 / 0.5);
}
.booking {
  position: relative;
  margin: auto;
  width: min(90%, 28rem);
  box-sizing: border-box;
  padding: 1.5rem;
  border-radius: 1rem;
  background: #fff;
}
.close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}
```

```js
const book = document.querySelector('#book');
const backdrop = document.querySelector('.backdrop');
book.addEventListener('click', () => {
  backdrop.hidden = false;
  backdrop.querySelector('.close').focus();
});
// … Close, Cancel and Escape hide the backdrop and focus Book a service again.
```

1. Give the backdrop `overflow-y: auto`. It already fills the window, so when the dialog inside it is taller than the window, the backdrop scrolls and carries the whole dialog past.
2. Make the backdrop a grid with 1rem of padding, and take `position: fixed`, `top`, `left` and the transform off the dialog. It now sits in the backdrop's flow, so its height counts towards what the backdrop scrolls. `position: relative` keeps the × in its corner.
3. Centre the dialog with `margin: auto`. When it fits, the margins centre it; when it is taller than the window, they fall to zero and its top starts at the top of the backdrop, where scrolling can reach it.

This fixes the Before and passes 1.4.10. It is how Bootstrap's modal and Headless UI's documented dialog scroll: the dialog keeps its full height and moves as one piece, with 1rem of backdrop above its top and below its end.

## After: a native dialog

```html
<button type="button" id="book">Book a service</button>
…
<dialog class="booking" id="booking" aria-labelledby="booking-title">
  <button type="button" class="close" aria-label="Close">×</button>
  <h2 id="booking-title">Book a full service</h2>
  <p>Thursday 12 November, 10:00</p>
  <p>$85, paid when you collect the bike</p>
  <p>Bring the bike in by 10:00. We’ll email you when it is ready, usually the same afternoon.</p>
  <p>Plans change? Move or cancel the booking for free up to 24 hours before.</p>
  <button type="button" class="cancel">Cancel</button>
  <button type="button" class="confirm">Confirm booking</button>
</dialog>
```

```css
.booking::backdrop {
  background: rgb(0 0 0 / 0.5);
}
.booking {
  width: min(90%, 28rem);
  box-sizing: border-box;
  padding: 1.5rem;
  border: 0;
  border-radius: 1rem;
  background: #fff;
}
.close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
}
```

```js
const book = document.querySelector('#book');
const booking = document.querySelector('#booking');
book.addEventListener('click', () => {
  booking.showModal();
  booking.querySelector('.close').focus();
});
// … Close and Cancel call booking.close(); Escape closes it, and focus returns to Book a service.
```

1. Replace the backdrop and the dialog's `div` with a `dialog` element, named by its heading, and style its `::backdrop` instead of a backdrop of your own.
2. Open it with `showModal()`. The browser's own styles for a modal dialog centre it and give it `max-height: calc(100% - 6px - 2em)` of the window and `overflow: auto`, so a tall dialog fits the window and scrolls.
3. Remove `position`, `top`, `left` and the transform from the dialog: the browser centres it. Don't give it a `min-height`, `max-height: none` or a larger `max-height`, which would take its limit away again. Keep `box-sizing: border-box`: the browser's limit allows for its own 1em of padding, and a wider padding would otherwise be added on top of it.

This fixes the Before and passes 1.4.10, with no line of CSS about its height. It is the only fix that also makes the page behind inert and closes on Escape with no script of your own, which [modals that leave keyboard focus behind them](/learn/modal-focus-management) relies on. The browser's limit is fixed in the HTML standard's rendering section, and Chromium, Firefox and WebKit all apply it.

## Which option to use

All three keep every part of the dialog within reach at 400% zoom, so all three pass 1.4.10. Capping the dialog is the smallest change, two lines on a dialog you already have, and the one to reach for in a component library you can only restyle. Letting the backdrop scroll keeps the dialog's whole shape and moves it as one piece, as a long page would; choose it when the dialog's top and bottom belong together, such as a form whose heading should stay with its first field. The native dialog needs a change to the markup and the script, but its height limit comes from the browser, along with focus held inside and Escape; choose it when you can change the component, and keep its limit when you style it.

## Implementation decisions

A header or footer that stays put inside the dialog, so the × and Confirm booking are always in view while only the middle scrolls, is what Bootstrap's `modal-dialog-scrollable` and MUI's `scroll="paper"` do. In a window 256 pixels tall it leaves little room: in a dialog with 1rem of room above and below it, a header and a footer 60 pixels each leave about 100 pixels to read in, and if either wraps onto two rows at 320 pixels wide, the middle can shrink to nothing. If you keep them, keep each to one row at 320 pixels, and in a short window (`@media (max-height: 30em)`) let the whole dialog scroll instead.

Centring a dialog in an overlay that scrolls needs care. With `display: flex` and `align-items: center` on the overlay, a dialog taller than the window is centred by overflowing at both ends, and the overlay can only scroll down, so its top stays cut off. Chakra UI's issue tracker has [a report of its centred modal cut off at the top on phones](https://github.com/chakra-ui/chakra-ui/issues/2871). `margin: auto`, a grid, or `align-items: safe center` start the dialog at the top when it doesn't fit.

The same failure reaches other things that float:

- a menu fixed below a header, or a drawer fixed to one side with `top: 0` and `bottom: 0`, which needs its own `overflow-y: auto` to show its last links;
- a sheet that slides up from the bottom of the window, which needs a `max-height` and a scroll as the dialog does;
- a popover opened with the `popover` attribute, which the browser centres with `overflow: auto` but, unlike a modal dialog, gives no `max-height`, so a tall one runs past both edges.

A bar fixed to the bottom of the page is a different case: it fits, but covers the page under it, as in [floating buy bars that cover the page when zoomed in](/learn/floating-buttons).

`100dvh` is the window's height as it is now. On a phone, `100vh` is the height with the browser's address bar put away, so a dialog capped at `100vh` can still run under the bar until it is scrolled away; in a desktop browser at 400% the two are the same. On a fixed element, `max-height: calc(100% - 2rem)` works as well, because its percentages are taken from the window, unless an element around it has a property that changes that, such as a transform or a filter.

Locking the page's own scroll while the dialog is open, with `overflow: hidden` on `body`, doesn't change any of this: the dialog has to scroll by itself either way. `overscroll-behavior: contain` on the box that scrolls stops the page behind from moving when the dialog reaches its end.

## Verify the fix

1. In a window 1280 by 1024 pixels, zoom the browser to 400% (or set the viewport to 320 by 256 CSS pixels in the developer tools) and open each dialog, sheet and menu the page has.
2. Scroll each one to its top and to its end, with the mouse wheel and by touch. The × or Close button, the heading and the last button each come fully into view.
3. Press Tab from the first control in the dialog to the last, and Shift + Tab back. Each control is scrolled into view as it takes focus.
4. Repeat on a phone held on its side, and with the browser's text size at 200%.
5. Repeat with the longest content each dialog can hold: an error message showing, a long name or address, and every translation.

Record the OS, browser and assistive-technology versions, the build, the date, and the actual result of each step. Repeat after the dialog's component or styles change.

## Limits

This is a starting pattern, not a guarantee for every application. The sizes are this example's; test your own dialogs with their longest content. A dialog that fits the window but is wider than 320 pixels, so the page scrolls sideways, is a different failure, covered in [pages that scroll sideways on a narrow screen](/learn/reflow). A sticky header that hides the control in focus is covered in [sticky headers and banners that hide keyboard focus](/learn/focus-not-obscured). Passing at 400% doesn't make a long dialog easy to use there: a dialog that needs several screens to read may be better as a page of its own. Class names in the snippets are placeholders for your own.

---

Get this guide in your coding agent: MCP https://easeweb.dev/mcp (setup at https://easeweb.dev/services/developers)
