Can you provide step-by-step instructions on how to customize styles with MUI in my application?

I am encountering issues with MUI while attempting to customize the color of my buttons. The two methods I have tried so far have been unsuccessful.

For example, Method 1:

import React from 'react'
import { Typography } from '@mui/material'
import Button from '@mui/material/Button'
import Container from '@mui/material/Container'
import SendOutlinedIcon from '@mui/icons-material/SendOutlined';
import { makeStyles } from '@mui/styles';
import useStyles from './create-styles'

/* Styling Section (CSS) */

export default function Create() {
  const classes = useStyles()

  return (
    <Container>
      <Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
        Create a New Note
      </Typography>

      <div>
      <Button  onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
        Submit
      </Button>
      </div>
      
    </Container>
  )
} 

import { makeStyles } from '@mui/styles';

export default makeStyles(() => ({
    btn: {
        fontSize: 500,
        backgroundColor: 'pink',
        '&:hover': {
          backgroundColor: 'orange'
        }
      },
      title: {
        color: 'orange',
        textDecoration: 'underline',
        marginBottom: 20
      }
    
})) 

The above method is not working as expected.

Similarly, Method 2:

import React from 'react'
import { Typography } from '@mui/material'
import Button from '@mui/material/Button'
import Container from '@mui/material/Container'
import SendOutlinedIcon from '@mui/icons-material/SendOutlined';
import { makeStyles } from '@mui/styles';

/* Styling Section (CSS) */
const useStyles = makeStyles({
  btn: {
    fontSize: 500,
    backgroundColor: 'pink',
    '&:hover': {
      backgroundColor: 'orange'
    }
  },
  title: {
    color: 'orange',
    textDecoration: 'underline',
    marginBottom: 20
  }
})

export default function Create() {
  const classes = useStyles()

  return (
    <Container>
      <Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
        Create a New Note
      </Typography>

      <div>
      <Button sx={{fontSize: 60, backgroundColor: "red"}} onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
        Submit
      </Button>
      </div>
      
    </Container>
  )
}

Only when using this approach does the color customization work:

sx={{fontSize: 60, backgroundColor: "red"}}

Are there alternative solutions available to avoid styling inline?

I am currently following a tutorial video and it seems that the previous method of styling using MUI has become obsolete.

Answer №1

Below is a solution for your query

To style the button element, you will need to apply specific classes

import React from 'react'
import { Typography } from '@mui/material'
import Button from '@mui/material/Button'
import Container from '@mui/material/Container'
import SendOutlinedIcon from '@mui/icons-material/SendOutlined';
import { makeStyles } from '@mui/styles';



/* CSS Styling Section */
const useStyles = makeStyles({
  btn: {
    fontSize: 500,
    backgroundColor: 'pink',
    '&:hover': {
      backgroundColor: 'orange'
    }
  },
  title: {
    color: 'orange',
    textDecoration: 'underline',
    marginBottom: 20
  }
})

export default function Create() {
  const classes = useStyles()

  return (
    <Container>
      <Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
        Create a New Note
      </Typography>

      <div>
      <Button
        classes={{root: classes.btn}} // -- added this classes
        onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
        Submit
      </Button>
      </div>
      

    </Container>
  )
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

TypeScript version 3.7 has implemented a new feature where it will now display errors for each individual invalid prop instead of grouping them together as it

Scenario using TypeScript 3.5.3 https://i.stack.imgur.com/wykd6.png link to interactive playground - TS 3.5.3 demo running successfully Example with TypeScript 3.7.2 https://i.stack.imgur.com/BPckB.png link to demo - TS 3.7.2 demo not functioning correctl ...

Is there a way to ensure the content of two divs remains aligned despite changing data within them?

Currently, I have two separate Divs - one displaying temperature data and the other showing humidity levels. <div class="weatherwrap"> <div class="tempwrap" title="Current Temperature"> ...

Can we resize the button to match the width of the blue bar?

.nav-info { height: auto; background-color: darkblue; color: white; padding: 1em; padding-left: 5%; flex-direction: row; justify-content: space-between; } .flexbox { display: flex; } .info a { color: white; text-decoration: none; fo ...

Stacked divs needed to be aligned vertically

Is there a way to make the head and main tabs fixed vertically one below the other? Keep in mind that these need to be separate angular components and cannot be combined under another div. The rows also need to be scrollable. .pages { float: left; ...

What is the best way to ensure that text stays aligned perfectly next to an

There seems to be a problem with my CSS. The text in the <li> tag is appearing below the line of the icon. How can I resolve this issue? I want the text to be vertically centered on the line. This seems to happen when I use the ::before selector with ...

Upon the initial hover, the data added to the title tag following an Ajax call is not appearing

I am currently working on an ajax request that retrieves information such as username, email, and user_id. Once the ajax call is successful, I use jQuery to append this data to the title tag. The main issue I am facing is that the data is only displayed af ...

I'm having trouble resolving the issue in my React App after attempting to export a functional component. How can I troubleshoot and

Since the first week of this online course on Coursera.org, I've been struggling to get my React app to display. Even after watching videos and revising the code multiple times based on Google search results, I couldn't make it work. Despite seek ...

Stepping up your game: Utilizing Jodit-React to seamlessly upload images through

I am encountering a connection error. Despite reading through the document available at , I couldn't find any information about using axios to upload an image with jodit-react. I am unsure about what steps to take next, and would greatly appreciate an ...

Maintaining an even distribution of flex children on each line as they wrap to the next line

I am curious about finding a way to maintain an even distribution of elements on each line, including the last one, using flex-wrap or any other flexbox technique. For example, let's say I have a flexbox with 6 elements. When it wraps, I would like t ...

How to arrange data in angular/typescript in either ascending or descending order based on object key

Hey there! I'm fairly new to Angular and have been working on developing a COVID-19 app using Angular. This app consists of two main components - the State component and the District component. The State component displays a table listing all states, ...

What steps do I need to take to extract the date and month from a single datepicker using the code below?

<div class="col-md-3 pull-left" style="padding:9px"> <input type="text" id="datepicker" class="form-control"> </div> The HTML and C ...

CSS - Adding a Distinctive "Line" to a Navigation Bar

I am attempting to design a unique navbar layout with two "lines". The first line should consist of several links, while the second line would include an HTML select field with a submit button. The following code will generate the desired navbar visual ou ...

How can I make my div change color when the check-box is selected?

I have a dynamic table in my web application that is populated with data from a database. Each row in the table represents multiple time slots for a specific date. I am working on a feature where the background color of a time block can be changed if a che ...

Error: The current element cannot be clicked. Please try again

I have been attempting to trigger a click event on another button within an onClick event function. An error occurred: Uncaught TypeError: this.clickBack.current.click is not a function The React version being used is 16.8.6 constructor(props) { s ...

Adjusting the dimensions of the "overflow avatar" to be consistent with the other avatars displayed in AvatarGroup

When setting the max prop of the AvatarGroup, the additional avatar does not match the height of the other avatars. <AvatarGroup max={3}> <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" sx={{ width: 24, height: 24 ...

Fading away backdrop images for the header

I've created a header class in my CSS with the following styling- header { background-image: url('../img/header-bg.jpg'); background-repeat: none; background-attachment: scroll; background-position: center center; .backg ...

Developing a Burger-licious Menu in React

Seeking a straightforward solution to transfer Vanilla JS code into a React application. Currently working on converting a website from plain HTML/CSS/JS to React, and facing challenges with integrating existing JS functionality, such as a hamburger menu t ...

What steps should I take to diagnose issues with automatic reloading in VitePWA?

I have been experimenting with leveraging vite to automatically reload my PWA application in the browser when I make updates (following the instructions provided here). In order to implement this, I included the following code snippet in my vite.config.js ...

What is the best way to showcase Select with both parent and child information using material-ui?

I received an array from the API: [ { "id": 1, "code": "978", "name_en": "Dislplay name", "name_ru": "Dislplay name", "child": [] ...

The reCAPTCHA feature in Next.js form is returning an undefined window error, possibly due to an issue with

Trying to incorporate reCAPTCHA using react-hook-form along with react-hook-recaptcha is posing some challenges as an error related to 'window' being undefined keeps popping up: ReferenceError: window is not defined > 33 | const { recaptchaL ...