Question
How can I use CSS to hide the scrollbar on an HTML page? I want to understand how to hide the scrollbar while optionally keeping the page content scrollable.
Short Answer
You will learn the difference between hiding a scrollbar and disabling scrolling, the CSS rules needed for major browsers, and how to apply them safely to a page or a specific scrollable element.
Concept
A scrollbar is the visual control shown when an element has more content than it can display at once. In CSS, scrolling behavior is controlled primarily by the overflow property.
There are two different goals that are often confused:
- Hide the scrollbar but allow scrolling: Users can still scroll with a mouse wheel, touch gesture, keyboard, or trackpad.
- Disable scrolling completely: Extra content is clipped and cannot be reached by scrolling.
To hide a scrollbar while retaining scrolling, use browser-specific scrollbar styling rules alongside an overflow value such as auto or scroll.
This matters because scrollable content must remain reachable. Hiding the visual scrollbar can create a cleaner layout, but it can also make it less obvious that more content exists. Use this technique only when the interface still provides a clear indication that content can scroll.
Mental Model
Think of a scrollable area as a long document inside a window.
overflow: autolets you move the document when it is longer than the window.- A scrollbar is the visible handle on the side of that window.
- Hiding the scrollbar removes the handle, not the document's ability to move.
overflow: hiddenlocks the window: content outside the visible area still exists, but you cannot move to it.
So, hiding a scrollbar and turning off scrolling are separate actions.
Syntax and Examples
Use a class when you want to hide the scrollbar on a particular scrollable container:
.scrollable-without-bar {
overflow: auto;
-ms-overflow-style: none; /* Internet Explorer and legacy Edge */
scrollbar-width: none; /* Firefox */
}
.scrollable-without-bar::-webkit-scrollbar {
display: none; /* Chrome, Safari, and newer Edge */
}
<div class="scrollable-without-bar">
<p>Long content goes here...</p>
</div>
The element must have a constrained size for scrolling to become visible. For example:
.scrollable-without-bar {
height: 200px;
overflow-y: auto;
-ms-overflow-style: none;
scrollbar-width: none;
}
.scrollable-without-bar::-webkit-scrollbar {
display: none;
}
To apply this to the entire page, target the root document element:
html {
-ms-overflow-style: none;
scrollbar-width: none;
}
html::-webkit-scrollbar {
display: none;
}
The page remains scrollable if its content exceeds the viewport height.
Step by Step Execution
Consider this scrollable panel:
<div class="news-panel">
<p>Item 1</p>
<p>Item 2</p>
<p>Item 3</p>
<p>Item 4</p>
<p>Item 5</p>
<p>Item 6</p>
</div>
.news-panel {
height: 100px;
overflow-y: auto;
scrollbar-width: none;
}
.news-panel::-webkit-scrollbar {
display: none;
}
Step by step:
height: 100pxlimits the panel to 100 pixels of visible height.- The six paragraphs are taller than 100 pixels, so some content lies below the visible portion.
overflow-y: automakes the panel vertically scrollable.scrollbar-width: nonehides Firefox's scrollbar.::-webkit-scrollbar { display: none; }hides the scrollbar in Chromium-based browsers and Safari.- The user can still scroll the panel using a wheel, touch gesture, trackpad, or keyboard when the panel has focus.
Real World Use Cases
Common uses include:
- Image galleries: Let users swipe or horizontally scroll through cards without displaying a large scrollbar.
- Mobile navigation: Create a horizontally scrollable category row with a clean appearance.
- Dashboard widgets: Keep a compact activity feed scrollable inside a fixed-height panel.
- Fullscreen or kiosk interfaces: Reduce visual clutter where scrolling is already communicated by the design.
- Custom-scrolling components: Hide the native bar when the application supplies a well-tested custom scroll indicator.
For important text, forms, or data tables, a visible scrollbar is often better because it clearly communicates that additional content is available.
Real Codebase Usage
In real projects, developers usually avoid applying hidden-scrollbar rules globally unless the design specifically requires it. A reusable utility class is safer:
.u-hide-scrollbar {
-ms-overflow-style: none;
scrollbar-width: none;
}
.u-hide-scrollbar::-webkit-scrollbar {
display: none;
}
Then combine it with explicit scrolling behavior where needed:
.product-row {
display: flex;
gap: 1rem;
overflow-x: auto;
overscroll-behavior-x: contain;
}
<div class="product-row u-hide-scrollbar">
<!-- product cards -->
</div>
Useful real-codebase patterns include:
- Use
overflow-x: autofor a horizontal card row and avoid changing vertical page scrolling. - Use
overflow-y: autofor a fixed-height panel. - Add a gradient edge, partial next card, arrow buttons, or instructional text so users can discover hidden content.
- Keep keyboard access in mind. A non-interactive scrolling container may need
tabindex="0"when keyboard scrolling is important. - Test on touch devices, mouse-wheel devices, and keyboard-only navigation.
Common Mistakes
Using overflow: hidden when scrolling should remain possible
/* This hides overflow and prevents scrolling. */
body {
overflow: hidden;
}
Use overflow: auto instead when users need to reach overflowing content.
Styling only one browser family
/* Works in Chromium browsers and Safari, but not Firefox. */
.panel::-webkit-scrollbar {
display: none;
}
Add scrollbar-width: none for Firefox.
Expecting a scrollbar without a scrollable area
.panel {
overflow-y: auto;
}
If the panel has no height or max-height constraint, it may grow to fit all of its content and never need to scroll. Add a size constraint:
.panel {
max-height: 20rem;
overflow-y: auto;
}
Hiding the only clue that content can scroll
A scrollbar helps users discover content. If you hide it, provide another cue, such as a partly visible next card, an arrow control, or a fade at the edge.
Applying page-level scrolling rules to a component
Using body { overflow: hidden; } to fix a small panel can stop the entire page from scrolling. Target the component's class instead.
Comparisons
| Approach | Scrollbar visible? | Can the user scroll? | Best use |
|---|---|---|---|
overflow: auto | Usually, when needed | Yes | Normal scrollable content |
overflow: scroll | Usually always | Yes | Reserve scrollbar space or force a bar |
overflow: hidden | No | No | Clip decorative overflow or temporarily lock a page |
overflow: clip | No | No | Clip overflow without creating a scroll container |
scrollbar-width: none plus ::-webkit-scrollbar | No | Yes, if overflow allows it | Hide native bars across major browsers |
overflow decides whether content can scroll. Scrollbar styling decides whether the scrollbar is shown. They are commonly used together but solve different problems.
Cheat Sheet
/* Hide a scrollable element's scrollbar across major browsers */
.hide-scrollbar {
overflow: auto;
-ms-overflow-style: none; /* Legacy Internet Explorer/Edge */
scrollbar-width: none; /* Firefox */
}
.hide-scrollbar::-webkit-scrollbar {
display: none; /* Chrome, Safari, Chromium Edge */
}
/* Vertical-only scrolling */
.panel {
max-height: 300px;
overflow-y: auto;
}
/* Horizontal-only scrolling */
.row {
overflow-x: auto;
overflow-y: hidden;
}
- Use
overflow: autoto allow scrolling only when content overflows. - Use
overflow: hiddenonly when content should not be scrollable. - A container needs a width, height,
max-width, ormax-heightconstraint before overflow is likely to occur. - Hiding the scrollbar does not automatically make a container keyboard-focusable.
- Test the result in Firefox, Safari, and a Chromium-based browser.
FAQ
Can I hide a scrollbar with CSS and still scroll?
Yes. Keep overflow: auto or overflow: scroll, then use scrollbar-width: none for Firefox and ::-webkit-scrollbar for Chrome, Edge, and Safari.
Does overflow: hidden hide only the scrollbar?
No. It also prevents users from scrolling to the hidden content.
Why does scrollbar-width: none not work in Chrome?
scrollbar-width is used by Firefox. Chrome, Edge, and Safari use the ::-webkit-scrollbar pseudo-element for this purpose.
Why is my element not scrolling after I add overflow: auto?
It may not have a size constraint. Add height, max-height, width, or max-width so that its content can overflow.
Should I hide the page scrollbar?
Only if users can still clearly discover and use scrolling. Hiding it can reduce accessibility and make long pages appear to end early.
Can I hide only the horizontal scrollbar?
Yes. Use overflow-x: auto for horizontal scrolling and apply the scrollbar-hiding rules to that element. Be careful: browser scrollbar styling often affects both scrollbar directions on the same element.
Is -ms-overflow-style required for modern browsers?
It is mainly for Internet Explorer and legacy Edge. Most modern projects can omit it if they do not support those browsers.
Mini Project
Description
Build a horizontally scrollable product-card row. The row should remain usable with a mouse wheel, trackpad, touch gesture, or keyboard, while its native scrollbar is hidden for a cleaner interface.
Goal
Create a responsive product row that scrolls horizontally without showing its native scrollbar.
Requirements
Requirement 1
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
Apply CSS to an iframe: Same-Origin Styling in HTML
Learn why parent-page CSS does not style iframe content, how same-origin rules work, and practical ways to style embedded pages safely.
Apply Multiple CSS Transforms: rotate() and translate()
Learn how to combine multiple CSS transform functions such as rotate() and translate(), understand transform order, and avoid overwritten rules.