Customize the underline color of Material-UI's Input component

Trying to create an input component with a white underline. However, the underline color changes to black when hovering over it. It should remain white. Override the underline class as shown in the demo and instructions below. Despite trying to implement this solution, it does not seem to work. Manually inspecting and removing certain styles in the browser resolves the issue.

Example: https://stackblitz.com/edit/yjpf5s (View: )

Style removed manually in browser to achieve desired functionality:

.MuiInput-underline-365:hover:not(.MuiInput-disabled-364):not(.MuiInput-focused-363):not(.MuiInput-error-366):before {
  border-bottom: 2px solid rgba(0, 0, 0, 0.87);

The override class style being utilized:

underline: {

        color: palette.common.white,
        borderBottom: palette.common.white,
        '&:after': {
            borderBottom: `2px solid ${palette.common.white}`,          
        },              
        '&:focused::after': {
            borderBottom: `2px solid ${palette.common.white}`,
        },              
        '&:error::after': {
            borderBottom: `2px solid ${palette.common.white}`,
        },                      
        '&:before': {
            borderBottom: `1px solid ${palette.common.white}`,          
        },
        '&:hover:not($disabled):not($focused):not($error):before': {
            borderBottom: `2px solid ${palette.common.white}`,
        },
        '&$disabled:before': {
            borderBottom: `1px dotted ${palette.common.white}`,
        },              
    },

Edit: The final working solution was:

'&:hover:not($disabled):not($focused):not($error):before': {
    borderBottom: `2px solid ${palette.common.white} !important`,
},

Answer №1

Upon examining the source code, I noticed that they have implemented the following structure:

{
   focused: {},
   disabled: {},
   error: {},
   underline: {
    '&:before': {
        borderBottom: '1px solid rgba(255, 133, 51, 0.42)'
    },
    '&:after': {
        borderBottom: `2px solid ${theme.palette.secondary.main}`
    },
    '&:hover:not($disabled):not($focused):not($error):before': {
        borderBottom: `2px solid ${theme.palette.secondary.main}`
    }
}

This implementation has been effective for me.

Answer №2

After being inspired by Guillaume's response, I have created a simplified version of the working code without considering error handling:

const CustomizedWhiteTextField = withStyles({
  root: {
    '& .MuiInputBase-input': {
      color: '#fff', // Adjust text color
    },
    '& .MuiInput-underline:before': {
      borderBottomColor: '#fff8', // Semi-transparent underline
    },
    '& .MuiInput-underline:hover:before': {
      borderBottomColor: '#fff', // Solid underline on hover
    },
    '& .MuiInput-underline:after': {
      borderBottomColor: '#fff', // Solid underline on focus
    },
  },
})(TextField);

To implement this customized component, use:

<CustomizedWhiteTextField
  fullWidth
  onChange={this.handleNameChange}
  value={this.props.name}
/>

Answer №3

Start by inserting your input in this manner

<Input {...props} className='myClass' />

Next, modify your CSS like so:

.gc-input-bottom::after{
    border-bottom: 2px solid $input-border-color-active!important;
    :hover{
        border-bottom: none!important;
    }
}

.gc-input-bottom::before{
    border-bottom: 1px solid $input-border-bottom-color!important;
}

The before selector will display the underline constantly and the after selector will show the underline after clicking on it. Now you can customize it as needed.

Answer №4

As of March 2023, in the current version of MUI, the proper way to implement styling for an input component is as follows:

            <Input
              sx={{ color: "#D8D8D8", ':before': { borderBottomColor: '#808080 !important' }, ':hover:before': { borderBottomColor: 'red !important' }, ':after': { borderBottomColor: 'red !important' }}}
              id="username-input"
              onKeyDown={keyPressedHandler}
              onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
                setValues({ ...values, username: event.target.value });
              }}
            />

This specific example demonstrates custom styling for a username input field. The use of !important has been applied across multiple properties to ensure styling priority. It was observed that ':hover:before' required the !important tag for proper functionality, while others may function without it.

Answer №5

consider attempting it this way

.CustomInput-highlight-24:hover:not(.CustomInput-disabled-23):not(.CustomInput-focused-22):not(.CustomInput-error-25):before {
    border-bottom: 2px solid rgb(255, 255, 255) !important;
}

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

What is the best way to adjust image size based on different screen sizes while ensuring the buttons at the top remain responsive

How can I make the image and image select and delete buttons responsive? I am looking to eliminate the gap after the delete button. Any suggestions are welcomed. <div class="container mt-0"> <div class="row"> ...

Managing images in JavaScript while conserving memory

Welcome I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px. My vision involves creating a captivating visual experience for users as t ...

Material-UI is experiencing too many re-renders, resulting in an unstable layout. The TextareaAutosize feature has been implemented to restrict the number of renders and avoid triggering an

I'm facing an issue with the code snippet below: export function MyCustomInput({ handleChange, defaultValue }) { return ( <TextField defaultValue={defaultValue} onChange={(event) => { if (handleChange) { handl ...

Display a new component when the button is clicked within the current component while completely hiding the previous component

As a beginner in React, I am seeking a way to show a different component when a button is clicked within the current component. The goal is to immediately display the new component without showing the previous one. I aim to have only the new component dis ...

I receive an [object HTMLCollection] as the output

I am currently working on recreating a login page and I want it so that when the button is clicked, it displays the name you entered, but instead, it returns [object HTMLCollection]. My expectation was to have the name displayed as entered. var nombre ...

Accessing elements within a ReactJS map structure using the material-ui Selectable List component is not supported

I'm facing a dilemma. Unfortunately, my proficiency in English writing is not up to par... ※Please bear with me as it might be hard to follow. I'm trying to choose the ListItem component, but for some reason, I can't select the ListIt ...

Using jQuery, learn how to successfully call a selector from dynamic content

I am currently facing a challenge with a table that is generated server-side and then appended to the view page (client-side). Since the table is not directly included in the DOM, I am using the StickyTableHeaders jQuery plugin to create a sticky header fo ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

excess margin running along the left side of each page

When visiting http://tadris3.ir/, one may notice a peculiar space on the left side of all pages. Can anyone assist in resolving this bothersome issue? ...

Adding margin padding to a Material UI Navbar: Step-by-step guide

Hey there, I've added buttons to my Navbar from Mui. However, I'm running into an issue where the margin and padding won't change no matter what I do. I'm trying to create some space between them. Please see the code below: import { use ...

Animating or transitioning CSS keyframes to an inline value when the page is initially loaded

In my current project, I am working on creating HTML and CSS-based bar representations of percentage values. The structure of the code is as follows: Using HTML: <div class="chart"> <div class="bar" style="width:43%"></div> </div&g ...

Issue with CSS causing dropdown to open underneath modal

I am facing an issue with my bootstrap modal that contains multiple dropdowns. To accommodate the content, I have set overflow: auto to enable a scroll bar on the right side of the modal. The problem arises when I click on a dropdown - it opens under the ...

Pause jQuery at the conclusion and head back to the beginning

As a beginner in the world of jQuery, I am eager to create a slider for my website. My goal is to design a slideshow that can loop infinitely with simplicity. Should I manually count the number of <li> elements or images, calculate their width, and ...

Tips for Keeping a Responsive Image at the Forefront of a Text-Image Layout as You Scroll

I'm currently in the process of creating a website where text appears on the left side with an accompanying image on the right. The challenge I'm encountering is ensuring that as users scroll, the image adjusts dynamically based on the associated ...

Tips for aligning an image and paragraph in the center of a table row using Material UI

I designed a table using material UI that includes images and paragraphs in the same row. However, I am facing an issue where they do not align at the same height as required for them to be centered in the row. Currently, the view shows a mismatch in heig ...

Ensure that the Footer spans the entire width of the screen by using a container with fluid width, while

What is the best way to ensure that my Footer spans the full width of the browser window? Note: My website uses Bootstrap, with the Footer utilizing 'container-fluid' while the page content utilizes 'container'. Live link: HTML < ...

What is the method to customize the color of the Stepper component in React Material UI?

The image shows my attempt to modify the step color based on its status: green for correct, yellow for in-progress, and red for incorrect. Can anyone guide me on how to achieve this? ...

The Angular Date Picker stubbornly refuses to show dates in the format of DD/MM

Implementation of my Application import { MAT_MOMENT_DATE_ADAPTER_OPTIONS, MAT_MOMENT_DATE_FORMATS, MomentDateAdapter } from '@angular/material-moment-adapter'; import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-fie ...

Having trouble viewing the CSS menu on Internet Explorer 8? Could it be a potential JavaScript issue causing the problem?

Receiving complaints about the menu bar dysfunction in Internet Explorer 8 has left me bewildered. Despite working perfectly on all other browsers and earlier versions of IE, I cannot figure out what's wrong with the code. You can view the website at ...

Particular arrangement of a table

I am looking to create a responsive table that has a vertical left header row on mobile devices and a normal horizontal header row on desktop, like the following: Header 1 data data data Header 2 data data data Header 3 data data data Does anyone have ...