Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS pseudo selectors with MUI

I have seen in a lot of the MUI code that they use pseudo selectors in their react styled components. I thought I would try to do it myself and I cannot get it to work. I'm not sure what I am doing wrong or if this is even possible.

I am trying to make some CSS that will offset this element against the fixed header.

import React from 'react'; import { createStyles, WithStyles, withStyles, Typography } from '@material-ui/core'; import { TypographyProps } from '@material-ui/core/Typography'; import GithubSlugger from 'github-slugger'; import Link from './link';  const styles = () =>   createStyles({     h: {       '&::before': {         content: 'some content',         display: 'block',         height: 60,         marginTop: -60       }     }   });  interface Props extends WithStyles<typeof styles>, TypographyProps {   children: string; }  const AutolinkHeader = ({ classes, children, variant }: Props) => {   // I have to call new slugger here otherwise on a re-render it will append a 1   const slug = new GithubSlugger().slug(children);    return (     <Link to={`#${slug}`}>       <Typography classes={{ root: classes.h }} id={slug} variant={variant} children={children} />     </Link>   ); };  export default withStyles(styles)(AutolinkHeader); 
like image 627
Joff Avatar asked Dec 13 '18 18:12

Joff


People also ask

How do I add a pseudo-class in material UI?

To add multiple classes in React Material UI using the classes props, we can use the classnames package. We call makeStyles with an object with the class names as the property names. And we set each property to an object with the CSS styles as the value. Next, we call useStyles to return the classes object.

Does MUI use CSS?

The css propEmotion's css() method works seamlessly with MUI.

How do you override CSS in material UI?

The second way to override the style of a component is to use the inline-style approach. Every component provides a style property. These properties are always applied to the root element. You don't have to worry about CSS specificity as the inline-style takes precedence over the regular CSS.

How do you use makeStyles in material UI?

In order to use makeStyles function in your application you will need to import it in the component in where you plan to access the css that results from your makeStyles function. This is assuming you have already installed Material Ui in your project. This is in order for react to recognize it as a styles file.


2 Answers

I found out that the content attribute needed to be double quoted like this

const styles = () =>   createStyles({     h: {       '&::before': {         content: '"some content"',         display: 'block',         height: 60,         marginTop: -60       }     }   }); 

and then everything worked like expected

like image 92
Joff Avatar answered Sep 18 '22 04:09

Joff


As @Eran Goldin said, check the value of your content property and make sure it's set to a string "". Odds are, you are doing something like this:

'&::before': {   content: '',   ... } 

Which doesn't set the content property at all in the output stylesheet

.makeStyles-content-154:before {   content: ;   ... } 

In Material-UI style object, the content of the string is the css value, including the double quote, to fix it simply write

'&::before': {   content: '""', // "''" will also work.   ... } 
like image 35
NearHuscarl Avatar answered Sep 20 '22 04:09

NearHuscarl