Has this ever happened to you? You‘re browsing a website, perhaps reading an article or viewing a product page, when you notice something strange. There‘s an odd gap on the right side of the screen, and you can scroll sideways to reveal hidden content.
This phenomenon is known as horizontal overflow, and it occurs when an element on the page is wider than the visible area. Horizontal scrolling is sometimes used intentionally for certain elements like wide data tables. But more often than not, this sideways overflow happens unintentionally due to a styling issue and ends up breaking the layout of the page.
Unexpected horizontal scrolling creates a poor experience for users. It looks unprofessional, makes content harder to read, and can be frustrating to navigate, especially on mobile devices. In most cases, it‘s best to disable horizontal overflow and eliminate the sideways scrollbar altogether.
In this guide, we‘ll walk you through several methods to remove horizontal scrolling on your WordPress site. Whether you‘re comfortable editing code or prefer to use plugins, you‘ll learn how to keep your content contained within the proper width and provide a better browsing experience. Let‘s get started!
Why You Should Disable Horizontal Overflow
Before we dive into the tutorial, let‘s examine some key reasons to remove unintended sideways scrolling from your site:
Improved layout and design – Horizontal scrollbars can wreak havoc on your carefully designed page layout. Text, images, and other elements can get cut off or appear in the wrong place. Removing overflow ensures your content displays properly.
Better usability – Visitors expect to scroll up and down, not left and right. Sideways scrolling can easily go unnoticed. When it is noticed, it‘s awkward to use, particularly on mobile. Disabling it makes your pages more intuitive to navigate.
Increased professionalism – Unless done deliberately and well, horizontal scrollbars look amateurish and unpolished. They suggest a lack of attention to detail in design. Eliminating them helps your site look sleek and professional.
Reduced confusion – When parts of the page are cut off by overflow, it can confuse users. They may think content is missing or the page hasn‘t loaded correctly. Pages without horizontal scrolling appear more complete and instill confidence in visitors.
In short, most of the time, sideways scrolling causes more harm than good. It‘s a common problem, but also an easy one to solve. Disabling overflow takes just a few minutes and will make a big difference in how users perceive and interact with your WordPress site.
Now that you know the importance, let‘s look at how to actually disable horizontal scrollbars.
Method 1: Disable Horizontal Overflow with CSS in WordPress Customizer
The quickest way to get rid of a horizontal scrollbar is to add a bit of CSS code that tells browsers not to allow horizontal overflow. Don‘t worry – even if you‘re not too familiar with CSS, this is a simple snippet you can copy and paste! We‘ll add the code through the WordPress Customizer, so you won‘t have to edit your theme files directly.
Here are the step-by-step instructions:
- In your WordPress dashboard, go to Appearance > Customize.
- In the Customizer sidebar, click the Additional CSS tab.
- Click inside the code editor box to paste the following CSS rule:
html, body {
max-width: 100%;
overflow-x: hidden;
}Here‘s what this code does:
html, bodytargets both the root<html>element and the<body>element of the pagemax-width: 100%sets the maximum width of those elements to 100% of the browser window widthoverflow-x: hiddenspecifies that any horizontal overflow beyond that width should be hidden and not scrollable
- Click the Publish button at the top to save your changes.
That‘s it! The horizontal scrollbar should now be gone from your site. Since we added the CSS in the Customizer, you can see a live preview of your site as you make changes. If you want to tweak the code further, you can edit it right here and see the results instantly.
Note that this method applies the no-overflow rule globally to your entire site. In some cases, you may want to allow horizontal scrolling on certain pages or elements (more on that later). But for most WordPress sites, this quick CSS fix is all you need.
Method 2: Disable Horizontal Overflow with a WordPress Plugin
If you‘re not comfortable editing code yourself or want an even easier solution, you can install a plugin to handle disabling horizontal overflow. There are a few plugins that provide this functionality, but we recommend WP Add Custom CSS because it‘s lightweight, focused, and frequently updated.
Here‘s how to disable horizontal scrolling with WP Add Custom CSS:
- In your WordPress dashboard, go to Plugins > Add New.
- Search for "WP Add Custom CSS" in the plugin search bar.
- Click the Install Now button next to the plugin name, then click Activate.
- In the left sidebar, click Add Custom CSS to access the plugin settings.
- In the CSS code editor, paste the same snippet from Method 1:
html, body {
max-width: 100%;
overflow-x: hidden;
}- Click the Save CSS button to store your changes.
And you‘re done! Just like with the manual CSS method, the horizontal scrollbar will now be disabled on your site. WP Add Custom CSS includes a few extra features like revision history, LESS support, and an option to add CSS to the login page. But for our purposes, it provides a user-friendly interface to input CSS without dealing with theme files directly.
One advantage of using a plugin is that your CSS will be safely stored in the WordPress database, independent of your theme. So if you change themes in the future, you won‘t lose your custom styles. The same CSS can also be added to multiple sites quickly using a plugin.
Method 3: Disable Horizontal Overflow in functions.php or style.css
For more advanced users who are comfortable editing theme files, you can also disable horizontal scrollbars by adding CSS directly to your child theme‘s functions.php file or style.css stylesheet. Modifying theme files gives you more control but also requires more caution, as incorrect code can break your site.
Here‘s how to add the CSS in your theme:
Option A: Using functions.php
- In your WordPress dashboard, go to Appearance > Theme File Editor.
- Note: If you don‘t see the Theme File Editor menu option, you may need to enable it by setting
define(‘DISALLOW_FILE_EDIT‘, false);in yourwp-config.phpfile.
- Note: If you don‘t see the Theme File Editor menu option, you may need to enable it by setting
- In the file list on the right, locate and click on
functions.phpto edit it. - Scroll down to the bottom of the file and paste the following code before the closing
?>tag:
function disable_horizontal_scroll() {
echo ‘<style type="text/css">
html, body {
max-width: 100%;
overflow-x: hidden;
}
</style>‘;
}
add_action(‘wp_head‘, ‘disable_horizontal_scroll‘);This code uses PHP to output the CSS into the <head> section of your site‘s HTML. The wp_head hook ensures the styles are added to every page.
- Click the Update File button to save your changes.
Option B: Using style.css
- In the Theme File Editor, locate and click on
style.cssto edit your theme‘s main stylesheet. - Scroll to the bottom of the file and paste in the CSS rule:
html, body {
max-width: 100%;
overflow-x: hidden;
}- Click the Update File button to save your changes.
With either of these methods, be sure to test your site thoroughly after making changes to theme files. If anything looks broken, you can revert the changes by editing the files again or using a backup plugin.
Tips and Considerations
While disabling horizontal overflow is usually a good idea, there are a few things to keep in mind to ensure it works well on your site. Here are some additional tips and potential issues to consider.
Check for Mobile Responsiveness
Hiding overflow can sometimes interfere with responsive design and mobile layouts. For example, if your theme has an off-canvas mobile menu that slides in from the side, setting overflow-x: hidden may prevent users from accessing it.
To avoid this issue, make sure to test your site on multiple devices after disabling horizontal scrolling. You may need to adjust the CSS to target only larger screen sizes using media queries. For instance, you could wrap the overflow-x rule inside a media query like this:
@media screen and (min-width: 768px) {
html, body {
max-width: 100%;
overflow-x: hidden;
}
}This would apply the rule only on screens wider than 768px, leaving mobile devices unaffected. The exact breakpoint to use depends on your theme‘s layout.
Allow Overflow on Certain Elements
In some cases, you might want to allow horizontal scrolling on a particular element, like a wide table or image gallery. To do this, you can add a class or ID to the element and override the global overflow-x rule. For example:
.wide-table {
overflow-x: auto;
}This would allow horizontal scrolling on any element with the wide-table class, while still preventing it on the rest of the page. Use this sparingly, as too many scrollable elements can still be confusing for users.
Accessibility Concerns
While removing horizontal scrollbars generally improves usability, it‘s important to note that completely disabling overflow can make some content inaccessible to certain users. For instance, people who rely on keyboard navigation may not be able to reach content that is hidden offscreen.
To mitigate this, avoid putting critical content in overflowing elements in the first place. If you do need to include wider elements, make sure they are clearly visible and provide alternative ways to access the information, such as data table pagination or image captions. Testing your site with tools like WAVE from WebAIM can help identify accessibility issues.
Related Reading: Resizing Elements to Fit the Screen
Sometimes horizontal scrollbars appear because an element like an image or video is simply too wide for the content area. In these cases, you can fix the problem at the source by resizing the element to fit within the available space.
For images, you can add the following CSS rule to make them responsive:
img {
max-width: 100%;
height: auto;
}This will ensure images never exceed the width of their container, while maintaining their aspect ratio. If you‘re using WordPress 5.5 or later, this responsive styling is included by default for images added through the block editor.
Similarly, you can make embedded videos responsive by wrapping them in a container div and using the following CSS:
.video-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}This will make the video scale proportionally to fit the width of the container, preventing horizontal overflow. Many WordPress video plugins like Embed Plus for YouTube handle this automatically.
By taking a responsive approach and ensuring elements adapt to the screen size, you can avoid horizontal scrolling issues in the first place.
Bonus: Customizing the Vertical Scrollbar
Now that we‘ve eliminated the horizontal scrollbar, you may want to customize the appearance of the main vertical scrollbar as well. While most browsers have a default scrollbar style, you can override it with CSS to match your site‘s design.
Here are a few examples of scrollbar styling you can add to your theme‘s stylesheet or custom CSS area:
/* Webkit browsers (Chrome, Safari, Edge) */
::-webkit-scrollbar {
width: 12px; /* width of the scrollbar */
}
::-webkit-scrollbar-track {
background: #f1f1f1; /* color of the track */
}
::-webkit-scrollbar-thumb {
background: #888; /* color of the thumb */
}
::-webkit-scrollbar-thumb:hover {
background: #555; /* color of the thumb on hover */
}
/* Firefox */
* {
scrollbar-width: thin;
scrollbar-color: #888 #f1f1f1;
}This CSS targets different parts of the scrollbar and applies custom colors and widths. Note that the WebKit styles (for Chrome, Safari, and Edge) use vendor prefixes and have more granular control over the track, thumb, and hover state. Firefox uses a different syntax with more limited options.
Scrollbar styling is not supported in all browsers (notably Internet Explorer), so it should be used as a progressive enhancement rather than a critical design element. Test your styles in multiple browsers to ensure a consistent experience.
Key Takeaways and Next Steps
Horizontal scrolling is a frequent annoyance on the web, but with a few small tweaks to your WordPress site, you can eliminate unintended sideways overflow and create a better user experience. To recap, here are three methods you can use to disable horizontal scrollbars:
- Add the
overflow-x: hiddenCSS rule in the WordPress Customizer - Install the WP Add Custom CSS plugin and add the CSS through its interface
- Paste the CSS directly into your theme‘s
functions.phpfile orstyle.cssstylesheet
Each method has its advantages, so choose the one that fits your comfort level with code and your specific needs. Remember to test your site thoroughly on different devices and screen sizes after making changes, and consider accessibility implications.
Beyond fixing horizontal overflow, you can further enhance your site‘s scrolling experience by:
- Ensuring images and videos are responsive and don‘t exceed the content width
- Customizing the vertical scrollbar style to match your design
- Improving overall performance and page load times to minimize scrolling lag
By taking these steps, you‘ll create a smoother, more professional, and user-friendly experience for your WordPress site visitors.
We hope this in-depth guide has given you the knowledge and tools to tackle horizontal scrolling on your own site. If you have any questions or additional tips to share, let us know in the comments below!
