0

Is there some way to use the styled utility from mui to place 2 divs side by side?

None of the examples in the documentation cover this particular use case.

The answers here and here address this, but not within the context of mui / material ui.

I tried the following, but the Box elements end up on separate lines:

import React from 'react';
import Box from "@mui/material/Box";
import { styled } from "@mui/material/styles";
import { Container } from "@mui/material";

const Wrapper = styled(Box)({
  display: "grid",
});

const App = (props) => {
  return (
    <Container>
      <Wrapper>
        <Box>text here</Box>
        <Box>more text</Box>
      </Wrapper>
    </Container>
  )
}

export default App

How can I modify the above code to get both lines of text on the same line?

Woodchuck
  • 3,869
  • 2
  • 39
  • 70

1 Answers1

2

There are multiple ways. You can use Grid, Box, and Styled components.

Here are two examples using Grid and Box with styled components.

A working example on Codesandbox

import * as React from "react";
import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import { styled } from "@mui/material/styles";
import { Container } from "@mui/material";

const Wrapper = styled(Box)({
  display: "grid",
  gridTemplateColumns: "repeat(2, 1fr)",
  gridGap: 8,
  marginTop: 16
});

export default function App() {
  return (
    <Container>
      <Grid xs={12} sx={{ border: "1px solid #ccc" }} container>
        <Grid xs={6} sx={{ border: "1px solid #ccc" }} item>
          div one
        </Grid>
        <Grid xs={6} sx={{ border: "1px solid #ccc" }} item>
          div two
        </Grid>
      </Grid>
      <Wrapper>
        <Box>div one</Box>
        <Box>div two</Box>
      </Wrapper>
    </Container>
  );
}
Mehul Thakkar
  • 2,177
  • 13
  • 22
  • 1
    This is exactly what I needed. Someone please consider reopening this question now that I've added a minimal reproducible example. Thanks! – Woodchuck Nov 29 '22 at 16:29