Material-UI LeftNav with a sticky header

I attempted to create a fixed header, specifically a Toolbar, inside a LeftNav so that when the LeftNav is scrolled, the Toolbar remains in place. However, for some reason, setting position: 'fixed' on the Toolbar does not work within the LeftNav. When the content in the LeftNav exceeds the window height, the entire LeftNav, including the top Toolbar with a fixed position, scrolls along with it. Has anyone found a way to maintain a fixed position element within a LeftNav?

Below is the reference code:

...
const toolBarStyle = {
  position: 'fixed',
  top: 0,
};
return (
  <LeftNav
    open={open}
    docked={false}
    onRequestChange={onRequestChange}
  >
    <Toolbar style={toolBarStyle}> />
    {this.props.children} // children could be a lot of content
  </LeftNav>
);
...

Answer №1

After some experimentation, I finally cracked the code on this issue. By setting the LeftNav element to have a style of position: 'fixed', and wrapping the content in a div with overflowY: 'auto', I was able to achieve the desired outcome. Here's the updated code snippet:

......
render() {
const toolBarStyle = {
  position: 'absolute',
  top: 0,
};
const containerStyle = {
  position: 'fixed',
  top: 0,
  overflowY: 'hidden',
};
const contentContainerStyle = {
  marginTop: 57, // matching toolbar height
  width: '100%',
  height: this.props.windowSize.height - 57, 
  overflowY: 'auto',
};
return (
  <LeftNav
    style={containerStyle}
    docked={false}
    onRequestChange={onRequestChange}
  >
    <Toolbar style={toolBarStyle} />
    <div style={contentContainerStyle}>
      {this.props.children}
    </div>
  </LeftNav>
);
...

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

What is preventing my accordion from closing completely?

I'm currently working on creating FAQ questions in an accordion format. However, I've encountered an issue where adding padding around the answer section prevents the accordion from closing completely. Removing the padding solves the problem, but ...

Unable to adjust padding to 0 for a Material UI Button

I have a regular Material-UI button in my React application and I tried adding the following style to it: button: { display: 'inline-block', padding:0 } So, when I apply this style to the button using the className attribute like this: ...

HTML links remain enclosed in a box even after the code has been finalized

Hey there, I have written this code and for some reason, every link shared after it is displaying with a black background. I'm new to coding and can't seem to figure out what I'm doing wrong. Any help would be greatly appreciated. a:link, ...

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

Changing the position of a Bootstrap popover dynamically from top to bottom

Struggling with the bootstrap popover here. I can't seem to change the popover position from top to bottom when it reaches the top of the viewport. I attempted to use placement: 'auto bottom' but unfortunately, it didn't do the trick f ...

Customizing Material UI CSS in Angular: A Guide

Recently, while working with the Mat-grid-tile tag, I noticed that it utilizes a css class called .mat-grid-tile-content, which you can see below. The issue I am encountering is that the content within the mat-grid-tile tag appears centered, rather than f ...

Chrome displays radio buttons with a white background that is not intended. Firefox, on the other hand

The radio buttons in Google Chrome are displaying an unwanted white background around the circle, which is not the intended behavior as seen in Firefox. Please refer to these images for comparison. For a direct example of the issue on the page, please vi ...

How to responsively position and scale a background image for a full-width div

Here is the situation: I have an SVG with dimensions of 1920*1920 pixels () This is what I require: I intend to use it as a background-image for a div that spans the full width of the screen. The background image for the div must be responsive. An essent ...

What is the process for verifying the versions of packages installed in a React application that has been

We utilized react-boilerplate to develop our application, incorporating material-ui as the chosen UI library and hosting it on Azure IIS. Recently, with a new material-ui package release, some issues have arisen in the local environment but not in the dep ...

Modify th:hover in CSS to alter the background color

I currently have a table structured as follows: <table class="cedvel"> <caption> count: <%:Html.Encode(TempData["RowsCount"])%></caption> <thead> <tr&g ...

Customize Popover Color in SelectField Component

Looking to customize the SelectField's popover background color in material-ui. Is this possible? After exploring the generated theme, it seems that there is no option for configuring the selectField or popover. Attempted adjusting the menu's ba ...

Employing jQuery for adding a class to the initial TD in every table row

Currently, I am dealing with a table structured like this: <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent"> <tr class="tablerow"> <td>This is the td I want to add a class to.</td> <td c ...

Utilizing Material UI for autocomplete forms with dynamic label inputs

Is it possible to display both the title and year in the label of an autocomplete form? I encountered an issue with the standard Material UI demo when attempting to use an array to show both title and year in the form label. The error pertained to lowerca ...

What is preventing me from translating an element using transform: translateY()?

I attempted to move the letter 'h' down by 40px on its own, but the translation only seems to work if I relocate the entire word. I also experimented with positioning 'h' absolutely relative to the parent element, but then 'ello&ap ...

The enchanting world of CSS background scrolling animations

I am currently developing a website where I have split the hero/header into two sections - one on the left with information and the other on the right with a graphic. I wanted to add a simple parallax effect to the image on the right side, so I used backgr ...

The alignment of the submit button is consistently off when placed alongside two input boxes within a table

Completing this task is usually straightforward. I have a sign-in form with an email input, password input, and submit button aligned horizontally. However, after trying various styles and structures for hours, the button appears slightly lower than the in ...

Changing divider color in Material-UI with React

I need some assistance with changing the color of the divider component from the material ui framework. I have successfully changed colors for other components using the useStyles() method like this: const useStyles = makeStyles(theme => ({ textPad ...

The text field is being styled with inline styles automatically

I am trying to set a custom width for a textarea in my form, but the inline styles are automatically overriding my CSS. I have checked my scripts, but I am unsure which one is manipulating the DOM. If you have any insight, please let me know. This is the ...

Only one specific SVG tag is affected by the CSS stylesheet

I'm encountering an issue with my web page that has multiple SVG tags. Each SVG tag includes a block of style tags containing CSS styles for the respective SVG element. The problem is that these styles seem to leak into the overall global styles. For ...

Tips on modifying responsive design with jQuery

I have implemented a responsive design approach with the following CSS code: @media all and (max-width:799px){ .bigFont{ font-size: 28px; } } @media all and (min-width:800px){ .bigFont{ font-size: 48px; } } If I want to modify the font size using ...