Align Items Flexbox , React Material in React - css

I'm using flexbox, React Material in my React App and i wanted to align the searchbar and the clear button on the same line and with space on each other.
Pls see this link
CLICK HERE
<div className={classes.searchBarSection}>
<Paper component="form" className={classes.searchBar}>
<InputBase
className={classes.input}
placeholder="Search..."
inputProps={{ "aria-label": "search..." }}
/>
<IconButton
type="button"
className={classes.iconButton}
aria-label="search"
>
<SearchIcon />
</IconButton>
</Paper>
<Button variant="contained" size="small" color="primary">
Clear
</Button>
</div>

try this:
searchBarSection: {
display: "flex",
justifyContent: "space-between",
width: "300px"
},
adjust the width for how much space you want between them
this will look like:

add these styles, the width will give the searchBatSection breathing room to space the children. Experiment around with the justifyContent options to find your suit.
searchBarSection: {
width: "50%",
display: "flex",
justifyContent: "space-between"
},

Use grid
searchBarSection: {
display: 'grid',
gridTemplateColumns: '0.8fr 0.2fr',
gap: '12px',
},
https://codesandbox.io/s/material-demo-6w93i

Related

Aligning in CSS and Material UI in React

How do I align one button to the left while the other two on the center?
Pls check codesandbox here
CLICK HERE
<DialogActions sx={{ justifyContent: "center" }}>
<Button>Left</Button>
<Button onClick={handleClose}>Cancel center</Button>
<Button onClick={handleClose}>Subscribe center</Button>
</DialogActions>
You can achieve that using Grid. Check out below codes.
<DialogActions
sx={{
display: "grid",
gridTemplateColumns: "1fr repeat(2, auto) 1fr",
justifyContent: "center"
}}
>
<Button sx={{ marginRight: "auto" }}>Left</Button>
<Button onClick={handleClose}>Cancel center</Button>
<Button onClick={handleClose}>Subscribe center</Button>
</DialogActions>

I want to display a text with ellipsis inside a Card Component but this is not working

I have this component and I want to display the text with an ellipsis. I´ve tried different things but is not working. any Ideas are welcome!
<Card.Body style={{
display: "inline-block",
maxWidth: "100px",
maxHeight: "100px",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}} onClick={()=> UpdateDetails(player)}>
<Card.Title>{player.realName}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">
{player.playerName}
</Card.Subtitle>
<Card.Text>{player.asset}</Card.Text>
</Card.Body>
{/* <button onClick={()=> DeletePlayer(players.id)}>X</button> */}
</Card>
))}

Hiding addToCart Button and then displaying it at the center of the product when hover over

I have this add to cart button, that I can hide it, and I want to display it at the center of the product Image when hover over it.
Am using Nextjs with material ui element, I try to display it on the middle by assigning top: value, it didn't work I try margin bottom also it didn't work.
The problem is the addToCart Button don't even go up to the image part, it literaly stays within the card space below the image... here is the component
<Card className={classes.card}>
<CardActionArea className={classes.cardArea}>
<CardMedia className={classes.cardMedia}
component="img"
image={product.image}
title={product.name}
>
</CardMedia>
<CardContent className={classes.cardContent}>
<NextLink href={`/product/${product.slug}`} passHref>
<Link>
<Typography>{product.name}</Typography>
</Link>
</NextLink>
<Typography>${product.price}</Typography>
</CardContent>
</CardActionArea>
<CardActions>
</CardActions>
<div className={classes.addTocart}>
<Button
fullWidth
size="large"
variant="contained"
color="primary"
onClick={() => addToCartHandler(product)}>
Add to cart
</Button>
</div>
</Card>
Any Idea type of styles I can give to it using styles.js
below are my styles component
cardContent: {
display: 'flex',
justifyContent: "space-between"
},
addToCart: {
display: "flex",
justifyContent: "center",
alignItems: "center"
},
addToCartButton: {
justifyContent: "center",
position: 'absolute',
top: 80,
}
}));
Below is the current project am working on
Here is my current project image
And Here is the link to similar of what I want to do
In this link you will notice how the button add to cart get display when you hover over it...
Example of what I want to Imitate

In material ui, why isn't the "spacing" styling attribute doing anything when I try and space elements in my Box?

I'm using materialUI in my React 16.10 application. I have two buttons in a Box.
<Box
style={{
marginTop: "3rem",
marginLeft: "1rem",
display: "flex",
justifyContent: "center",
spacing: 1,
}}
>
<Button onClick={handleSave} variant="contained" color="primary">
SAVE
</Button>
<Button
onClick={toDetailsView}
startIcon={<CancelIcon />}
variant="contained"
color="primary"
>
CANCEL
</Button>
</Box>
Even though I'm using "spacing: 1", the buttons appear right next to each other so it is impossible to distinguish where one ends and the next begins ...
How do I add style so that there is a little bit of space in between the buttons?
Looking for easy fix ?
margin: 1rem;

How to center a button in Material-UI

I couldn't figure out how to center buttons in Material-UI. This is the code I have:
function BigCard(props) {
const { classes } = props;
return (
<div>
<Card className={classes.card}>
<CardContent>
<Typography variant="display1" className={classes.title}>
Start Funding!
</Typography>
</CardContent>
<CardActions >
<Button size="small" color="primary" className={classes.actions}>
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</div>
);
How can I center my button?
You can use Box element
<Box textAlign='center'>
<Button variant='contained'>
My button
</Button>
</Box>
you could use the override with classes in the material ui doc,
FIRST WAY
You can do something like :
//Add your justify css in your styles const
const styles = {
...
root: {
justifyContent: 'center'
}
};
And use the classes props to add this to your CardActions component :
<CardActions classes={{root: classes.root}}>
SECOND WAY (easier)
OR You can use the style directly on your component, but i advise you to train how to use classes if you're working alot with material-ui
Just do something like :
<CardActions style={{justifyContent: 'center'}}>
For use cases that avoid defining css
<Grid container justify="center">
{props.children}
</Grid>
Here is how I did it with Material - UI
import {Typography, Button} from '#material-ui/core';
<Typography align='center'>
<Button
color='primary'
size='large'
type='submit'
variant='contained'
>
Sign Up
</Button>
</Typography>
Quick and dirty:
<Button style={{margin: '0 auto', display: "flex"}}>
NEXT
</Button>
From MUI v5, you can apply the custom styles using the sx props. Because CardActions layout is already flex, you only need to set its justify-content to center.
Using sx instead of inline styles like the other answer help reduce the CSS specificity, and make it easier to override the CSS in the future
<CardActions sx={{ justifyContent: "center" }}>
<Button size="small">Learn More</Button>
</CardActions>
Live Demo
What I tried is below and is also shown in official videos of material ui on youtube.
<Grid item xs={12} sm={12} md={4} lg={4}
style={{
textAlign:'center' // this does the magic
}}
>
<Button>
NEXT
</Button>
</Grid>
Thanks and best wishes
You have to use two properties: display and justify-content
<CardActions display='flex' justifyContent='center'>
This will centre your button horizontally
<Button
size="medium"
variant="contained"
color="primary"
style={{
display: "flex",
flexDirection: "row",
justifyContent:"center",
backgroundColor: purple[500],
'&:hover': {
backgroundColor: purple[700],
},
}}
>
Get Hired
</Button>
The easiest way is to use a Box wrapping around the button
<Box
sx={{
display: 'flex',
flexDirection: { xs: 'column', md: 'row' },
alignItems: 'center',
justifyContent: 'center',
}}
>
<Button>Click me</Button>
</Box>
This may sound counter-intuitive but what worked for my case (slightly different than yours) was to use the fullWidth attribute in the Button element.
Here is my scenario (MUI v5):
<Grid container alignItems={'center'} >
<Grid item>
<Button fullWidth variant='contained' >Click Me!</Button>
</Grid>
</Grid>
wrap element in this center
.center{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
}

Resources