How to change mui textfield border color on hover in react js?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To change the border color on hover in mui textfield, you have to use mui material styles, which we can define on hover in mui textfield.

Today, I am going to show you. how to change mui textfield border color on hover in react js mui text field.

const CssTextField = styled(TextField)({
  '& .MuiOutlinedInput-root': {
    '&:hover fieldset': {
      borderColor: 'yellow',
    },
  },
});

Installation

Install the following packages to use mui text field in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

MUI material text field change border color on hover example

To change the border color on hover, the class .& .MuiOutlinedInput-root has to be used.

App.js

import * as React from 'react';
import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';

const CssTextField = styled(TextField)({
  '& label.Mui-focused': {
    color: 'green',
  },
  '& .MuiInput-underline:after': {
    borderBottomColor: 'green',
  },
  '& .MuiOutlinedInput-root': {
    '& fieldset': {
      borderColor: 'red',
    },
    '&:hover fieldset': {
      borderColor: 'yellow',
    },
    '&.Mui-focused fieldset': {
      borderColor: 'green',
    },
  },
});

export default function CustomizedInputs() {
  return (
    <Box
      component="form"
      noValidate
      sx={{
        display: 'grid',
        gridTemplateColumns: { sm: '1fr 1fr' },
        gap: 2,
      }}
    >
      <CssTextField label="Custom CSS" id="custom-css-outlined-input" />
    </Box>
  );
}

In the above code example, I have used the @mui/material TextField component and changed the border color on the hover of all types of MUI Textfield.

Check the output of the above code example.

React, Mui Textfield Border Color on Hover

All the best 👍 🤝

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts