- Chrome and Safari exclude the scrollbar dimensions.
- Firefox, Opera and IE include the scrollbar dimensions.
@media (min-width: 800px) { ... }
Chrome and Safari will include those styles when the body width is 800px or greater. However, if your OS shows vertical scrollbars with a width of 20px, other browsers would include the styles at 780px.
Let’s read the W3C media query specification for width:
The ‘width’ media feature describes the width of the targeted display area of the output device. For continuous media, this is the width of the viewport (as described by CSS2, section 9.1.1 [CSS21]) including the size of a rendered scroll bar (if any).The Webkit engine is wrong. But is it better? At first glance, Chrome’s implementation seems logical but it’s important to understand that ‘width’ refers to the viewport dimensions, i.e. the total area inside the browser window — not the page width. Using the assumptions above, Chrome’s viewport width is actually 820px (800px body + 20px scrollbar). In other browsers, the viewport width is 800px (780px body + 20px scrollbar). Unfortunately, knowing the viewport width does not aid CSS development since you don’t know whether the scrollbar is visible or what its dimensions are. However, Chrome’s alternative is far worse since the introduction or removal of a scrollbar can trigger a media query. This is a little complex, so I’ll explain with an example:
- Assume you have a simple responsive site which shows a desktop layout at 800px or greater and a mobile site at 799px or less. Scrollbar widths are 20px.
- Currently, the user has their browser window sized to an 810px viewport width. The height is large enough to show the current page (in desktop layout) without scrollbars.
- You click through to another page. Here, the content is longer than the viewport height and a vertical scrollbar appears. This reduces the body width to 790px and Chrome instantly switches from the desktop to mobile layout. The user becomes confused and vows never to return to your site again.
- Mobile browsers do not normally display permanent scrollbars so the problem is never apparent on those devices.
- A desktop browser user would be unlucky to have window dimensions set at a point where layouts noticeably changed.
- You could prevent Chrome switching layouts by ensuring the vertical scrollbar is always visible, i.e.
body { overflow-y: scroll; }
.
Frequently Asked Questions (FAQs) about Responsive Scrollbars
What is a responsive scrollbar and why is it important?
A responsive scrollbar is a scrollbar that adjusts its size and functionality based on the size of the device screen or browser window. It is important because it enhances the user experience on different devices. With the increasing use of mobile devices, it’s crucial to ensure that your website or application is easily navigable on all screen sizes. A responsive scrollbar ensures that users can easily scroll through your content, regardless of the device they are using.
How can I create a responsive scrollbar in CSS?
Creating a responsive scrollbar in CSS involves using media queries to adjust the scrollbar’s properties based on the screen size. Here’s a basic example:/* For desktop: */
::-webkit-scrollbar {
width: 1em;
}
/* For mobile: */
@media (max-width: 600px) {
::-webkit-scrollbar {
width: 0.5em;
}
}
In this example, the scrollbar’s width is set to 1em for desktop screens and 0.5em for screens smaller than 600px.
Why does my scrollbar disappear on mobile devices?
Some mobile browsers hide scrollbars to save screen space. However, they should appear when the user starts scrolling. If your scrollbar is not appearing at all, it could be due to a CSS issue. Make sure you’re not setting the scrollbar’s width or height to 0 on mobile devices.
How can I customize the appearance of my scrollbar?
You can customize the appearance of your scrollbar using CSS. The ::-webkit-scrollbar
pseudo-element is used to change the appearance of the scrollbar in WebKit browsers. You can change the width, color, and other properties of the scrollbar. Here’s an example:::-webkit-scrollbar {
width: 1em;
background-color: #F5F5F5;
}
::-webkit-scrollbar-thumb {
background-color: #000000;
}
In this example, the scrollbar’s width is set to 1em, the scrollbar’s background color is set to light gray, and the scrollbar thumb’s color is set to black.
How can I make my scrollbar responsive in Chrome?
Chrome supports the ::-webkit-scrollbar
pseudo-element, which you can use to make your scrollbar responsive. You can use media queries to adjust the scrollbar’s properties based on the screen size. Here’s an example:/* For desktop: */
::-webkit-scrollbar {
width: 1em;
}
/* For mobile: */
@media (max-width: 600px) {
::-webkit-scrollbar {
width: 0.5em;
}
}
In this example, the scrollbar’s width is set to 1em for desktop screens and 0.5em for screens smaller than 600px.
How can I make my scrollbar responsive in Firefox?
Firefox supports the scrollbar-width
property, which you can use to adjust the width of the scrollbar. However, it does not support the ::-webkit-scrollbar
pseudo-element. To make your scrollbar responsive in Firefox, you can use media queries along with the scrollbar-width
property. Here’s an example:/* For desktop: */
html {
scrollbar-width: thin;
}
/* For mobile: */
@media (max-width: 600px) {
html {
scrollbar-width: auto;
}
}
In this example, the scrollbar’s width is set to ‘thin’ for desktop screens and ‘auto’ for screens smaller than 600px.
How can I make my scrollbar responsive in Safari?
Safari supports the ::-webkit-scrollbar
pseudo-element, similar to Chrome. You can use media queries to adjust the scrollbar’s properties based on the screen size. Here’s an example:/* For desktop: */
::-webkit-scrollbar {
width: 1em;
}
/* For mobile: */
@media (max-width: 600px) {
::-webkit-scrollbar {
width: 0.5em;
}
}
In this example, the scrollbar’s width is set to 1em for desktop screens and 0.5em for screens smaller than 600px.
How can I make my scrollbar responsive in Internet Explorer?
Internet Explorer does not support the ::-webkit-scrollbar
pseudo-element or the scrollbar-width
property. However, you can use JavaScript to adjust the scrollbar’s properties based on the screen size.
Can I use JavaScript to make my scrollbar responsive?
Yes, you can use JavaScript to adjust the scrollbar’s properties based on the screen size. However, this is more complex and less efficient than using CSS. It’s recommended to use CSS whenever possible.
Are there any libraries or plugins that can help me create a responsive scrollbar?
Yes, there are several libraries and plugins that can help you create a responsive scrollbar. Some popular ones include SimpleBar, OverlayScrollbars, and Perfect Scrollbar. These libraries provide a lot of customization options and are easy to use. However, they add extra dependencies to your project and may not be necessary if you only need basic scrollbar customization.
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.