Achieve full height in Grid component of material UI by eliminating margins

In this codesandbox example, I have successfully set the grid container height to full using 100vh. However, there is an issue with a margin of 8px being added by the user agent to the body element, and I'm struggling to find a solution to remove it.

<Box sx={{ flexGrow: 1 }}>
  <Grid container spacing={2} sx={{ height: "100vh" }}>
    <Grid item xs={4} sx={{ backgroundColor: "red" }} />
    <Grid item xs={8} sx={{ backgroundColor: "blue" }} />
  </Grid>
</Box>

Does anyone have any suggestions or solutions for this issue?

Answer №1

Don't forget to include <CssBaseLine /> to reset default margins and paddings.

import { CssBaseline } from "@mui/material";

ReactDOM.render(
  <StyledEngineProvider injectFirst>
    <CssBaseline/> 
    <Demo />
  </StyledEngineProvider>,
  document.querySelector("#root")
);

In order to adjust the spacing in Material-UI's Grid, negative margins are used. This is why the parent of the Grid should have padding that will correct this negative margin.

<Box paddingTop={2} sx={{ flexGrow: 1 }}>
  <Grid container spacing={2} sx={{ height: "100vh" }}>
    <Grid item xs={4} sx={{ backgroundColor: "red" }} />
    <Grid item xs={8} sx={{ backgroundColor: "blue" }} />
  </Grid>
</Box>

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

Detect when a user's mouse is hovering over an iframe and escape the iframe accordingly

When the iframe window is visible, there are no issues. However, if the iframe window is set to 0px X 0px in order to hide it while still loading, consider redirecting the iframe or not displaying it at all. In case something is loaded within an iframe or ...

Can a rotationX be applied within an existing rotationX?

I have a collection of 3 divs labeled a, b and c inside a container. The container is rotatedX by 90 degrees, giving the appearance that it is "lying on its back". Now, I am trying to rotate the a, b, and c divs so they appear to be "standing up" again. Ho ...

Material-UI has officially deprecated the `css` function and is now recommending the use of the `styleFunctionSx` instead

I developed a component library called dot-components that is built on top of mui-org/material-ui. In another application utilizing my dot-components library, I have observed a console warning popping up during unit tests. Console Warning console.warn ...

Arrows indicating upward movement on decimal increments within Textfield Material UI

Utilizing the OutlinedInput component from Material UI to enable users to input a number. When the input's type is set to number, the browser automatically includes two small arrows in the input field, which is working as expected. However, these arr ...

line of checkboxes aligned horizontally with the help of Bootstrap

My goal is to create a horizontal row of checkboxes within a form. I've been able to achieve a functional but unattractive version by using the following code: div.form-group(ng-show = 'avariable') label 1 input(type = 'checkbo ...

transforming a table into a stylized CSS design

Seeking assistance in converting a table into CSS as I am new to CSS: <table dir="ltr" width="500" border="0" align="center"> <caption><font face="Helvetica">Movies</font></caption> <colgroup width="50%" /> ...

Styling the <Autocomplete/> component in Material UI with React to achieve rounded corners

Google's search bar has always been a favorite of mine, with its rounded corners and generous text padding. https://i.stack.imgur.com/UbKrr.png I'm attempting to replicate this aesthetic using Material UI's <Autocomplete/> component ...

What is the method for changing the color of the bottom border from blue to green in a select field with React JS?

I am currently utilizing the select field component from material UI react Upon selecting an item from the field, I encounter two specific issues: The border bottom line turns blue and the background color changes to gray. My objective is to modify the ...

Styling the background of a container in CSS using Bootstrap

Currently, I am utilizing bootstrap and trying to find an elegant solution for adding a background to my container. Specifically, I am working with the bootstrap class container, as opposed to container-fluid. The official Bootstrap documentation advises ...

How can I specifically target and count children from a certain class using CSS selectors?

Consider this HTML structure with items arranged at the same level: <div class="h2">Title: Colors</div> <div class="pair">Hello world (1)</div> <div class="pair">Hello world (2)</div> <div class="pair">Hello wo ...

Tips for avoiding unnecessary re-renders while maintaining state when using Material UI Autocomplete with numerous choices

I'm currently utilizing Material UI's Autocomplete component with the ability to select multiple options. The challenge I'm facing is updating the state each time a value changes when multiple options are selected. The onChange prop is being ...

Tips for implementing an image as a background in AngularJS

Struggling with a Dilemma: This issue has been driving me insane for the past three days... I am eager to utilize an angularJS variable as a background image without relying on a directive. My objective is to load images of any shape (square, rectangle, ...

Height of the Accordion List

I have a code snippet below for an accordion list that I put together. I initially set the height for all the heading boxes to be uniform, but it seems like box A is displaying larger than the others. Can you help me figure out what went wrong? Any suggest ...

Adding style using CSS to a dynamically generated table row in Vue.js

Currently, I am working with a table that dynamically generates rows using v-for: <table> <tr><th class='name'>Name</th><th>Surname</th></tr> <tr v-for='data in datas'><td class=&a ...

Tips for adjusting the height and border of Bootstrap tabs

I'm facing an issue with the modal and nav-tabs I created. There are a couple of problems that need to be addressed. Firstly, the tabs have excessive height and I've tried adjusting it using CSS height property, but it's not working as e ...

Understanding the concept of for loops in JavaScript and incorporating them in CSS styling

Hello there! I initially used this code to draw 12 lines, but now I am looking to incorporate a for loop and implement it in CSS. Can someone please guide me on how to proceed? <!DOCTYPE html> <html> <head> <style> #straigh ...

The drop-down menu fails to appear when I move my cursor over it

#menu { overflow: hidden; background: #202020; } #menu ul { margin: 0px 0px 0px 0px; padding: 0px 0px; list-style: none; line-height: normal; text-align: center; } #menu li { display: inline-block; } #menu a { display: block; position: relative; padding ...

The Flutter image blurs into a striking combination of red and black as the screen dimensions are altered

https://i.stack.imgur.com/YeZa6.png Hello everyone, we are facing an issue with Flutter web. Here's what's happening: When we use flutter run -d chrome --web-renderer canvaskit and flutter run -d chrome --web-renderer html, nothing changes. If ...

Can we make one tab disappear when another tab is clicked in Ionic 2 app menu icon?

I am working on a project using Ionic 2 and I have implemented multiple tabs in the application. However, I need to find a way to hide the top tab when the user clicks on the bottom tabs menu icon. Here is my Plunker for your reference. My goal is to ma ...

Exploring the functionality of closing Material UI Drawer on escape key in a React 16 app with RTL support

I am currently experimenting with the Material UI Drawer component. I expected it to close when pressing the Esc key or clicking outside of it, but unfortunately, it is not behaving as anticipated. I am utilizing the react testing library for my tests an ...