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

Could not find the element using the specified cssSelector

I am attempting to locate and interact with this specific div element. <div class="leaflet-marker-icon marker-cluster marker-cluster-small leaflet-clickable leaflet-zoom-animated" style="margin-left: -6px; margin-top: -6px; width: 12px; height: 12px; o ...

Emphasize specific letters in a word by making them bold, according to the user

In my app, there is a search feature that filters data based on user input and displays a list of matching results. I am trying to make the text that was searched by the user appear bold in the filtered data. For example, if the user searches 'Jo&apos ...

In my HTML document, I have three identical Id's and I need to figure out how to target the third one using CSS. Can you help

I am working on an HTML document that contains multiple instances of ids and classes. Here is a snippet of the code: <!DOCTYPE html> <html> <body> <div id="Caption_G" class="editor-group"> editor-group<br /> ...

Using the default theme in Material UI5

Could someone please explain how to pass in the default theme in Material UI5? In Material UI6, I used to do it like this: const useStyles = makeStyles((theme) => ({ home: { display: "flex", paddingTop: "8rem", width: ...

Can the default Bootstrap classes in the ExtLib application layout control be modified?

I am currently using the responsive Bootstrap theme in combination with the application layout control in extlib, but I have been unable to locate any documentation on whether it is possible to modify the predefined Bootstrap classes. In the example below ...

Is it possible to use the React Context API within the getInitialProps, getServerSideProps, or getStaticProps functions

Is there a way to invoke an action creator using the React Context API within getInitialProps/getServerSideProps/getStaticProps of NextJs? When working with Redux and NextJs, it is straightforward to access the store and dispatch an action creator. Howeve ...

Set the search input to occupy the full width of the container by utilizing Bootstrap's

I'm having trouble adjusting the width of my search input to 100% on screen resize (using @media (max-width: 1200px)). The issue: https://i.sstatic.net/EMr6W.jpg Here's how it should look (I can achieve this with a specific pixel width, but not ...

What method can I use to enlarge the carousel component from the flowbit-svelte library?

I have been working on a project that requires a Carousel, and I decided to incorporate the flowbit-svelte carousel into my svelte project. While the carousel is functioning well, I am facing an issue with the component size. It seems to be cutting off my ...

What could be causing Jest to struggle with locating data-testid within the map function in React?

Having trouble testing the code below. When data-testid = 'w_1' is added to a separate deletable div, it works fine. What could be causing this issue? export default class App extends Component { clickTest(){ alert('hi'); } re ...

Adjusting the size based on the screen width of various devices

I'm currently working on a website that is not responsive, and I know I can make it responsive using media queries. However, this will be a significant task... I'm looking for a quicker solution to simply zoom or scale my website to match the dev ...

Monitoring customer's credit card operations in React Native

Looking for suggestions on reliable APIs or services that enable an application to monitor a user's credit card and debit card transactions. Any recommendations? ...

Customizing the border of a FloatingActionButton in Material UI

How can I add a border to a FloatingActionButton? Which props should I use to achieve this? The iconStyle property does not seem to work. Here is the link to my code: https://codepen.io/palaniichukdmytro/pen/NgQmve Additionally, I would like to know how t ...

Utilizing Spring Boot Java with Axios.post in React for Effective Data Communication

Struggling with axios.post in React - here's my code React GenService.jsx import axios from "axios"; import { Route, withRouter } from "react-router-dom"; const COURSE_API_URL = "http://localhost:8080"; class GenService { retrieveAll() { re ...

The parameter cannot be assigned a value of type 'string' as it requires a value that matches the format '`${string}` | `${string}.${string}` | `${string}.${number}`'

I recently updated my react-hook-forms from version 6 to version 7. Following the instructions in the migration guide, I made changes to the register method. However, after making these changes, I encountered the following error: The argument being pass ...

Guide on how to create a cookie for visitors who are not logged in, to retrieve cart information from a database using React and Node.js

Currently, I am developing the add to cart feature for my project. For the front end, I am utilizing reactjs and for the backend, Nodejs with express. My goal is to store the cart information for users who are not logged in within the database instead of ...

Flexbox design that adapts responsively to display 3 items per row, shifting to 2 items and finally

I have a segment with six elements. For desktop, I would like to display 3 items per row; for medium devices, 2 items per row; and for mobile devices, only 1 item per row. <div class="flex-container"> <div>1</div> <div>2</d ...

Encountering a problem when utilizing webview in react native due to an error with the evaluation of

While utilizing webview for trading charts, I am encountering an issue where a warning is displayed and the URL fails to load. Below is the code snippet used for loading. It seems like the video URI works fine in full screen mode, but other URIs are not ...

What is the best way to implement React within a map function?

As I navigate through the data, I am encountering an issue where clicking on an image only displays the last item in my array when a modal popup should show the corresponding data.title and data.info. It seems like all the modals are opening at once, but I ...

How can I automatically scroll to an anchor element once CSS animation has finished

I am currently working on a CSS animation that looks like this: @-webkit-keyframes flip { 0% {opacity:1;} 100% {opacity: 0;} //This could be 90% //And here could be frame for 100% which would scroll it down. } #headercover { -webkit-animation-name ...

No overflow is occurring within the DIV element

This code snippet is fully functional on all browsers except for IE. Unfortunately, the overflow feature does not seem to be working correctly. Any assistance would be greatly appreciated. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ...