React how to set image as background of navbar - reactjs

i'm using create-react-app and have a navbar that I would like to add a background image to. I have tried a couple different things like add the backgroundImage styling and adding an image tag but have not been able to make it work. The picture I am using is a .jpg and was downloaded from a stock image site. I added the image to my project by dragging it in and adding it to an images folder.
The current path from my header (what I called my navbar) file to my image is ../../images/pexels-kai-pilger-1341279.jpg
Here is my header file:
import React from 'react';
import { makeStyles } from '#material-ui/core';
import { ReactComponent as MenuLogo } from '../../images/menu-logo.svg';
const useStyles = makeStyles(theme => {
return ({
header: {
backgroundColor: '#d9d9d9',
boxShadow: '0rem 0rem 0rem 0.05rem #666666',
padding: '0rem 1rem 0rem 1rem',
position: 'relative',
width: '100vw',
height: 70,
zIndex: '100',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
backgroundImage: "url('SpaceLogo')"
},
headerActive: {
left: 352,
backgroundColor: '#d9d9d9',
boxShadow: '0rem 0rem 0rem 0.05rem #666666',
padding: '0rem 1rem 0rem 1rem',
position: 'relative',
width: '81.7%',
height: 70,
zIndex: '100',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center'
},
menuLogo: {
backgroundColor: '#d9d9d9',
border: 'none',
'&:hover': {
cursor: 'pointer'
}
},
});
});
function Header(props) {
const { toggleMenu, isMenuOpen } = props;
const classes = useStyles(props);
return (
<div className={ isMenuOpen ? classes.headerActive : classes.header } >
<button
className={classes.menuLogo}
onClick={() => toggleMenu()}>
<MenuLogo />
</button>
</div>
);
}
export default (Header);
I believe that is the only file that is needed for this question, but I can update if I need to add more info.
If I could get some help on how to get this set up, that would be great.
Bonus question: Is there a way for me to choose what part of the picture is visible in the navbar? For example, the image is a large square but my navbar is a thin rectangle. I would like to use the middle of the image as the background as opposed to the top.

header: {
backgroundColor: '#d9d9d9',
boxShadow: '0rem 0rem 0rem 0.05rem #666666',
padding: '0rem 1rem 0rem 1rem',
position: 'relative',
width: '100vw',
height: 70,
zIndex: '100',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
backgroundImage: `url(${SpaceLogo})`
},
This should be work

Related

How can I give ToggleButton Border Colors in material ui?

I need to use you for part of my project and I made a lot of changes to you as follows:
const GenderBoxStyle = styled(ToggleButtonGroup)(({ theme }) => ({
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'flex-start',
alignItems: 'center',
padding: 0,
gap: theme.spacing(3),
paddingLeft: theme.spacing(3),
border: 'unset !important',
'& .MuiToggleButtonGroup-grouped': {
border: '1px solid !important',
borderRadius: theme.spacing(2),
borderColor: 'unset',
color: theme.palette.text.primary,
},
}));
const ToggleButtonStyle = styled(ToggleButton)(({ theme }) => ({
color: theme.palette.text.primary,
px: 3,
cursor: 'pointer',
// outlineColor: theme.palette.grey[100],
// outlineWidth: '1px',
// outlineStyle: 'solid',
borderColor: theme.palette.grey[100],
border: '1px solid !important',
borderRadius: theme.spacing(2),
width: 168,
height: 48,
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 0,
}));
But I want to change the color of the border as shown below, but it is not possible to use the border:
enter image description here
How can I give Togglebutton Border Colors in material ui?
In the GenderBoxStyle you can try to add MuiToggleButton-standard and set the color for this.
I provide the code snippet, you can try it.
Hope this can help you.
const GenderBoxStyle = styled(ToggleButtonGroup)(({ theme }) => ({
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'flex-start',
alignItems: 'center',
padding: 0,
gap: theme.spacing(3),
paddingLeft: theme.spacing(3),
border: 'unset !important',
'& .MuiToggleButtonGroup-grouped': {
border: '1px solid !important',
borderRadius: theme.spacing(2),
borderColor: 'unset',
color: theme.palette.text.primary,
},
'& .MuiToggleButton-standard': {
color: '#30a39c'
}
}));
Here is the result for changing the color of the border.

My custom makeStyles for MaterialUI Button component is getting overriden by ..css-zln006-MuiButtonBase-root-MuiButton-root

My custom makeStyles for MaterialUI Button component is getting overridden by .css-zln006-MuiButtonBase-root-MuiButton-root.
I have a Material UI Button component:
import { Button } from "#mui/material";
import useStyles from "./HeaderStyles.js";
const Header = () => {
const classes = useStyles();
return (
<React.Fragment>
<AppBar position="fixed">
<Toolbar disableGutters>
<Button
className={classes.button}
variant="contained"
color="secondary"
>
Click me
</Button>
</Toolbar>
</AppBar>
</React.Fragment>
);
};
export default Header;
My HeaderStyles.js file:
import { makeStyles } from "#mui/styles";
const useStyles = makeStyles((theme) => ({
button: {
fontFamily: "Pacifico",
textTransform: "none",
fontSize: "1rem",
borderRadius: "50px",
marginLeft: "50px",
marginRight: "25px",
height: "45px",
},
}));
export default useStyles;
My styles are getting overridden by the .css-zln006-MuiButtonBase-root-MuiButton-root class.
This class is replacing my button style.
.css-zln006-MuiButtonBase-root-MuiButton-root {
display: '-webkit-inline-box',
display: '-webkit-inline-flex',
display: '-ms-inline-flexbox',
display: 'inline-flex',
WebkitAlignItems: 'center',
WebkitBoxAlign: 'center',
MsFlexAlign: 'center',
alignItems: 'center',
WebkitBoxPack: 'center',
MsFlexPack: 'center',
WebkitJustifyContent: 'center',
justifyContent: 'center',
position: 'relative',
boxSizing: 'border-box',
WebkitTapHighlightColor: 'transparent',
backgroundColor: 'transparent',
outline: '0',
border: '0',
margin: '0',
borderRadius: '0',
padding: '0',
cursor: 'pointer',
WebkitUserSelect: 'none',
MozUserSelect: 'none',
MsUserSelect: 'none',
userSelect: 'none',
verticalAlign: 'middle',
MozAppearance: 'none',
WebkitAppearance: 'none',
WebkitTextDecoration: 'none',
textDecoration: 'none',
color: 'inherit',
fontFamily: '"Roboto","Helvetica","Arial",sans-serif',
fontWeight: '500',
fontSize: '0.875rem',
lineHeight: '1.75',
letterSpacing: '0.02857em',
textTransform: 'uppercase',
minWidth: '64px',
padding: '6px 16px',
borderRadius: '4px',
WebkitTransition: 'background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
transition: 'background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
color: 'rgba(0, 0, 0, 0.87)',
backgroundColor: '#FFBA60',
boxShadow: '0px 3px 1px -2px rgba(0,0,0,0.2),0px 2px 2px 0px rgba(0,0,0,0.14),0px 1px 5px 0px rgba(0,0,0,0.12)'
}
My button style
.makeStyles-button-59 {{
height: '45px',
fontSize: '1rem',
fontFamily: 'Pacifico',
marginLeft: '50px',
marginRight: '25px',
borderRadius: '50px',
textTransform: 'none'
}
How to solve this?
#mui/styles is the legacy styling solution for MUI. It is deprecated
in v5. It depends on JSS as a styling solution, which is not used in
the #mui/material anymore. If you don't want to have both emotion &
JSS in your bundle, please refer to the #mui/system documentation
which is the recommended alternative.
Use sx prop instead
const buttonStyles = {
borderRadius: 50,
fontFamily: "Pacifico",
fontSize: "1rem",
textTransform: "none",
};
<Button sx={buttonStyles} variant="contained" color="secondary">
Click me
</Button>

How to only give MUI Switch border style when it is checked?

Here is the demo link. Right now I give root a border style. However, I want to remove the border style when the switch is checked. How would I do this? Thanks for the help!
import * as React from "react";
import Switch from "#mui/material/Switch";
import { withStyles } from "#material-ui/core/styles";
const label = { inputProps: { "aria-label": "Switch demo" } };
const CustomSwitch = withStyles({
root: {
width: "40px",
height: "20px",
padding: "0px",
borderRadius: "10px",
marginRight: "8px",
border: "1px solid #606060",
position: "relative"
},
colorSecondary: {
"&.Mui-checked + .MuiSwitch-track": {
backgroundColor: "#05756C"
},
"&.Mui-checked": {
color: "white"
}
},
thumb: {
position: "absolute",
width: "12px",
height: "12px"
},
track: {
backgroundColor: "transparent"
},
switchBase: {
color: "#606060",
"&$checked": {
// color: 'white!important',
}
}
})(Switch);
export default function BasicSwitches() {
return (
<div>
<CustomSwitch {...label} />
</div>
);
}
Remove the border from the parent (SwitchBase) and put it in the track component inside so it can be targeted by CSS selector when the state of the sibling element changes:
root: {
...
// border: '1px solid #606060', // <-------------------- comment this line
},
track: {
backgroundColor: 'transparent',
borderRadius: '10px',
border: '1px solid #606060',
height: 'auto',
opacity: 1,
},
switchBase: {
color: '#606060',
'&.Mui-checked + $track': {
border: 'none',
},
},
Since you're using v5, you shouldn't use withStyles anymore, it is deprecated and will be removed in v6. Here is the equivalent demo in v5 using styled instead.

how to add background-clip property using css in js using react material ui

What exactly I want to do is to clip the background with only text using the CSS in Reactjs.
I have used Material UI for designing the page in react.
import { makeStyles } from '#material-ui/core/styles'
const useStyles = makeStyles({
root: {
minWidth: 275,
},
loginCard: {
width: '100%',
height: '100vh',
display: 'grid',
placeItems: 'center',
},
emailInput: {
margin: '0px 0px 20px 0px'
},
actions: {
padding: '16px'
},
submitBtn: {
marginLeft: 'auto',
},
bullet: {
display: 'inline-block',
margin: '0 2px',
transform: 'scale(0.8)',
},
title: {
fontSize: 25,
textAlign: 'center',
fontWeight: 'bold',
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
'&::-webkit-background-clip': 'text',
'&:: -webkit-text-fill-color': 'transparent'
},
pos: {
marginBottom: 12,
},
})
export default useStyles;
how to add background-clip property using CSS in js using react material UI?
to add -webkit-background-clip css style you've to use WebkitBackgroundClip in cssJS.
WebkitBackgroundClip compiles down to -webkit-background-clip.
Here is the working example of clipping the background with only text using only CSS in js
import React from "react";
import "./styles.css";
import { makeStyles } from '#material-ui/core/styles';
const useStyles = makeStyles((theme) => ({
body:{
background: 'black',
textAlign: 'center',
padding: '120px 20px',
width:'100vw',
height:'100vh'
},
heading:{
display: 'inline-block',
fontSize: '80px',
lineHeight: 0.9,
padding: '20px',
fontFamily: "'Syncopate', sans-serif",
textTransform: 'uppercase',
background: 'radial-gradient(circle farthest-corner at center center,white,#111) no-repeat',
WebkitBackgroundClip: 'text',
WebkitTextFillColor: 'transparent'
}
}));
export default function App() {
const classes = useStyles();
return (
<div className={classes.body}>
<h1 className={classes.heading}>mega<br/>fancy</h1>
</div>
);
}
codesandbox link:- https://codesandbox.io/s/competent-poincare-zx3lb

Key frames doesn't work in MUI styled components

I'm trying to create infinity rotating for an image (img tag) using built in styled component implementation from MUI (before it was implemented on external library).
Here is my fragment of code. If I'm running it I have an error.
export const SearchPreloaderContainerDiv = styled('div')({
margin: '0 auto',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
flex: 1,
'& h4': {
margin: '50px 0 0 0',
color: '#282828',
fontFamily: 'Cuprum',
fontStyle: 'normal',
fontSize: '43px',
lineHeight: '70px',
},
'#keyframes loader': {
from: {
transform: 'rotate(0deg)',
},
to: {
transform: 'rotate(360deg)',
},
},
'& img': {
padding: '20px',
boxSizing: 'border-box',
animation: 'loader linear 3s infinite',
},
});
Before it was implemented using keyframes helper from external styled-componet library but now I can not find similar helper in MUI

Resources