html

Remove the Horizontal Scrollbar CSS

To hide or remove a scrollbar from the web page if your web design permits it, CSS is a technology that makes it simple to make the scrollbar of an HTML element disappear. Depending on your use case, there are multiple methods to do so. When the length or width of the content on a web page exceeds the visible browser area, scrollbars are automatically displayed by default in browsers. Therefore, no scrollbars will show up if the content does not exceed the horizontal and vertical limits/boundaries of the browser’s window.

Why remove the scroll bar?

Although it is not recommended in web design to change the default browser behavior, there are times when it is necessary. For instance, a scrollbar that emerges once an element’s content overflows may be difficult to perceive if it occupies a small portion of a webpage. Additionally, removing the scrollbar entirely makes sense because, as a web designer, you might not think of scrolling on a webpage. Regardless of the justification for CSS scrollbar hiding, cross-browser compatibility should be considered. Additionally, after hiding the scrollbar, preview the website to make sure it works as intended on desktop and mobile devices.

Removing scroll-bar in CSS:

Content that exceeds the boundaries of its container can be handled using the overflow attribute in CSS. If the content of an element is larger than the available space, you can use this property to clip, conceal, or scroll the content. Features for handling scrollable overflow in visual media are included in this CSS module. In CSS, overflow occurs when a container’s contents go past one or more of the container’s edges. In essence, it specifies what occurs when an element’s contents are too big to fit within its block context.

The over-flow property of CSS supports the following values:

auto: To add a scrollbar, but it will only do so if the content exceeds the scrollable area.

visible: To enable content to display even when it overflows.

scroll: To clip the content and to see the remaining content, it will add a scrollbar.

hidden: It will show the visible content, and the remaining content will be concealed.

clip: It pins an element’s contents to its box.

We can set the scrollbar values with overflow-x to control the horizontal scrollbar, overflow-y for the vertical scrollbar and overflow will support both.

Example 01: Hiding the horizontal scrollbar in CSS

Let’s start with the basic example of removing the scrollbar from the webpage using CSS. Unfortunately, there isn’t a specific CSS rule that can be used to make the scrollbar disappear and yet allow users to scroll a page or element. With the use of a few rules of browser-specific CSS, this is still doable. Apply the following CSS to the body of the webpage (for the full page) or a single element to hide or remove the scrollbar and maintain scrolling functionality.

As seen above, we can use different ways to make the scrollbars disappear in different browsers. In the above example, the element is a specific area or selector from which you want to remove the scrollbars. In this example, we will create a simple sample page to show you how you can hide the horizontal scrollbar, but its scrolling functionality will work. We will create a webpage page by using <html>> tags. Inside these tags, we will create head and body elements to define the root and body of the element.

The attribute ‘display:none’ will make the bar disappear without affecting its scrolling functionality.

To hide the horizontal scroll bar, we will allow our content to exceed the horizontal limit of the page. For this we will use “white-space: nowrap;” This will allow the text to remain on the same line until we use the break line tag. In older browsers like ‘Internet Explorer’, we will set the ‘attribute -ms-overflow-style: none’. While in modern browsers, like in Firefox, we can use ‘scrollbar-width: none’ and for Chrome and other modern browsers, ‘display: none’ can be used. When content extends beyond the block-level element’s horizontal boundaries, the overflow-x CSS property determines what displays.

We can do the same for the div elements by using div selectors in CSS.

Using the above code, we will get a webpage as follows:

As you can see, this is the same as the previous one, but this time we used the div selectors to hide the scrollbars of the containers. We have stylized the div by specifying its border to be 5px solid black, border-radius 5px, height of 150px and width of the div container to be 300px.

Example 02: Removing the Scrollbar and disabling its functionality in CSS

Through its CSS overflow feature, CSS enables the ability to both to disappear the scrollbar and disable scrolling. Handling the content that exceeds the limits of its container is managed by these properties. Hiding the horizontal scrollbar can be helpful because horizontal scrolling is often a terrible choice. By using ‘overflow-x: hidden,’ we can make the horizontal scrollbar disappear and stop horizontal scrolling. We simply added the ‘overflow: hidden’ rule to the body of a container element to stop scrolling while using this attribute. This will hide all material that extends past the element’s boundary.

By using this code, our horizontal scrollbar will disappear and we will be unable to scroll left or right.

As you can see, using ‘overflow-x: hidden’ has removed the horizontal scrollbar as well as the functionality to scroll left or right.

However, to display material that extends the limits of the container, use overflow: visible as an alternative.

As you can see, the image is exceeding the boundaries of the container.

Example 03: Removing the horizontal scrollbar until we need it

In some situations, hiding the scrollbar might improve your design. In other cases, deleting this portion of the page can make the user experience worse. The majority of visitors connect a scrollbar with the action of scrolling. Therefore, it could seem unusual to others if you use this technique throughout the entire website. Additionally, scroll bars indicate how much of the page is still visible. The lack of a scrollbar may be off-putting unless you have another visual cue that there is still more content to be seen. User testing can reveal how the absence of a scrollbar affects your site’s user experience, although in many situations, hiding the scrollbar until the user starts scrolling is a smart compromise.

As overflow is set to auto, the bars will not appear if the content is not larger than the boundaries of the browser window.

As you can see, there is no horizontal scrollbar as the limit has not been exceeded. When the limit is exceeded the scrollbar will appear on the screen for scrolling.

As you can see in the HTML code, the content in <p> is larger than the previous example, so the horizontal scrollbar will appear at the bottom.

The scrollbar is added by the auto setting, but it won’t show up unless there is content overflow. As a result, the concealed scrollbar only appears when required.

Conclusion

Since it facilitates navigation, the scrollbar is a helpful visual indicator for the majority of visitors. However, in some circumstances, you can purposefully hide it to improve or change your design. With just a few principles from our article, you can use CSS to hide the scrollbar on a page or HTML element. In this tutorial, we have learned how to hide the scrollbar and how this feature works in different browsers. Now, after completing this tutorial, you should be able to easily remove or hide the scrollbars by yourself.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.