how to turn label horizontal center in material UI in grid? - reactjs

I am trying to make my label horizontally center in material UI using GRID API.
could you please tell me how I achieve this
https://codesandbox.io/s/007k3v472w
<div className="search-container">
<Grid container direction="row" spacing={24}>
<Grid item xs={6} className="abc">
<label>h</label>
</Grid>
<Grid item xs={6} className="pqr">
<label>hnnn</label>
</Grid>
</Grid>
</div>
https://material-ui.com/api/grid/
I already use alignItems but it not work
Expected out h and hnnn should be horizally center of their width.
currently they are left aligned

All you have to do add container attribute to each of your grid item and apply alignItem to make it center.
Your code should look something like this,
<Grid container direction="row" spacing={24}>
<Grid item xs={6}
container
direction="column"
justify="center"
alignItems="center"
className="abc">
<label>h</label>
</Grid>
<Grid item xs={6}
container
direction="column"
justify="center"
alignItems="center"
className="pqr">
<label>hnnn</label>
</Grid>
</Grid>
Let me know if it help!

Where you have defined your class as abc remove item and replace it with container and add justify="center"
To do it with CSS use text-align: center

Related

MaterialUI - Indepdendent Grids next to each other?

I have a Grid container and two "sub-containers" each taking half the space of the parent grid. I want to stack items of different heights on each of the sub-containers, but I'm having a problem with one side's height affecting the other.
The code is extremely simple:
<Box sx={{ flexGrow: 1 }}>
<Grid container spacing={3}>
<Grid item container xs={6}>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
<Grid item xs={12}>
<Item>Short items</Item>
</Grid>
</Grid>
<Grid item container xs={6}>
<Grid item xs={6}>
<Item style={{ height: "450px" }}>Tall item</Item>
</Grid>
</Grid>
</Grid>
</Box>
And also a Codesandbox forked from the documentation's. The result I want is the items on the left stacking right on top of each other.
You can add alignContent="baseline" to your left Grid container like:
<Grid item container xs={6} alignContent="baseline">
You can take a look at this sandbox for a live working example of this usage.

how to use grid material ui?

I have a component whose grid looks like this:
component Region:
<Grid container spacing={10} >
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
<Grid item xs={4} ></Grid>
</Grid>
And I use this component in another component, which also has a series of grids..component address:
<Grid container spacing={10}>
<Grid item>
<form >
<Region></Region>
<Grid container spacing={10}>
<Grid item xs={4} ></Grid>
</Grid>
<form >
</Grid>
</Grid>
What I want is for this gride to be a continuation of the other grids, but for me it goes to the next line.
What should I do to get the grid inside the address component in the following component region?
You need to have all grid items in the same grid container. So:
remove the top container and item, because you only got one item and it doesn't make sense.
remove the container from Region component and use a Fragment
remove the container from Address
Region:
<>
<Grid item xs={4}>1</Grid>
<Grid item xs={4}>2</Grid>
<Grid item xs={4}>3</Grid>
<Grid item xs={4}>4</Grid>
<Grid item xs={4}>5</Grid>
</>
Form:
<form>
<Grid container spacing={10}>
<Region />
<Grid item xs={4} >6</Grid>
</Grid>
</form>

Material UI Card alignment listing

I'm using Material UI to create the cards with grid container.
But Grid item will have same height for each row, how can I make the bottom card close to the top one when having white space? and the last card I wish to stick to the left instead of middle.
See the image below:
This is the look and feel I wish to achieve.
My example code:
codesandbox
Is it achievable by using Material Grid container or something else? or I have to create my own css? Please guide me. Thanks
I am able to achieve what I am try to by using flex-direction=column.
But I need split my data into few column, is it worth to do it?
Any other solution?
<Grid container spacing={3} className={classes.container}>
<Grid item xs={6} md={4} lg={3}>
<Grid container direction='column'>
<Grid item>
<Card></Card
</Grid>
</Grid>
</Grid>
</Grid>
<Grid item xs={6} md={4} lg={3}>
<Grid container direction='column'>
<Grid item>
<Card></Card
</Grid>
</Grid>
</Grid>
</Grid>

Better way to position elements in a grid

I try to have a header on my application, where the left item (A) is aligned to the left side of the header, the second item (B) is in the center of the header and the third item is aligned to the right.
I tried a lot and found out that only this worked for me
const AppHeader = () => {
return(
<Grid container
alignItems="baseline"
>
<Grid item xs={4}>
<Grid container justify="flex-start">
A
</Grid>
</Grid>
<Grid item xs={4}>
<Grid container justify="center">
B
</Grid>
</Grid>
<Grid item xs={4}>
<Grid container justify="flex-end">
C
</Grid>
</Grid>
</Grid>
)
}
I think this is a ugly solution, because putting a container inside an item seems so unnecessary to me. Especially if we consider that this container has no grid items. I would really like to tell the item to align its content to a position. It would be better if this solution could be more like his:
const AppHeader = () => {
return(
<Grid container
alignItems="baseline"
>
<Grid item xs={4} justify="flex-start">
A
</Grid>
<Grid item xs={4} justify="center">
B
</Grid>
<Grid item xs={4} justify="flex-end">
C
</Grid>
</Grid>
)
}
Is there a way to do something similar like that?
Assuming you are using material-ui Grid component. You can do something like this
<Grid container direction="row" justify="space-between" alignItems="center">
<div>A</div>
<div>B</div>
<div>C</div>
</Grid>
CodeSandbox Link: https://codesandbox.io/s/small-haze-1wvtr

Incompatibility of Material UI Grid in IE 11

I'm trying to achieve the following grid layout using the Grid component in Material UI.
I was able to achieve this layout in Chrome and Firefox using the following sample code, however in IE 11 all the grid items overlap and the textfields get expanded.
<Grid container direction='column' spacing={0}> // Grid 1 (Level0)
<Grid item xs={12}> // Grid 1 (Level1)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
Logo
</Grid>
<Grid item xs={6}>
FormControl
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 2 (Level1)
<Grid container direction='column' spacing={0}>
<Grid item xs={12}> // Grid 1 (Level2)
<Grid container direction='row' spacing={0}>
<Grid item xs={12}>
<TextField/>
</Grid>
<Grid item xs={12}>
<TextField/>
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 2 (Level2)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
<TextField />
</Grid>
<Grid item xs={6}>
<TextField />
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
<Grid item xs={12}> // Grid 3 (Level1)
<Grid container direction='row' spacing={0}>
<Grid item xs={6}>
Label
</Grid>
<Grid item xs={6}>
Label
</Grid>
</Grid>
</Grid>
</Grid>
Please let me know where am I going wrong!
According to the Material UI documentation, Material UI supports IE 11.
However, the Grid formatting issues as you describe them are a documented problem with a fix. The issues are a result of a Grid item not having the flex size set. In Chrome you can just use <Grid item>, but for IE you need to explicitly set the flex size <Grid item xs=12>.
How does this relate to you?
In your case it looks like your child <Grid container> components should also be <Grid item> components. That is, they should be of the form <Grid container item>, but they should also have the flex size set <Grid container item xs=12>.
Below is a sample snippet:
<Grid container>
<Grid item xs={12}>
<Grid container item xs={12}>
<Grid item xs={6}>
Logo
</Grid>
<Grid item xs={6}>
FormControl
</Grid>
</Grid>
</Grid>
</Grid>

Resources