最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Material UI - How to stretch Grid container to parent height and width - Stack Overflow

programmeradmin2浏览0评论

Problem

I cannot work how to get a Grid container element to stretch to the height and width of it's parent element without modifying the height and width using the style prop or editing the style with the makeStyles/useStyles hook.

I am sure there has to be a simpler way. I have created a sandbox with what I would like (written with vanilla React) and what I have so far (written with Material UI ponents).

The reason I am not just using regular Box ponents is due to of the lack responsive controls it has in parison to Grid.

Example

Codesandbox

Problem

I cannot work how to get a Grid container element to stretch to the height and width of it's parent element without modifying the height and width using the style prop or editing the style with the makeStyles/useStyles hook.

I am sure there has to be a simpler way. I have created a sandbox with what I would like (written with vanilla React) and what I have so far (written with Material UI ponents).

The reason I am not just using regular Box ponents is due to of the lack responsive controls it has in parison to Grid.

Example

Codesandbox

Share Improve this question asked Aug 11, 2020 at 7:16 Alex MckayAlex Mckay 3,70619 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I was overplicating things and under appreciating the power of Box.

Box is responsive. My takeaway is to use Grid sparingly on items that have heights and widths and use Box in bination with the Hidden for screen layouts.

import React from "react";
import { Box } from "@material-ui/core";

export default function Layout() {
  return (
    <Box bgcolor="green" display="flex" height="100vh" width="100vw">
      <Box bgcolor="red" flex={{ xs: 1, sm: 2 }} />
      <Box
        bgcolor="yellow"
        display="flex"
        flex={1}
        flexDirection={{ xs: "column", sm: "row" }}
      >
        <Box bgcolor="blue" flex={{ xs: 1, sm: 6 }} />
        <Box bgcolor="purple" flex={{ xs: 11, sm: 6 }} />
      </Box>
    </Box>
  );
}
发布评论

评论列表(0)

  1. 暂无评论