reactjs - How to change the border color of Material UI TextField

Reactjs - How to change the border color of Material UI TextField

To change the border color of a Material UI TextField component in ReactJS, you can use the InputProps prop to pass custom styling. Here's how you can do it:

import React from 'react'; import TextField from '@mui/material/TextField'; import { createTheme, ThemeProvider } from '@mui/material/styles'; const theme = createTheme({ components: { MuiTextField: { styleOverrides: { root: { '& .MuiOutlinedInput-root': { '& fieldset': { borderColor: '#your_custom_color', }, '&:hover fieldset': { borderColor: '#your_custom_hover_color', }, }, }, }, }, }, }); function CustomTextField() { return ( <ThemeProvider theme={theme}> <TextField label="Custom Border Color" variant="outlined" /> </ThemeProvider> ); } export default CustomTextField; 

In this example:

  • Replace #your_custom_color and #your_custom_hover_color with your desired colors for the border and hover effects respectively.
  • ThemeProvider is used to apply the custom theme to the TextField.
  • The createTheme function from @mui/material/styles is used to create a custom theme where we override the default styles of the TextField component.

Make sure you have @mui/material and @mui/material/styles installed in your project to use Material UI components and theming.

Examples

  1. React Material UI TextField change border color

    Description: This query focuses on changing the border color of a Material UI TextField component in React.

    import React from 'react'; import TextField from '@mui/material/TextField'; import { createTheme, ThemeProvider } from '@mui/material/styles'; // Custom theme with updated TextField styles const theme = createTheme({ components: { MuiTextField: { styleOverrides: { root: { '& .MuiOutlinedInput-root': { '& fieldset': { borderColor: 'blue', // Change border color here }, }, }, }, }, }, }); function CustomTextField() { return ( <ThemeProvider theme={theme}> <TextField label="Outlined" variant="outlined" /> </ThemeProvider> ); } export default CustomTextField; 
  2. React Material UI TextField outline color change example

    Description: This example demonstrates how to change the outline color of a Material UI TextField.

    import React from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField() { return ( <TextField label="Outlined" variant="outlined" InputProps={{ style: { borderColor: 'green' }, // Change outline color here }} /> ); } export default CustomTextField; 
  3. How to customize Material UI TextField border color in React

    Description: This snippet shows how to customize the border color of a Material UI TextField component using inline styles.

    import React from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField() { return ( <TextField label="Outlined" variant="outlined" style={{ '& .MuiOutlinedInput-root': { borderColor: 'red' } }} // Change border color here /> ); } export default CustomTextField; 
  4. React Material UI change TextField border color on focus

    Description: This query addresses changing the border color of a Material UI TextField when it is focused.

    import React from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField() { return ( <TextField label="Outlined" variant="outlined" focused InputProps={{ style: { borderColor: 'purple' }, // Change border color here on focus }} /> ); } export default CustomTextField; 
  5. React Material UI TextField error state border color change

    Description: This example demonstrates changing the border color of a Material UI TextField in error state.

    import React from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField({ error }) { return ( <TextField label="Outlined" variant="outlined" error={error} helperText={error ? 'Error message' : ''} InputProps={{ style: { borderColor: error ? 'red' : 'blue' }, // Change border color on error }} /> ); } export default CustomTextField; 
  6. React Material UI TextField border color based on state

    Description: This snippet shows how to change the border color of a Material UI TextField based on component state.

    import React, { useState } from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField() { const [focused, setFocused] = useState(false); return ( <TextField label="Outlined" variant="outlined" focused={focused} onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} InputProps={{ style: { borderColor: focused ? 'green' : 'gray' }, // Change border color based on focus }} /> ); } export default CustomTextField; 
  7. How to apply CSS to Material UI TextField border color

    Description: This query involves applying custom CSS to change the border color of a Material UI TextField.

    import React from 'react'; import TextField from '@mui/material/TextField'; import { makeStyles } from '@mui/styles'; const useStyles = makeStyles({ root: { '& .MuiOutlinedInput-root': { '& fieldset': { borderColor: 'orange', // Change border color here }, }, }, }); function CustomTextField() { const classes = useStyles(); return ( <TextField label="Outlined" variant="outlined" className={classes.root} /> ); } export default CustomTextField; 
  8. React Material UI TextField border color change with styled-components

    Description: This example demonstrates changing the border color of a Material UI TextField using styled-components.

    import React from 'react'; import TextField from '@mui/material/TextField'; import styled from 'styled-components'; const StyledTextField = styled(TextField)` & .MuiOutlinedInput-root { & fieldset { border-color: pink; // Change border color here } } `; function CustomTextField() { return <StyledTextField label="Outlined" variant="outlined" />; } export default CustomTextField; 
  9. React Material UI TextField border color change on hover

    Description: This snippet shows how to change the border color of a Material UI TextField when hovered over.

    import React from 'react'; import TextField from '@mui/material/TextField'; function CustomTextField() { return ( <TextField label="Outlined" variant="outlined" onMouseOver={(e) => e.currentTarget.style.borderColor = 'brown'} // Change border color on hover onMouseOut={(e) => e.currentTarget.style.borderColor = ''} /> ); } export default CustomTextField; 
  10. React Material UI TextField border color change using CSS classes

    Description: This example demonstrates using CSS classes to change the border color of a Material UI TextField.

    import React from 'react'; import TextField from '@mui/material/TextField'; import './CustomTextField.css'; // Import CSS file with custom styles function CustomTextField() { return <TextField label="Outlined" variant="outlined" className="custom-textfield" />; } export default CustomTextField; 

More Tags

row-height argb date-range android-studio-import sequence-diagram virtualbox textarea android-canvas git-merge mamp

More Programming Questions

More Biochemistry Calculators

More Livestock Calculators

More Financial Calculators

More Housing Building Calculators