cards in ant design for are floating over fixed nav bar - css

I made a single page app in react using ant design. I made the navbar on top fixed, now when scrolling the cards in the content are floating over the navbar.
Top menu/navbar code
import React,{Component} from 'react'
import {Layout, Menu, Row, Col, Icon} from 'antd'
import Logo from './Glogo.png'
const {Header}=Layout
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
export default class TopMenu extends Component{
render(){
return(
<Header style={{position: 'fixed', width: '100%' }}>
<img src={Logo} style={{height:"40px", width:"140px",
float:"left", marginTop:"10px"}}alt="Logo"/>
<Menu
theme="dark"
mode="horizontal"
style={{ lineHeight: '64px' }}
>
</Menu>
</Header>
)
}
}
In header i declared the position as fixed.
Content code:
import React, {Component} from 'react';
import {Layout, Card, Row, Col, List} from 'antd';
const {Content} = Layout;
class Dashboard extends Component{
constructor(props){
super();
this.state={
session_id:[
{
event:"underline",
timestamp:"10:30",
observation:["magnetic effect","flemings right hand rule","magnetic effect","flemings right hand rule","magnetic effect","flemings right hand rule","magnetic effect","flemings right hand rule","magnetic effect","flemings right hand rule"],
suggestions:["take a look at this concept", " watch this video"]
},
{
event:"underline",
time:"10:30",
observation:["magnetic effect","flemings right hand rule"],
suggestions:["take a look at this concept", " watch this video"]
},
{
event:"underline",
time:"10:30",
observation:["magnetic effect","flemings right hand rule"],
suggestions:["take a look at this concept", " watch this video"]
},
]
}
}
render(){
return(
<Content style={{ padding: '0 50px', marginTop: 64, minHeight: "calc(100vh - 128px)" }}>
<div style={{ background: '#fff', padding: 24, minHeight: 500, marginBottom:"30px" }}>
{this.state.session_id.map((val, i)=>
<Card key={i}hoverable style={{margin:"15px", boxShadow:"0 0 10px gray "}}>
<Row gutter={16}>
<Col xs={24} sm={24} md={8} lg={8} >
<Card title="Event" hoverable bordered={false}>
Name: {val.event}<br/>Time: {val.time}
</Card>
</Col>
<Col xs={24} sm={24} md={8} lg={8} >
<Card title="Observation" hoverable bordered={false}>
<List
size="small"
bordered
dataSource={val.observation}
renderItem={item => (<List.Item>{item}</List.Item>)}
/>
</Card>
</Col>
<Col xs={24} sm={24} md={8} lg={8} >
<Card title="Suggestions" hoverable bordered={false}>
<List
size="small"
bordered
dataSource={val.suggestions}
renderItem={item => (<List.Item>{item}</List.Item>)}
/>
</Card>
</Col>
</Row>
</Card>)}
</div>
</Content>
)
}
}
export default Dashboard;
The cards in the content are floating over the fixed header while the content itself is below the header.
I want the navbar to be on top of everthing else. What can i do.

May z-index will help you in your case.
<Header style={{position: 'fixed', width: '100%', zIndex: 100}}>

Related

Two components overlap each other when screen size is reduced

My site has a component (NavPanel) that consists of two components (a back button (BackToButton ) and a search field (SearchTextField )). With a standard screen size, they are positioned as they should be, but if the screen sizes are reduced, then these two components overlap each other.
The most optimal for me would be if, with a compressed screen size, the second component (SearchTextField ) will be located under the first (BackToButton ). Tell me how you can solve this problem?
const Style = {
paddingLeft: '8%',
paddingRight: '8%',
minHeight: '70px',
alignItems: 'center',
flexWrap: 'nowrap',
whiteSpace: 'nowrap'
}
export default function NavPanel() {
return (
<Grid container sx={Style}>
<BackToButton />
<SearchTextField />
</Grid>
);
}
Here you go...
You didn't use the grid correctly.
See the forked snippet here.
EDIT 1
A:
B:
EDIT 2
You can change the breakpoint from sm to md. It means that PageNameBackToButton and FilterButtonSearchTextField will be stacked earlier, but A will not happen.
See the forked snippet here.
PageNameBackToButton.jsx
import React from "react";
import { Tooltip } from "#mui/material";
import BackToButton from "./BackToButton";
const PageName = {
color: "#000000",
fontSize: "20px",
fontWeight: "700",
letterSpacing: "0.2px",
paddingRight: "20px"
};
const PageNameBackToButtonContainerStyle = {
width: "50%",
justifyContent: "start",
alignContent: "center"
};
export default function PageNameBackToButton(props) {
return (
<div sx={PageNameBackToButtonContainerStyle}>
<BackToButton />
<div style={{ marginTop: "5px", display: "inline-block" }}>
<span style={PageName}>Some Text</span>
<span>
<Tooltip title={`Here long long text`} placement="right">
<span
style={{ fontSize: "18px", color: "#ef1400", userSelect: "none" }}
>
Live
</span>
</Tooltip>
</span>
</div>
</div>
);
}
FilterButtonSearchTextField.jsx
import { TextField } from "#mui/material";
const FilterButtonSearchTextFieldContainerStyle = {};
const SearchTextField = {};
export default function FilterButtonSearchTextField() {
return (
<div sx={FilterButtonSearchTextFieldContainerStyle}>
<TextField
required
label="Search Search"
size="small"
style={SearchTextField}
/>
</div>
);
}
Filter.jsx
import React from "react";
import { Grid } from "#mui/material";
import FilterButtonSearchTextField from "./FilterButtonSearchTextField";
import PageNameBackToButton from "./PageNameBackToButton";
import { styled } from "#mui/material/styles";
const FilterContainerStyle = {};
const Root = styled("div")(({ theme }) => ({
padding: theme.spacing(1),
[theme.breakpoints.up("md")]: {
display: "flex",
justifyContent: "flex-end"
}
}));
export default function Filter(props) {
const pageName = props.pageName !== undefined ? props.pageName : "";
const showBackToButton =
props.showBackToButton !== undefined ? props.showBackToButton : false;
return (
<Grid container spacing={2} sx={FilterContainerStyle}>
<Grid item md={6} xs={12}>
<PageNameBackToButton
showBackToButton={showBackToButton}
pageName={pageName}
/>
</Grid>
<Grid item md={6} xs={12}>
<Root>
<FilterButtonSearchTextField table={props.table} />
</Root>
</Grid>
</Grid>
);
}
<Grid container> is meaningless without some <Grid item>s inside it.
Just like the Bootstrap,
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.
Your code would be like this:
<Grid container spacing={1} sx={Style}>
<Grid item xs={12} sm={6}>
<BackToButton />
</Grid>
<Grid item xs={12} sm={6}>
<SearchTextField />
</Grid>
</Grid>
I strongly recommend reading the MUI Grid Docs.

How to move Icons in Material UI

How can I move my components on the image by using material UI ? I want to move my heart component to the top right corner of my image and ratings on the bottom of my image.
import image from "../../Assets/pic.jpg";
import React, { useState } from "react";
import { makeStyles } from "#material-ui/core/styles";
import Card from "#material-ui/core/Card";
import CardActionArea from "#material-ui/core/CardActionArea";
import FavoriteBorderIcon from "#material-ui/icons/FavoriteBorder";
import CardContent from "#material-ui/core/CardContent";
import CardMedia from "#material-ui/core/CardMedia";
import Typography from "#material-ui/core/Typography";
import Grid from "#material-ui/core/Grid";
import styles from "./Cards.module.css";
import Stars from "../Stars/Stars";
const useStyles = makeStyles({
root: {
maxWidth: 345,
display: "flex",
},
text: {
textAlign: "center",
color: "#333333",
},
textCardBottom: {
display: "flex",
justifyContent: "center",
},
textPrice: { color: "#333333" },
textStrike: { margin: "0px 10px 0px 10px" },
textDiscount: { color: "#ff6a6a" },
stars: {
right: 9,
},
ratings: {},
});
const Cards = () => {
const [showComponent, setShowComponent] = useState(false);
const classes = useStyles();
const handleToggleHoverIn = (event) => {
event.preventDefault();
setShowComponent(true);
};
const handleToggleHoverOut = (event) => {
event.preventDefault();
setShowComponent(false);
};
console.log("The state showComponent value is ", showComponent);
return (
<Card
onMouseEnter={handleToggleHoverIn}
onMouseLeave={handleToggleHoverOut}
className={classes.root}
>
<CardActionArea>
<CardMedia
component="img"
alt=""
image={image}
title="Contemplative Reptile"
/>
{/* {showComponent ? ( */}
<Grid container>
<Stars right="40%" /> // want this rating component on the centre of my image
<FavoriteBorderIcon fontSize="large" /> // want this heart to the top right corner
</Grid>
{/* ) : null} */}
<CardContent>
<Typography
gutterBottom
variant="h5"
component="h2"
className={classes.text}
>
Printed round Neck
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="div"
className={classes.textCardBottom}
>
<Typography
variant="body2"
color="textSecondary"
component="b"
className={classes.textPrice}
>
Rs. 454
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="strike"
className={classes.textStrike}
>
Rs. 699
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="span"
className={classes.textDiscount}
>
(35 % off)
</Typography>
{/* <p>
<b>Rs. 454</b>
<strike>Rs. 699</strike>
<span style={{ color: "#FF7F7F" }}> (35 % off) </span>
</p> */}
</Typography>
</CardContent>
</CardActionArea>
</Card>
);
};
export default Cards;
I tried to use the position property of material UI but I wasn't able to move the components. I tried the Material UI docs but couldn't find properties that could help me moving the components.
did you tried to add inline styles like this:
<FavoriteBorderIcon fontSize="large" style={{ position: "absolute", top: "5px", right: "5px" }}/>
<Stars right="40%" style={{ position: "absolute", bottom: "5px" }}/>
and perhaps you need also add some position to Grid as well
<Grid style={{ position: "relative" }}>

Align Image and Text in Horizontal Direction in React

I'm trying to achieve like this in the picture below. Right now, my image is on the top while the text is below. I wanted to achieve like the text is just on the right side of the image.
Pls check this codesandbox link CLICK HERE
CODE
const drawer = (
<div>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.headerIcon}>
<AccountCircleIcon fontSize="large" />
</div>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
<Divider />
</div>
);
Adding display: "flex" to children doesn't really do much. What I did was I added a wrapper class around you icon, name and role, with the display: "flex", flexDirection:"row" justifyContent: "center" and alignItems:"center" properties. What the wrapper then does is that it puts all the divs "under" it in a row, like:
<div className="classes.wrapper">
<div>This one is to the left</div>
<div>This one is to the right</div>
</div>
However, if I for example put another 2 divs under the one to the right, they will stack on top of each other, because the flexDirection property is set to row for all children under the wrapper, but not for their children.
<div className="classes.wrapper">
<div>This one is to the left</div>
<div>
<div>This one will be to the right on top</div>
<div>This one will be to the right under</div>
</div>
</div>
I also removed some other stuff, but here's the code:
import React from "react";
import { makeStyles } from "#material-ui/styles";
import Divider from "#material-ui/core/Divider";
import Drawer from "#material-ui/core/Drawer";
import Hidden from "#material-ui/core/Hidden";
import AccountCircleIcon from "#material-ui/icons/AccountCircle";
import "./styles.css";
const useStyles = makeStyles(theme => ({
wrapper: {
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
margin: "0.5rem"
},
innerWrapper: {
display: "flex",
flexDirection: "column",
alignItems: "baseline",
marginLeft: "0.5rem"
},
headerTitle: {
fontSize: "1.3rem",
cursor: "pointer"
},
headerName: {
margin: "0",
fontStyle: "bold",
fontSize: "1rem"
},
headerRole: {
margin: "0",
fontSize: "0.7rem"
},
iconButtons: {
marginLeft: "auto"
}
}));
export default function LoginForm() {
const classes = useStyles();
const drawer = (
<>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.wrapper}>
{" "}
<div className={classes.headerIcon}>
{" "}
<AccountCircleIcon fontSize="large" />
</div>
<div className={classes.innerWrapper}>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
</div>
<Divider />
</div>
</>
);
return (
<nav className={classes.drawer}>
<Hidden lgUp implementation="css">
<Drawer
variant="temporary"
anchor={"left"}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
);
}
For more information on how to use flexbox in CSS, check out this guide.
Here is how I have done it. I have adjusted the text on the right side of the icon.
You can do further styling:
import React from "react";
import { makeStyles } from "#material-ui/styles";
import Divider from "#material-ui/core/Divider";
import Drawer from "#material-ui/core/Drawer";
import Hidden from "#material-ui/core/Hidden";
import AccountCircleIcon from "#material-ui/icons/AccountCircle";
import "./styles.css";
const headerStyles = {
display: "flex",
justifyContent: "center"
};
const useStyles = makeStyles(theme => ({
root: {
display: "flex"
},
headerTitle: {
...headerStyles,
fontSize: "1.3rem",
cursor: "pointer"
},
headerIcon: {
...headerStyles,
marginTop: "1rem"
},
headerName: {
...headerStyles,
marginTop: "0.2rem"
},
headerRole: {
...headerStyles,
marginTop: "-0.8rem",
marginBottom: "1rem"
},
iconButtons: {
marginLeft: "auto"
},
userName: {
display: "flex",
flexDirection: "row"
}
}));
export default function LoginForm() {
const classes = useStyles();
const drawer = (
<div>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.userName}>
<div className={classes.headerIcon}>
<AccountCircleIcon fontSize="large" />
</div>
<div>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
</div>
</div>
<Divider />
</div>
);
return (
<nav className={classes.drawer}>
<Hidden lgUp implementation="css">
<Drawer
variant="temporary"
anchor={"left"}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
);
}

Material-ui Grid: Grid item's width too big and going outside of Grid container

I am trying to set up a simple layout for a SPA using Material-ui and React. My idea is to have a left hand column as a sidebar and a right-hand main area to render information etc. However, in my current set-up I have two issues:
The <Grid item> and its container <Button> elements extend beyond the left sidebard <Grid container item xs={3} className={classes.sideBarGrid}> into the right hand column. I am not sure what I am doing wrong and any help would be greatly appreciated!
Code Sandbox
Also, I cannot get the right hand grid column <Grid container item xs={9} className={classes.labelGrid}> get to work to be full width, even though I set it to width: "100%".
Code:
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import CssBaseline from "#material-ui/core/CssBaseline";
import Typography from "#material-ui/core/Typography";
import Grid from "#material-ui/core/Grid";
import Button from "#material-ui/core/Button";
import TextField from "#material-ui/core/TextField";
const useStyles = makeStyles(theme => ({
mainContainer: {
width: "100vw",
height: "100vh"
},
labelGrid: {
flexGrow: 1,
flexDirection: "column",
backgroundColor: "#EBEDF0",
alignItems: "center",
justifyContent: "center",
width: "100%"
},
sideBarGrid: {
maxWidth: 300,
flexDirection: "column",
justifyContent: "space-between"
},
avatar: {
margin: theme.spacing(1),
backgroundColor: theme.palette.secondary.main
},
labelarea: {
margin: theme.spacing(1)
},
imagearea: {
minHeight: 200
},
classButton: {
margin: theme.spacing(1)
},
submit: {
margin: theme.spacing(1)
},
commentField: {
margin: theme.spacing(2, 2, 3)
}
}));
export default function Labelscreen(props) {
const classes = useStyles();
// history for react router
// array with potential classes for image
const buttonText = ["one", "two"];
// function to filter list of labels by property and see if object property is null
return (
<Grid container className={classes.mainContainer}>
<CssBaseline />
<Grid container item xs={3} className={classes.sideBarGrid}>
<Grid item>
{buttonText.map((item, key) => (
<Button
className={classes.classButton}
variant="outlined"
color="primary"
fullWidth
>
{item} ({key + 1})
</Button>
))}
<TextField
id="imageComment"
label="Comment"
placeholder="please put comments here"
multiline
fullWidth
variant="outlined"
value="adfljdaf"
/>
</Grid>
<Grid item>
<Button
type="submit"
fullWidth
variant="contained"
color="secondary"
className={classes.submit}
>
Go back
</Button>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Next
</Button>
</Grid>
</Grid>
<Grid container item xs={9} className={classes.labelGrid}>
<Typography component="h1" variant="h5">
Something
</Typography>
</Grid>
</Grid>
);
}
EDIT
The gray area on the right hand does not fill the whole screen when the screen size is large, even though the width is set to 100% in the labelGrid
Your buttons have margin (right & left), so they move beyond your left sidebar.
You can fix this using:
classButton: {
margin: theme.spacing(1, 0)
},
submit: {
margin: theme.spacing(1, 0)
},
To add back the space on the left&right side you can add padding on the container:
sideBarGrid: {
maxWidth: 300,
flexDirection: "column",
justifyContent: "space-between",
padding: theme.spacing(0, 1)
},

AppBar overlaps with other elements

I am starting to use React/Material-UI, and also new to CSS etc...
I have a simple page layout with an APPBar. Unfortunately this AppBar overlaps the elements which are meant to go below it.
I have found this answer:
AppBar Material UI questions
But this feels completely wrong. What if my AppBar has a variable height, depending on the icons, display modes etc...?
I have tried to create a vertical grid, to wrap the elements in different items, made the top container a flex one and play with flex settings, nothing seems to work, the app bar always sits on top of the text.
The code is very simple:
import React from 'react';
import { AppBar, Typography, Box } from '#material-ui/core';
function App() {
return (
<div>
<AppBar>
<Typography variant='h3'>
AppBar
</Typography>
</AppBar>
<Box>
<Typography variant='h1' style={{ border: '1px solid black' }}>
Hello
</Typography>
</Box>
</div>
)
}
export default App;
The "Hello" text chunk is only half visible:
This is happening because the MaterialUI App Bar defaults to position="fixed". This separates it from the standard DOM's layout to allow content to scroll beneath it, but as a result no space is made for it on the page.
You can get around this by wrapping all content below it in a div and specifying enough margin, or by changing the position property of <AppBar> so it's no longer "fixed". In your example, you could also just apply the styles to <Box> if that's the only content below the <AppBar>.
e.g.
import React from 'react';
import { AppBar, Typography, Box } from '#material-ui/core';
function App() {
return (
<div>
<AppBar>
<Typography variant='h3'>
AppBar
</Typography>
</AppBar>
<div style={{marginTop: 80}}>
<Box>
<Typography variant='h1' style={{ border: '1px solid black' }}>
Hello
</Typography>
</Box>
</div>
</div>
)
}
export default App;
MaterialUI provides a theme mixin for the AppBar that can help. Not sure if you're using the recomended JSS setup, but you can do something like this:
import withStyles from '#material-ui/core/styles/withStyles';
const styles = theme => ({
appBarSpacer: theme.mixins.toolbar
});
const style = withStyles(styles)
function MyScreen ({ classes }) {
<AppBar></AppBar>
<div className={classes.appBarSpacer}></div>
<Box></Box>
}
export default style(MyScreen)
The mixin will give that div the same height as your AppBar, pushing down the other content.
According to Material-ui, there are 3 solutions to this problem.
https://material-ui.com/components/app-bar/#fixed-placement
You can use position="sticky" instead of fixed. ⚠️ sticky is not supported by IE 11.
You can render a second component
You can use theme.mixins.toolbar CSS
I personally enjoy using the 2nd solution like this.
return (
<>
<AppBar position="fixed">
<Toolbar>{/* content */}</Toolbar>
</AppBar>
<Toolbar />
</>
);
<AppBar position='static'>
use this it will do it and content won't hide under Appear
I think having a good app setup is opinianted, but I would recommend the following
import React from "react";
import ReactDOM from "react-dom";
import {
AppBar,
Typography,
Box,
CssBaseline,
makeStyles,
Container,
Grid,
Toolbar
} from "#material-ui/core";
const useStyles = makeStyles(theme => ({
content: {
flexGrow: 1,
height: "100vh",
overflow: "auto"
},
appBarSpacer: theme.mixins.toolbar,
title: {
flexGrow: 1
},
container: {
paddingTop: theme.spacing(4),
paddingBottom: theme.spacing(4)
}
}));
function App() {
const classes = useStyles();
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="absolute">
<Toolbar className={classes.toolbar}>
<Typography
component="h1"
variant="h6"
color="inherit"
noWrap
className={classes.title}
>
AppBar
</Typography>
</Toolbar>
</AppBar>
<main className={classes.content}>
<div className={classes.appBarSpacer} />
<Container maxWidth="lg" className={classes.container}>
<Grid container spacing={3}>
<Grid item xs={12}>
<Box>
<Typography variant="h1" style={{ border: "1px solid black" }}>
Hello
</Typography>
</Box>
</Grid>
</Grid>
</Container>
</main>
</div>
);
}
try this!
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
[theme.breakpoints.down('sm')]: {
marginBottom: 56,
},
[theme.breakpoints.up('sm')]: {
marginBottom: 64,
},
},
menuButton: {
marginRight: theme.spacing(1),
},
title: {
flexGrow: 1,
}, }))
You can add the above to your code like this
const Navbar = () => {
const classes = useStyles()
return (
<div className={classes.root}>
<AppBar position='fixed' color='primary'>
<Toolbar>
<IconButton
edge='start'
className={classes.menuButton}
color='inherit'
aria-label='menu'>
<MenuIcon />
</IconButton>
<Typography variant='h6' className={classes.title}>
News
</Typography>
<Button color='inherit'>Login</Button>
</Toolbar>
</AppBar>
</div>
)}
For more documentation visit material-ui breakpoint customization

Resources