Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best way to specify "justify" for grid item in material-ui?

I have a simple Grid from the material-UI library. It looks like that:

<Grid container>
    <Grid item sm={6}>
        <SearchPanel/>
    </Grid>
    <Grid item sm={6}>
        <ItemStatusFilter/>
    </Grid>
</Grid>

I just can't understand how can I align the first grid item in the center, and the second, for example, on the right side?

UPD: I could do it with justify-content: flex-end!important in my CSS files, but I'm not sure that it's the best way.

like image 939
Roman Andreev Avatar asked Feb 02 '19 14:02

Roman Andreev


People also ask

How do you align grid items in material UI?

Material-UI Grid Align Right If you need to horizontally align using only CSS, remember that justify-content horizontally aligns using the following values: flex-start: horizontally aligns left. center: horizontally aligns center. flex-end: horizontally aligns right.

How do I center align grid items?

One of the easiest ways of centering the content of grid items is using Flexbox. Set the display to "grid" for the grid container, and "flex" for grid items. Then, add the align-items and justify-content properties, both with the "center" value, to grid items.

What is Xs in material UI grid?

A value given to a breakpoint applies to all the other breakpoints wider than it (unless overridden, as you can read later in this page). For example, xs={12} sizes a component to occupy the whole viewport width regardless of its size. The Auto-layout makes the items equitably share the available space.

Does material UI have a grid system?

The Material Design responsive layout grid adapts to screen size and orientation, ensuring consistency across layouts. The grid creates visual consistency between layouts while allowing flexibility across a wide variety of designs. Material Design's responsive UI is based on a 12-column grid layout.


1 Answers

Below is one way of doing this for v3 and v4 of Material-UI (v5 example further down).

import React from "react";
import ReactDOM from "react-dom";

import Grid from "@material-ui/core/Grid";

function App() {
  return (
    <Grid container>
      <Grid item xs={4}>
        {/* Intentionally Empty */}
      </Grid>
      <Grid container item xs={4} justify="center">
        <div>Search Panel</div>
      </Grid>
      <Grid container item xs={4} justify="flex-end">
        <div>Item Status Filter</div>
      </Grid>
    </Grid>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

This is the general idea of Duncan's comment. I have changed sm to xs just so I could verify the behavior on any size screen. In the end, Material-UI's Grid just adds some convenience around the CSS Flexbox model, so to know how to do it with Grid you need to understand how you would do it in CSS. The main thing Grid adds is the easy responsive aspects of controlling the 12-column grid differently for different screen sizes.

Edit y25382q6r1

Here's an equivalent example for v5 (justify prop renamed to justifyContent):

import React from "react";
import ReactDOM from "react-dom";

import Grid from "@mui/material/Grid";

function App() {
  return (
    <Grid container>
      <Grid item xs={4}>
        {/* Intentionally Empty */}
      </Grid>
      <Grid container item xs={4} justifyContent="center">
        <div>Search Panel</div>
      </Grid>
      <Grid container item xs={4} justifyContent="flex-end">
        <div>Item Status Filter</div>
      </Grid>
    </Grid>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit Grid justify

like image 95
Ryan Cogswell Avatar answered Oct 20 '22 13:10

Ryan Cogswell