# Overflow, Outline & Display

> Three properties that control how a box relates to its content and its neighbors: what spills out, what draws outside without shifting layout, and what box type the element becomes.

Subject: CSS | Lesson 9 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/overflow-and-display
Previous: [calc(), clamp(), min() & max()](https://codewithanimation.in/css/calc-clamp-and-functions.md) | Next: [Fonts & Web Font Loading](https://codewithanimation.in/css/fonts-and-web-fonts.md)

## Three properties, three different jobs

`overflow` decides what happens when content is bigger than its container. The default is `visible`, meaning the content spills out and is not clipped. `hidden` clips it, `scroll` always shows a scrollbar, and `auto` shows one only when needed.

`outline` draws a line OUTSIDE the border edge. Unlike `border`, an outline is not part of the box model: it takes up no space and never shifts neighboring elements. That is why it is the right property for focus rings, which must highlight an element without reflowing the page.

`display` sets the box type the element generates: `block`, `inline`, `inline-block`, `flex`, `grid`, `none`, or `contents`. It is the most fundamental layout property, because it determines whether an element starts on its own line, flows with text, or disappears entirely.

## Each one fixes a layout problem nothing else can

Without `overflow`, long content would always break your layout. A table too wide for its column, a code block longer than its container, or a chat transcript that should scroll independently all need `overflow: auto` or `hidden` to behave. The property is what makes bounded UI regions possible.

`outline` exists because focus indicators must be visible WITHOUT moving content. If a focus ring used `border`, focusing a button would push its neighbors by the border width. Outline draws over the surrounding area and leaves layout untouched, which is why the accessibility guidelines and the default `:focus-visible` style both use it.

`display: none` is the only way to fully remove an element from layout and the accessibility tree. `visibility: hidden` keeps the space reserved. Knowing the difference is the difference between a tab panel that collapses cleanly and one that leaves a gap.

## The values that matter day to day

**Overflow values:** `visible` (spill out), `hidden` (clip, no scrollbar, still scrollable via JS), `scroll` (always show scrollbar), `auto` (scrollbar only when needed), and `clip` (clip, NOT scrollable even via JS). Use longhands `overflow-x` and `overflow-y` when the axes differ, for example a horizontal-only scroller: `overflow-x: auto; overflow-y: hidden;`.

**Text truncation** needs three properties together: `overflow: hidden`, `white-space: nowrap`, and `text-overflow: ellipsis`. Miss any one and it breaks: without `nowrap` the text wraps instead of truncating, without `overflow: hidden` the overflow still shows.

**Outline vs border:** `border` is in the box model and takes up layout space; `outline` is outside the box model and takes up none. Use `outline-offset` to push the outline away from the border edge, which is how you get a focus ring with a visible gap. Outline cannot be styled per side the way border can.

**Display values:** `block` fills width and starts a new line. `inline` flows with text and ignores `width`, `height`, and vertical margin. `inline-block` flows inline but respects `width` and `height`. `none` removes the box completely. `contents` dissolves the element's own box so its children join the grandparent's layout directly.

**`none` vs `visibility: hidden`:** `display: none` removes the element from layout and the accessibility tree, so it takes up no space and screen readers skip it. `visibility: hidden` keeps the box's space reserved but invisible, and is inherited, so a child can override it with `visibility: visible`. Pick `none` when the element should truly disappear; pick `hidden` when you need to preserve layout space.

## Common mistake: display: none is not visibility: hidden

The classic bug: a developer hides a tab with `visibility: hidden` and wonders why the inactive tab still takes up space and pushes the active one over. `visibility: hidden` keeps the box in the layout, just transparent. To fully remove an element from the flow, use `display: none`. The reverse mistake is using `display: none` on an element you want to animate back in, which fails because the element has no box to transition from; for show/hide animations use `visibility` with a transition delay, or the newer `transition-behavior: allow-discrete` with `@starting-style`.

## Tip: Use outline, not border, for focus rings

If you style `:focus-visible` with `border`, focusing an element shifts its neighbors by the border width and causes a visible jump. Always use `outline` (or `box-shadow` as a ring) for focus indicators: they draw without affecting layout. Pair `outline: 2px solid` with `outline-offset: 2px` for a focus ring that has a gap, which is both more visible and more polished.

## Try it: Try it

See the four `overflow` values side by side on the same overflowing content, then compare `outline` vs `border` to watch outline draw without shifting layout.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/overflow-and-display

## Check yourself

### Question 1

You want a box that clips its overflowing content but does NOT show a scrollbar and can NOT be scrolled programmatically via scrollTop. Which value do you use?

- A. overflow: clip
- B. overflow: hidden
- C. overflow: scroll
- D. overflow: visible

Correct answer: A

`overflow: clip` clips content like `hidden` but the element is not a scroll container, so it cannot be scrolled programmatically or by user input. Use it when you want pure visual clipping with no scroll behavior. `hidden` keeps the scroll container.

- Why not B: `hidden` clips and hides the scrollbar, but the box is still a scroll container: JS can set `scrollTop` and users can scroll with a trackpad or keyboard in some browsers. `clip` removes that behavior.
- Why not C: `scroll` always shows a scrollbar and is fully scrollable. You want no scrolling at all.
- Why not D: `visible` is the default: content spills out and is not clipped at all.

### Question 2

You add `outline: 3px solid red` to a button. What happens to the layout of the button and its neighbors?

- A. Nothing shifts; the outline draws over neighboring content without taking up space
- B. The button grows by 3px and pushes neighbors outward
- C. Only the button's own size changes, neighbors stay put
- D. The outline is invisible until the button is focused

Correct answer: A

Outline is not part of the box model. It draws outside the border edge and never affects the element's size or its neighbors' positions. This is exactly why it is the correct property for focus rings: highlighting an element must not reflow the page.

- Why not B: That is how `border` behaves, not `outline`. Border is in the box model and takes up space; outline is outside the box model and takes up none.
- Why not C: Outline does not change the button's own box size either. It draws outside the border edge without affecting any layout, including the element it is on.
- Why not D: `outline` is always visible when set. `:focus-visible` is a selector that only matches during keyboard focus; the outline property itself has no built-in focus dependency.

### Question 3

An element is set to `display: none`. Which statement is TRUE?

- A. It is removed from layout and from the accessibility tree, so it takes up no space and screen readers skip it
- B. It takes up space but is invisible, and screen readers still read it
- C. It takes up space and is visible, but cannot be clicked
- D. It is removed from layout but still read by screen readers

Correct answer: A

`display: none` fully removes the element: no box, no layout space, no accessibility tree entry. This is the key difference from `visibility: hidden`, which keeps the layout space and is inherited. Use `none` for true removal; use the visually-hidden pattern when content should reach screen readers but not sighted users.

- Why not B: That describes `visibility: hidden`, not `display: none`. `none` removes the box entirely from the layout AND the accessibility tree.
- Why not C: That describes `pointer-events: none`. `display: none` makes the element invisible and space-free.
- Why not D: `display: none` removes the element from the accessibility tree too. To hide visually but keep it for screen readers, use the visually-hidden pattern (clip + absolute + 1px size), not `display: none`.

## Remember this

- `overflow: auto` shows a scrollbar only when needed; `hidden` clips but stays scrollable via JS; `clip` clips and is not scrollable at all.
- Text truncation needs all three: `overflow: hidden` + `white-space: nowrap` + `text-overflow: ellipsis`.
- `outline` draws outside the box model and never shifts layout; use it for focus rings. `border` is in the box model and takes up space.
- `display: none` removes the element from layout AND the accessibility tree. `visibility: hidden` keeps the space. `display: contents` dissolves the wrapper so children join the parent's layout.
