When you hover over the button, it seamlessly transitions to a

Previously, my button component was styled like this and it functioned properly:

<Button
component={Link}
to={link}
style={{
  background: '#6c74cc',
  borderRadius: 3,
  border: 0,
  color: 'white',
  height: 48,
  padding: '0 30px',
  width: 200,
}}
//className={classes.button}
>
{text}
</Button>

However, I'm now in the process of transitioning from inline styles to using the following approach:

export const useStyles = makeStyles(() =>
  createStyles({
    button: {
      background: '#6c74cc',
        borderRadius: 3,
        border: 0,
        color: 'white',
        height: 48,
        padding: '0 30px', 
        width: 200,        
    },
  }),
);

Unfortunately, after making this change, when I hover over my button, it turns transparent or translucent. This is unexpected as the styling remains consistent with the previous version. How can I prevent this unintended behavior?

Answer №1

By utilizing the makeStyles and createStyles, you can customize the theme of various components such as a Button or Text.

To ensure your styles are applied correctly, it is important to override the root of the component. Failure to do so may result in conflicts with default themes, especially evident in hover effects.

You have two options for implementation - either overriding the root directly or creating a custom hover effect through the use of classes.button:

const useStyles = makeStyles(createStyles({
    root: {
      background: "#6c74cc",
      borderRadius: 3,
      border: 0,
      color: "white",
      height: 48,
      padding: "0 30px",
      width: 200
    }
  })
);

export default function StackOverflowDemo() {
  const classes = useStyles();

  return <Button className={classes.root}>Hello Sandbox!</Button>;
}

An alternative approach involves adding the hover effect within the "button" object itself:

const useStyles = makeStyles(() =>
  createStyles({
    button: {
      background: "#6c74cc",
      borderRadius: 3,
      border: 0,
      color: "white",
      height: 48,
      padding: "0 30px",
      width: 200,
      "&:hover": {
        background: "red", // Customize with desired color
      }
    }
  })
);

export default function StackOverflowDemo() {
  const classes = useStyles();

  return <Button className={classes.button}>Hello Sandbox!</Button>;
}

The &:hover syntax used here is provided by JSS, which is incorporated into Material-UI. For further information on this topic, consider exploring these resources:

Answer №2

To identify the source of the issue, check the developer tools for the default styling that is causing the hover effect. If you want to override this default styling, simply apply your desired style. For example, to prevent any changes during hover, you can use the following code:

button:hover {
      background: '#6c74cc',
}

You can also utilize nesting for a more organized approach:

button: {
//...
"&:hover" {
background: '#6c74cc',
}
}

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

How to retrieve the row index from a table using JavaScript

This question has cropped up numerous times, and despite trying various solutions suggested before, none of them seem to be effective in my case. Within a modal, I have a table where users can make changes that are then used to update the database. The ta ...

Request Timeout: The server took too long to respond and the request timed out. Please try again later

I'm encountering an issue when attempting to send a dictionary as a JSON to the express API. The error message I keep receiving is: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_NSURLErrorFailingURLSessionTaskErrorKe ...

What is the best way to extract the event time when a user clicks on an event in fullcalendar?

Is there a way to extract only the time from an eventclick event in fullcalendar? Currently, I am receiving details about the event including date and time. How can I specifically retrieve just the time (e.g. 6:00:00 am)? You can find the code snippet tha ...

Is there a way to customize the color of the open date picker button?

import React from 'react' import { DesktopDatePicker } from '@mui/x-date-pickers'; function DatePicker() { return ( <DesktopDatePicker slotProps={{ textField: { variant: &a ...

What is the best way to remove text using javascript?

Hey everyone! I am new to coding in javascript and I'm hoping for some help with stripping text. I have a string that is formatted like this: <iframe src="http://embed.videokoo.com/61YVek?client_file_id=390856&width=720&height=480" style=" ...

Designing a patterned rectangle filled with stylish text

I am relatively new to the world of HTML and CSS, and I am encountering a specific challenge with CSS. My goal is to design a section that looks like this: ---Image--- --Text-- ---Image--- --Text-- ---Image--- --Text-- ---Image--- --Text-- The ma ...

Angular 5: Transforming and Concealing CSS Class Names

Can CSS selectors be renamed or obfuscated in an Angular CLI project? Many top websites like Google and Facebook use randomized CSS names for various reasons, such as preventing website scripting through targeting static class names. I would like to imple ...

finding the ID of the element that triggered a jQuery dialog

I'm utilizing jQuery dialog to trigger popup windows when buttons are clicked. <script> $(document).ready(function(){ $("#dialog-form").dialog({ autoOpen : false, height : 300, ...

Describing a property of an interface as the determined form of a conditional type that is generic

I am currently working on defining an interface that includes a method with a conditional function definition. For example: interface Example<T> { func: T extends string ? () => string : () => number; } class ExampleClass<T extends str ...

What is the best way to showcase a component using FlatList?

Discovering the power of React Native combined with TypeScript and Redux Toolkit Hello! I'm currently facing an issue with rendering a list of messages using FlatList. Everything renders perfectly fine with ScrollView, but now I need to implement inf ...

Updating div with specific class based on selected values

I have utilized Angular to display content based on selected values from a dropdown menu. Now, I am seeking a solution to dynamically add a CSS class to a div when specific values are chosen. In other words, I want to apply certain styling to the div base ...

retrieve information from the resolved data of a Promise.all method and Axios request

I have implemented the code below with the goal of extracting data from the data section of the PromiseResult object returned by a Promise. The code snippet is as follows: const promiseComment = axios.post(`${BASE_URL}/api/addComment/`, data, getDefaultHea ...

Having trouble getting the @tailwindcss/forms plugin to function properly alongside React

After installing the tailwindcss plugin forms using npm with npm install @tailwindcss/forms, I added the dependency in the forms section of my tailwindconfig file by including plugins: [ require("@tailwindcss/forms") ]. As per the documentation ...

Protractor for Angular 2: Pausing execution until specified element obtains a specified class

Looking for a method to delay my e2e test (angular2 project) until the targeted element receives a specific css class. Is there an alternative approach without using browser.wait() or browser.sleep()? ...

Retrieve the prior position using the AngularJS ui-router

Utilizing fromState and fromParams within the $stateChangeSuccess event allows us to access all details regarding the previous location. This concept is elaborated in the following discussion: Angular - ui-router get previous state $rootScope.$on('$s ...

Learn how to utilize the combineLatest/zip operators to only respond to emissions from the second observable while disregarding emissions from the first observable

Here's an example of how I'm initializing a property: this.currentMapObject$ = zip(this.mapObjects$, this.currentMapObjectsIndex$, (mapObjects, index) => mapObjects[index]); I want the value of this.currentMapObject$ to be emitted only ...

I customized the navbar in Bootstrap by centering it with custom CSS, but whenever I click on it, it suddenly shifts to one side. I'm puzzled as to why this is happening and

I attempted to center my navbar-toggler element, but was only successful in centering the navbar-contend using Bootstrap. I then took it a step further by trying to center the navbar-toggler icon with custom CSS, however, it now shifts to the left when cli ...

Learn the process of dynamically populating an HTML table with data using JavaScript and JSON

I have developed a code snippet to dynamically add an HTML table without using jQuery. The code serves as an application from the server to the client, where the client receives a JSON object to parse into a string. Here is how you can add an HTML table ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

Standards for coding across different languages

As I work on developing a framework that accommodates both C# and TypeScript, I am faced with an interesting dilemma. Take, for instance, the Validator class in C#: class Validator { public bool Validate(string value) { return someConditi ...