Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
Avoid inline styling - Mui V5

In my opinion seperating styling from code makes the code clearer and cleaner. Using inline styling (style={{}}) has been considered a bas practice for me.

In Mui V4 is was easy - I've just created a styles file and imported it to my component code using makeStyles() and useStyles() hooks.

Now with the new sx prop in Mui V5 I have no idea how to avoid inline styling. Any ideas?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do something similar with sx too. Create a new styles file and define your objects there. Just use them in the sx attribute :

styles.ts:

const gridStyles = {
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop: 2,
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
};
export default gridStyles;
import gridStyles from './styles';

export default function FullWidthGrid() {
  return (
    <>
      <Grid
        container
        sx={gridStyles}
      </Grid>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

to respond to @Yahli Gi, if u want to access the theme, there's two ways:

Method one:

const gridStyles = (theme) => ({
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop:theme.spacing(1),
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
});

export default gridStyles;

Method Two:

const gridStyles = {
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop: (theme) => theme.spacing(1),
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
};

export default gridStyles;

The imports are the same for the two examples above. Additionally the theme is automatically injected into the object by mui so no need to call the function yourself in the import.

import gridStyles from './styles';

export default function FullWidthGrid() {
  return (
    <>
      <Grid
        container
        sx={gridStyles}
      </Grid>
  );
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!