Adjust the text color of a cell in a table when hovering over the table row

Is there a way to change the text color of each row to red when hovering over it in a table? I tried using &:hover with the color property, but it didn't work.

<TableRow
  key={row.id}
  sx={{
    "&:hover": {
      color: "red" // not working
    }
    // "& .MuiTableCell-root:hover":{ // --- on hover only hovered cell text color is changing
    //   color:"red"
    // }
  }}
>
  <TableCell align="left">{row.category}</TableCell>
  <TableCell align="left">{row.name}</TableCell>
  <TableCell align="left">
    <IconButton aria-label="delete">
      <span className="material-icons-outlined">
        delete_forever
      </span>
    </IconButton>
  </TableCell>
</TableRow>

MuiTableCell-root override is done on <TableRow>, but this is affecting only each cell, but not entire row.

I need color to be changed on hovering each row? how to do that?

CodeSandbox Demo.

Solution:

<TableRow
 key={row.id}
 sx={{
  "&:hover .MuiTableCell-root, &:hover .material-icons-outlined": {
    color: "red"
  }
 }}
>

Answer №1

UPDATE: Add the following code snippet to your TableRow

<TableRow
  key={row.id}
  sx={{
    "&:hover .MuiTableCell-root, &:hover .MuiTableCell-root span.material-icons-outlined": {
      color: "red"
    }
  }}
>
  {/* some code */}
</TableRow>

Answer №2

If you want to achieve this effect, there are a couple of ways to do it.

<Table
    sx={{          
      "& .MuiTableCell-root:hover": {
        color: "red"
      }
    }}
    /*...other code is omitted for the brevity*/

Alternatively, you can create custom styles like this:

const styles = theme => ({ 
  tableCell: {
    "$hover:hover &": {
      color: "red"
    }
  },
  hover: {}
});

Then simply apply these styles using the following code:

<TableCell
    className={classes.tableCell}
    component="th"
    scope="row"
 >
    {row.category}
 </TableCell>

Answer №3

apply the style "&:hover .MuiTableCell-root" to change the text color to red

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

Utilize Imagemagick to implement full contrast and brightness adjustments for images

I've developed a tool that enables users to adjust contrast and brightness of images online using CSS3 filters. Now, I have obtained two values for brightness and contrast ranging from 0 to [inf], where "1" indicates no change in brightness/contrast. ...

Changing the positions of objects on a resized HTML Canvas

I am currently developing a tool that allows users to draw bounding boxes on images using the Canvas and Angular. Everything was working smoothly until I encountered an issue with zooming in on the canvas causing complications when trying to draw bounding ...

What is the functionality of the CSS switch button?

I'm curious about how the cool turn on/off switch button actually works. Take a look at for reference. I'm interested in implementing a prevention check to confirm whether the user truly wants to switch the button. Here's a snippet of the ...

Enhancing functionality by integrating Jquery/JS with input fields and labels

I am currently facing a challenge in applying CSS to the label element when the corresponding input is selected. After attempting and failing to use input:checked + label due to limitations in the HTML structure that cannot be altered, I seek alternative ...

Prevent Second Division from wrapping around floated Division

I am running into a minor issue with the positioning of a div in my current project. +------------------------+ |+-------+ ~~~~ TITLE| <--how can I prevent text from wrapping around || | ~~~~~~~~~~~~~ |\ on the left ...

Enhancing the KeyValuePair by incorporating additional value or parameter

I currently have a List<KeyValue<string, int>> that I utilize in the View of one of my applications. While it functions as intended, I am interested in expanding it by potentially incorporating an additional parameter/value to the KeyValue pair ...

What could be the reason for the lack of CSS being applied to elements sharing the same class?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=de ...

React Semantic UI - A modern solution for semantic web development

Having trouble styling React Components with Semantic UI for React (). I've customized Semantic UI's styles Core, but sometimes I need to incorporate my own styles into their components. I prefer using the BEM methodology for CSS naming conventi ...

Error message: "Unexpected token" encountered while using the three.js GLTFLoader() function

I have a requirement to load a .glb model into three.js by using the GLTFLoader. I was able to successfully create a rotating 3D mesh on a canvas without encountering any errors in the console. However, when I tried to replace it with the .glb model, I enc ...

Error message: The configuration object used to initialize Webpack is invalid and does not adhere to the API schema, resulting in an unknown error

Attempting to grasp a basic project using the MERN stack through an example has been quite challenging. Unfortunately, when trying to execute webpack, an unexpected error occurred in the terminal. The operating system I am working on is Ubuntu v16.04. np ...

Tips for sending class objects with Typescript/React

I need to establish an Array of items with different characteristics as a Prop on my component. My goal is to transmit an array of items with unique properties to a component. Here's an example of what my array of objects might look like: ` let dish ...

Updating in React hooks after executing multiple promises simultaneously for handling bulk processes

I encountered an issue in my project where I needed to process a CSV file by sending a request for each line in the file. While everything seemed to work fine, I faced a challenge in providing feedback as each line was successfully posted. Whenever I tri ...

Ways to retrieve the baseURL of an axios instance

This question is being posted to provide an easy solution for fellow developers who may be looking for an answer. //Suppose you have an axios instance declared in a module called api.js like this: var axios = require('axios'); var axiosInstance ...

Interact with Database using PHP and AJAX/JQuery

I am looking for assistance on how to display user data without refreshing the page. In my form, when I input a user's ID, I want a div to show up with the corresponding user information. Can someone provide guidance on how to achieve this? Form Exam ...

The age calculator is failing to display the accurate age calculation

This code is designed to compute the age based on the selected value in the combo box, and display the result in the text box below. The age should update every time the user changes the selected value in the combo box. However, the computed age is not cur ...

What is the process for creating a hover linear wipe transition using CSS/JS?

It's worth noting that I can't simply stack one image on top of the other because I'll be dealing with transparent images as well. preview of linear wipe ...

Creating diagonal background cutouts using CSS

Can you create a diagonal cut in a background color using CSS? Check out this example https://i.sstatic.net/vtpYf.jpg .item { color: #fff; font-size: 30px; background-color: #565453; padding: 10px 10px 10px 10px; width: 52%; text-align: ce ...

The following alert will not be visible: alert("I have entered the change function");

I am currently learning how to use jquery and I am experimenting with the change method However, I am facing an issue where the alert message "I am inside change" is not being displayed Could you please provide assistance on how to resolve this? Below is t ...

Detecting planes using react-three/xr technology

I am utilizing react-three/xr and react-three/drei within my next.js project. This piece of code positions the 3D model in a fixed location. const TestComponent: NextPage = () => { return ( <div className="testar"> <ARCanv ...

What is the best method for eliminating the initial character in every line of a textarea?

The desired output should display as LUNG,KIDNEY,SKELETON>J169>U and E, CREATININE:no instead of >LUNG,KIDNEY,SKELETON>J169>U and E, CREATININE:no. Is there a way to achieve this using JavaScript? Specifically, the ">" character at the be ...