React App Showing Scrollbar When Material UI AppBar is Present

I'm encountering a straightforward issue in my React application. How can I get rid of the scrollbar on my page? It seems like the problem arises from the height value I set to 100vh. However, upon removing the AppBar, the scrollbar disappears. Any suggestions on how to resolve this issue?

You can view my code on CodeSandbox by clicking here

<div>
  <AppBar position="static">
    <Toolbar>
      <Typography variant="h6">News</Typography>
    </Toolbar>
  </AppBar>
  <Grid container component="main" className={classes.root}>
    <CssBaseline />
    <Grid item xs={false} sm={4} md={7} className={classes.image} />
    <Grid item xs={12} sm={8} md={5} component={Paper} elevation={6} square>
      <div className={classes.paper}>
        <Avatar className={classes.avatar}>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
        <form className={classes.form} noValidate>
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            id="email"
            label="Email Address"
            name="email"
            autoComplete="email"
            autoFocus
          />
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            name="password"
            label="Password"
            type="password"
            id="password"
            autoComplete="current-password"
          />
          <FormControlLabel
            control={<Checkbox value="remember" color="primary" />}
            label="Remember me"
          />
          <Button
            type="submit"
            fullWidth
            variant="contained"
            color="primary"
            className={classes.submit}
          >
            Sign In
          </Button>
          <Grid container>
            <Grid item xs>
              <Link href="#" variant="body2">
                Forgot password?
              </Link>
            </Grid>
            <Grid item>
              <Link href="#" variant="body2">
                {"Don't have an account? Sign Up"}
              </Link>
            </Grid>
          </Grid>
          <Box mt={5}>
            <Copyright />
          </Box>
        </form>
      </div>
    </Grid>
  </Grid>
</div>

Answer №1

Your styling includes a height: 100vh for the main section. However, this causes the AppBar section to be positioned outside of the main section. To eliminate the scrollbar, remove the specific height assigned to the AppBar, which is currently set at 64px, when defining the height property for the main section:

height: calc(100vh - 64px);

Answer №2

There are a couple of methods to achieve this.
Firstly, you can set the position="fixed" property in the material-ui Appbar. This will keep the header fixed while allowing other elements to adjust accordingly. To maintain the same visual appearance, add padding equal to the height of the Appbar (typically around 64px).

Secondly, you can add paddingTop to the Grid container equal to the height of the Appbar, while keeping the position="static" property the same. Another approach is to subtract the appbar height from the total height using "calc(100vh - 64px)"



The key point here is to account for the appbar height when styling a static appbar within the Grid container.

Answer №3

If the AppBar height is not always 64px, it may be suitable for your specific case.

In my situation,

<AppBar position="sticky">

solved the problem.

If this solution does not work for you, there could be other styles causing the issue.

Just to provide more context, I also have the following styles applied:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body, #root {
    height: 100%;
    width: 100%;
}

The #root element is the main div with the id root, where React renders all components.

ReactDOM.createRoot(document.getElementById('root'));

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

A guide on utilizing the context API consumer within the getInitialProps function of Next.js

Is there a way to incorporate the context API within getInitialProps in order to manage the loading spinner while making requests? What is the best approach to invoke my context methods within getInitialProps function? ...

Steps to incorporate JSON data using the mapping feature of the Material-UI grid component

https://i.stack.imgur.com/tk3Hh.png I need help with adding JSON data to 4 ColorCards similar to the image provided. However, I am facing issues with rendering title`, `color`, and `rating. Please refer to the code snippet below: import ColorCard from &qu ...

Rebel Spirit on a Wordpress Platform

I've encountered a strange issue on my Wordpress blog. For some reason, the code "&nbsp" is being inserted right after the opening <body> tag. I have checked the header.php file and there is no instance of this code present there. It's ...

Why are static PropTypes used in ReactJS and do they offer any solutions or are they merely a recurring design choice?

While delving into the code base of a web application, I came across some static PropTypes that left me questioning their purpose and necessity. Here is a snippet of the code in question: static propTypes = { fetchCricketFantasyPlayers: PropTypes.fun ...

My React application is failing to launch in the browser. What steps should I take to troubleshoot and resolve

Initiating the development server... events.js:183 throw er; // Unhandled 'error' event ^ Error: watch /home/test/Documents/test/public ENOSPC at _errnoException (util.js:1022:11) at FSWatcher.start (fs.js:1382:19) ...

Using the options from the previous state of Material UI Autocomplete

I want to implement a data suggestion feature using the autocomplete component, which is based on 2 queries fetching data from GraphQL. const handleSearchChange = (evt, newInputValue) => { const { agentsEmail, prefix, ...

Ensure that the text in the table with a width of 100% remains stable

Currently, I am encountering an issue with the inside text moving within my application on a webpage. In my previous tables, I utilized the min-width option to prevent the text or td from shifting when resizing the website page. For example, when stretchin ...

Adjust the label width to ensure that it aligns correctly with the outlined input field

My current issue involves using a `TextField` with a lengthy label. Despite the label having enough space for the text, it tends to jump to the next line. Check out my codesandbox It seems like the automatic calculation is incorrect, so I am considering ...

React: Mandating the presence of a child component within a designated parent

Here is an example of my component structure: <Dialog id="_login" modal={true} onSubmit={() => console.log("x")} onCancel={() => console.log("C")} visible={true} > <DialogHead> Title Here </DialogHead> <Dialo ...

Issue arising from CSS and div elements

I am facing some challenges in making certain divs behave as desired. Take a look at the following references: Link 1: - contains two divs Link 2: - has only one div I am trying to utilize two divs (.content) without them being positioned incorrectly ...

A guide on accessing a JSON file in Django with React using i18next

Running React build folder within Django leads to an issue where WSGI server uploads translation.json as text/html instead of json. Is there a method to specify this file as json or instruct WSGI server/Django to ignore treating it as json? In i18n.js, ca ...

What is the comparison between actual pixels and text size in Internet Explorer?

Can you determine the actual font size in pixels corresponding to the following text size options in Internet Explorer? Largest Larger Medium Smaller Smallest In a web development project, I am looking to achieve a similar functionality to adjust the te ...

What is the best way to transform this unfamiliar CSS element into JavaScript code?

I'm facing an issue where I want to animate a CSS image based on time constraints, but since it's in CSS, I'm unable to achieve the desired effect. The animation in question is not the sun itself, but rather a yellowish half-circle animation ...

Issue with HTML CSS: There is a gap between the words "Get" and "started", causing "get" to appear on

Greetings, it appears I am encountering an issue with my website. When I refer to "Get Started," it displays as "GetStarted." However, when I insert a space between "Get" and "started," it separates onto two lines as "Get" on the first line and "started" o ...

issue encountered while attaching css file / css styles not rendering

Here is the css link I am using: <link rel="stylesheet" type="text/html" href="/public/assets/lib/css/style.css" /> Despite trying various solutions such as removing "rel="stylesheet"", changing to "text/html", checking paths, placing it in the pu ...

HTML: The art of aligning elements within a header

I've been working on creating my personal HTML record, but I'm facing one last challenge that I can't seem to overcome. I want to design my header like this: https://i.stack.imgur.com/FU8EJ.png With the following elements: TEXT SUMMARY wi ...

Ways to rotate SVG images exclusively upon clicking

After experimenting with rotating SVG images on my navbar buttons, I encountered an issue. Whenever I click one button, all the images rotate simultaneously. Additionally, clicking elsewhere does not reset the images to their original positions. This is t ...

Tips for utilizing ng class within a loop

Having some trouble with my template that loops through a JSON file using json server. The issue I'm facing is related to correctly applying ng class when clicking on icons. Currently, when I click on an icon, it adds a SCSS class but applies it to al ...

Is it possible to simultaneously run watchify and node-sass watch together?

Currently, I am utilizing npm scripts in conjunction with watchify and node-sass while incorporating the -w parameter. I am curious if it is feasible to execute both of these 'watch' commands simultaneously. Would this setup ensure that only sty ...

Incorporating new elements onto the page without causing the existing elements to re-render

My challenge involves working with a large array of objects, where each object corresponds to one element. To display these elements on the page, I utilize the map() function, but currently only the first 5 elements are being rendered initially. In order t ...