Changing the border color of a Material UI textbox is overriding the default style

  • Upon the initial page load, I expected the border color of the text box to be red.
  • However, it appeared grey instead.
  • I tried setting the border color to red for all classes but the issue persisted.
  • Even after making changes, the border color remained unchanged.
  • I'm unsure how to target this tag specifically.

  • I was able to achieve the desired result by using "!important", but is there a way to achieve it without using that?

  • If anyone has any insights on this, please let me know so I can address it in the future.

  • Please refer to my code snippet and sandbox link below.

https://codesandbox.io/s/m58841kkwp

 cssLabel: {
    "&$cssFocused": {
      color: { borderColor: "red" }
    }
  },
  cssFocused: { borderColor: "red" },
  cssUnderline: {
    "&:after": {
      borderBottomColor: "red"
    }
  },
  cssOutlinedInput: {
    "&$cssFocused $notchedOutline": {
      borderColor: "green"
    }
  },
  notchedOutline: { borderColor: "red !important" },



   <div className={classes.container}>
      <TextField
        className={classes.margin}
        InputLabelProps={{
          classes: {
            root: classes.cssLabel,
            focused: classes.cssFocused
          }
        }}
        InputProps={{
          classes: {
            root: classes.cssOutlinedInput,
            focused: classes.cssFocused,
            notchedOutline: classes.notchedOutline
          }
        }}
        label="Custom CSS border"
        variant="outlined"
        id="custom-css-outlined-input"
      />
    </div>

Answer №1

The issue arises from the conflict between your CustomizedInputs-notchedOutline class and the MuiOutlinedInput-notchedOutline class, resulting in the following CSS output:

.CustomizedInputs-notchedOutline-1356 {
    border-color: red;  //this is your class
}

.MuiOutlinedInput-root-1382 .MuiOutlinedInput-notchedOutline-1389 {
    border-color: rgba(0, 0, 0, 0.23);   //this class is overriding yours
}

To resolve this, you should use or create a more specific selector for the element, like so:

.more.class {    //multiple class selector
    border-color: red; 
}

Alternatively, if possible, modify the conflicting class directly.

EDIT: To address this, consider applying a stronger selector by adding & $notchedOutline to the cssOutlinedInput, as shown below:

cssOutlinedInput: {
    "& $notchedOutline": {   //include this nested selector
       borderColor: "red",
    },

    "&$cssFocused $notchedOutline": {
       borderColor: "green"
    }
}

Furthermore, remember to remove the !important modifier from

notchedOutline: { borderColor: "red !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

Identifying when two separate browser windows are both open on the same website

Is it possible to detect when a user has my website open in one tab, then opens it in another tab? If so, I want to show a warning on the newly opened tab. Currently, I am implementing a solution where I send a "keep alive" ajax call every second to the s ...

What is the process for dynamically updating a variable's state within a render function?

I'm currently working on a project for my class where we are tasked with creating a website using React. Unfortunately, neither myself nor my group members have been able to figure out how to render a function in a variable state and make it dynamic. ...

The Gulp task is not being recognized when I open the project in Visual Studio Code

Whenever I open gulp, I encounter an error related to minifying css and js files using gulp tasks. Despite my efforts to find a solution, the problem remains unresolved. Error Message: assert.js:374 throw err; ^ AssertionError [ERR_ASSERTION]: Task fu ...

Nuxtjs is incorporating the Vue-pano component for enhanced functionality

When using vue-pano with Nuxtjs, I encountered the error message: "window is undefined". If I import it like this: <script> import Pano from 'vue-pano' export default { components: { Pano } } </script> I then tried using a ...

Design a unique <Link> component within my React shared UI library by utilizing a monorepo approach

As a newcomer to application architecture, I am eager to experiment with building an app using a Monorepo structure. I have a query regarding a Next.js frontend app that utilizes my React-based UI package shared across multiple apps within the same Monore ...

What techniques can I use to adjust the size of an image through zooming in and out?

In my custom gallery component, the crucial code section looks like this: <Gallery> <Header> <img src={galleryIcon} alt='Galley icon' /> <h1>My Gallery</h1> </Header> ...

Error: The property value supplied for the calc() function is invalid

<App> includes both <Header> and <Content> components. I am attempting to calculate the height of <Content>, which is equal to the height of <App> minus the height of the header. // App.js import { useStyles } from "./Ap ...

Utilizing Typescript Decorators to dynamically assign instance fields within a class for internal use

I am interested in delving into Typescript Decorators to enhance my coding skills. My primary objective is to emulate the functionality of @Slf4J from Project Lombok in Java using Typescript. The concept involves annotating/decorating a class with somethin ...

Struggling to publish my Next.js app on Vercel. Any suggestions on how to proceed?

I have been facing an issue with my Next.js project. It runs smoothly on localhost, but I encountered a problem when trying to deploy it on Vercel. Below are the errors that I received: npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! ...

implementing a time picker feature with jQuery

As I am not familiar with jQuery, I came across this script while searching for a time picker. If anyone could guide me on how to incorporate this code into my HTML page to enable the time picker functionality, I would greatly appreciate it. /* jQuery t ...

Continuously refreshing the content

Having an issue with the $.ajax() function while trying to load content into a DIV from another page. The content keeps reloading into the element continuously. Observing my debugger, it seems that the element .driving_action_body is constantly reloading ...

JavaScript counter that keeps updating without pause

How can I create a live and continuous number counter on my website? After seeing the question above, I am interested in implementing a similar feature but with a slight twist. I am looking to have a counter that increments by 15.8 cents per second start ...

Transform Unicode characters into HTML using Qt

I am facing an issue with a particular string that includes Unicode characters and special symbols. The string, for instance, looks like this: SYMBOLSU+2510\n The string contains the Unicode character U+2510 and a new line symbol \n. I have a lo ...

Refresh nested object within a React context

I am having trouble updating a nested object in the context API in React Here is an example of my context setup: import { createContext, useState } from "react"; const TeethInfoContext = createContext(); export function TeethInfoProvider({ chil ...

Utilizing Reactjs to efficiently transmit name and value to Material UI autocomplete

I am trying to customize the Material UI Autocomplete component. I need to pass name, value and onchange similarly to how it is done for TextField. Can someone please help me achieve this? My current code is not functioning as expected. < ...

Is there a way to change the color of a specific tab without affecting the content within it

I am attempting to change the color of an individual tab using jQuery. However, when I set the background attribute in CSS, it colors the entire background instead. What specific property should I be setting to only change the color of the tab itself? ...

I am looking to incorporate a 10 px border at the top of my column by utilizing bootstrap4

How can I add a border to the top of my column using Bootstrap 4? I've been struggling with it and can't seem to get it right. Here is the code I'm currently using, which includes <div> tags for the columns: <!doctype html> &l ...

Adding an input field and icon at the center using ::after

Having trouble centering an input field with an icon after it in a div. I can center the input text easily using text-align:center, but struggling to center the icon added using ::before. The search text is centered, but not the icon How can I center bot ...

IE9 does not properly display SVGs used as background images

Utilizing SVG files as backgrounds for my HTML elements has been successful in most major browsers. However, an issue arises when trying to ensure compatibility with Internet Explorer 9. The problem lies within IE9's rendering of the SVG backgrounds, ...

Precise loading display in React using splines

When utilizing next.js to render a spline model, I have encountered an issue where the "onLoad" function does not accurately reflect the loading time. import Spline from '@splinetool/react-spline'; import { useState } fr ...