Preventing scrollbar-induced content shifting in Material-UI components: A guide

My browser app displays content dynamically, which can vary in length compared to the screen size. This causes a vertical scrollbar to appear and disappear on desktop browsers, leading to the content shifting left and right.

To prevent this, I apply style="overflow-y: scroll" to the body tag, ensuring the scrollbar is always visible - it simply remains disabled when the content is shorter than the screen.

The issue arises when using the material-ui SwipeableDrawer component for a sliding menu option. It appears that material-ui actively removes my overflow-y styling on the body tag when displaying the menu, but fails to reinstate it afterward.

  • Here's an example app published on Netlify.
  • Check out the source code here.
  • A screenshot illustrates how the content initially remains stable with the disabled scrollbar visible, only to shift left/right once the menu is shown.

https://i.sstatic.net/Swdi8.gif

This behavior exclusively occurs on desktop, as both Chrome and Firefox exhibit the issue. However, mobile devices like iOS do not experience this problem since the scrollbar is displayed over the content.

I prefer not to track and modify material-ui's behavior by adding/removing padding, especially considering that other components (e.g., poppers, tooltips) might behave similarly. Managing multiple components and adjusting padding seems error-prone and cumbersome.

Is there a way to have the vertical scrollbar overlay the content like it does on mobile browsers? Adding padding to ensure visibility would be necessary, but at least the solution would remain static, resolving the issue persistently.

Answer №1

To overcome this issue, you can opt for a solution that involves turning off the scroll lock like so:

<SwipeableDrawer disableScrollLock={true} ... >

However, a drawback of this approach is that you'll have to manually remember to apply this workaround each time you use any material-ui component with the disableScrolllock feature - such as popovers, popups, and more.

Alternatively, you can customize the default props for components. The following snippet illustrates how this can be done for SwipableDrawer (as Drawer inherits from Modal):

export const theme = createMuiTheme({
  props: {
    MuiModal: {
      disableScrollLock: true,
    },
  },
});

Note: It seems that the disableScrollLock attribute may no longer be necessary in current versions of mui (i.e., material-ui version 5) - although setting

style="overflow-y: scroll"
on your root body element or equivalent location is still crucial.

Answer №2

To address this issue, one solution is to

adjust the vertical scrollbar to be positioned at the top

by specifying the width of the body equal to the full viewport:

<body style="width: 100vw;">

This will cause the body element to stretch all the way beneath the scrollbar (similar to how it appears on mobile browsers).

However, a drawback of this approach is that any content located on the right side of the screen might be covered by the scrollbar.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble rendering the response text from the API fetched in Next.js on a webpage

I've written some code to back up a session border controller (SBC) and it seems to be working well based on the output from console.log. The issue I'm facing is that the response comes in as a text/ini file object and I'm unable to display ...

Is it possible to make the v-toolbar-title fixed within a v-navigation-drawer using Vuetify?

Can the <v-toolbar-title> be fixed within a <v-navigation-drawer>? <v-card class="d-inline-block elevation-12"> <v-navigation-drawer hide-overlay permanent stateless height="440" value="true"> <v-toolbar color="whi ...

Aligning a sprite at the center of a div background

My button has a unique design where it consists of a sprite set as the background image of a div. The sprite sheet is laid out horizontally, and I have also scaled down the source image to fit the div. .button { background: url(sprite.png); backgr ...

How can I achieve an endless scrolling text effect within a square on a webpage using CSS?

My header currently displays a horizontal infinite scroll of text, but I am looking to achieve a square pattern wrapping around the page. Can you provide guidance on how this can be accomplished? Below is the code snippet I am working with: .marquee { ...

The Github API checks are currently not properly parsing style attributes when given in the form of HTML within comment text

I have integrated github's create check run API to create a check run for my pull request. The JSON structure I am using for the request body is as follows: { "name" : name, "head_sha" : script.env.CI_COMMIT_SHA, ...

WP How can I modify a particular thumbnail size with a custom CSS class?

I have been attempting to use add_image_size for my custom thumbnail resize, but it seems to be having trouble maintaining the thumbnail size at 220 x 220. Instead, it keeps changing the height to 167 pixels. As a workaround, I am exploring a CSS-based sol ...

Is there an issue with JQM plugin due to jQuery's append() function?

I am just starting to learn about jQuery mobile as I develop an app using it. Whenever I click on the Add_details button, I want to add dynamic input fields only once. After saving, if I click on the Add_details button again, I need to append more dynamic ...

Is there a way to disable certain CSS features in Chrome, like CSS grid, for testing purposes?

I'm exploring alternative layouts for my app in case CSS grid is not supported. Is there a way to disable features like CSS grid in Chrome or Canary? ...

Identifying the HTML Hidden Attribute Using JavaScript Without Dependencies

As someone working in the analytics field, I often rely on CSS selectors to address various issues. Currently, I am faced with a task on a website where I need to determine whether a <p> element is hidden or visible. There are two possible scenarios: ...

The min-height attribute of the tooltip functions, but the min-width attribute does

I found a helpful tooltip on this site: I customized it to work with an input element: <div class="tooltip"> <input type="text" placeholder="Name" name="dp_name"> <span>Full name asd as dasd as dasd</span> </div> ...

Guide to aligning a fraction in the center of a percentage on a Materal Design progress bar

Greetings! My objective is to create a material progress bar with the fraction displayed at the top of the percentage. https://i.sstatic.net/GbphJ.png Currently, I have managed to show the fraction at the beginning of the percentage. Below is the code sn ...

Shorten the text in a flex container using ellipsis and flex-grow

I have a "table" built using flex and I need to truncate the text in the first column if there isn't enough space, keeping it on a single line all the time. .parent { display: flex; border: 1px solid black; gap: 10px; } .first-column { bor ...

Having trouble navigating to the bottom of a VUEJS app?

I've been working on developing a chatbot app that utilizes a REST API to stream content. While the functionality of the app is running smoothly, I have encountered an issue with the scroll to bottom feature. Instead of automatically scrolling to disp ...

Error occurred during SCSS rendering process: [Internal Error: Unable to locate or read the specified file.]

When trying to create a website using scss, I encounter an error every time I save the scss file. Strangely, after saving it multiple times, the file will render properly. Although this issue is minor, it really bothers me. I have searched for a solution ...

Map through the index and render the component while closing the tag

I have a piece of code that I'm working on. How can I dynamically add a tag based on the index? My goal is to create a grid with 3 columns. <div> {items.map((row, i) => ( {i % 3 === 0 && <div className={'grid-cont ...

Allowing breaks with the :nth-child() selector

There must be a straightforward explanation for this perplexing issue. However, in my view, it ought to function as intended. We are currently utilizing the Bootstrap 3 CSS framework. The following code is present on a specific page: <div class="promo ...

My footer is now overlaying both my content and navbar

I am facing an issue with my footer. When I test my new footer, it ends up covering the content and navbar. I have been trying to figure out how to solve this problem but haven't had any luck yet. Hopefully, I can find some answers here... $(".toge ...

Tips for connecting Angular events to <input> elements in HTML?

Within my Angular application, the value of a variable is manipulated by an HTML element <input> through two-way binding like this: <input [(ngModel)]=variableName (OnKeyup)="DoSomething" > However, the two-way binding with ...

Guide on darkening the surrounding div of an alert to give it a modal-like effect

I want to display an alert to the user in a visually appealing way. To achieve this, I am utilizing Bootstrap's alert class. Here is how I am showing the user a div: <div class="alert alert-warning alert-dismissible" role="alert"> Some text ...

What is the best way to conceal an unordered list menu?

Trying to create a menu using ul HTML, but encountering an issue. When the website is opened, the menu shows up by default, whereas I only want it to appear when a mouse hovers over it. Even after attempting to change the CSS class, the desired functionali ...