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

Transfer the attribute of a styled-component during its utilization

I'm trying to convert my Header component into a styled-component, but I keep encountering the error: Uncaught TypeError: WEBPACK_IMPORTED_MODULE_4_styled_components.a.Header is not a function class Header extends Component { render() { ...

Unforeseen behavior of React Router on the server side

In the code snippet below, my server.js file is configured to handle server-side rendering with React and Express: import express from 'express'; import path from 'path'; import React from 'react'; import { renderToString } f ...

The error message "FormData parsing error: final boundary is missing" appears due

Recently, I made the switch from node to bun in a Next.js project. However, when trying to parse form data, I encountered this error: 14 | const POST = async (request)=>{ 15 | try { 16 | console.log("request: ", await request.heade ...

Encountering TypeScript error TS2345 while attempting to reject a Promise with an error

I recently encountered a perplexing TypeScript error message that I am struggling to comprehend. The specific error reads as follows: error TS2345: Argument of type '(error: Error) => void | Promise' is not assignable to parameter of type & ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...

The A-frame advances in the direction of the camera's view

I need help creating a component in A-frame that can move the player or camera based on the direction it is facing. The movement should be restricted to the x/y plane and not affect the y axis. Currently, my DOM setup looks like this: <a-entity> ...

The Angular filter received an undefined value as the second parameter

Currently, I am facing an issue while trying to set up a search feature with a custom filter. It appears that the second parameter being sent to the filter is coming through as undefined. The objects being searched in this scenario are books, each with a g ...

Siblings are equipped with the advanced selector feature to

I'm struggling to understand this setup I have: <div class="container"> for n = 0 to ... <a href="some url">Link n</a> endfor for each link in ".container" <div class="poptip"></div> ...

Jest is logging an excessive number of setTimeout calls

I'm currently testing a react component that utilizes setTimeout. An issue has arisen where Jest is indicating that setTimeout is being called even though it's not actually happening. The usage of setTimeout in this component involves removing an ...

How can an accordion icon be added and list toggling be accomplished when HTML data is sourced from an API instead of a JSON response?

I have an API that sends HTML data as a response. The task at hand is to add accordion icons to the items in the list and enable list toggling using HTML, CSS, JavaScript, React, and MaterialUI. Unfortunately, I am limited in my options and cannot use JQ ...

Leveraging Angular Dragula without the need for RequireJS

I'm eager to incorporate Drag and Drop functionality into my Angular project with the help of the angular-dragula module (https://github.com/bevacqua/angular-dragula). However, it appears that this module heavily relies on RequireJS. My experience wit ...

Tips for utilizing ion view within an ionic 2 application

In my original use of Ionic 1, I placed the footer after the <ion-content> and before . However, in the new Ionic 2, I can't seem to find any reference to <ion-view>. My specific need is to have two buttons at the bottom of the screen fol ...

Tips for utilizing innerHeight for a div during both window loading and resizing tasks?

I'm currently working on calculating the top/bottom padding of a div (.content) based on its height, and updating it upon loading and resizing the window. The goal is to have it centered nicely next to another div (.character) positioned beside it. I ...

Combining React and GraphQL into a single server

I'm in the process of setting up a fullstack app using express and GraphQL on the backend, paired with React on the frontend. The React app is located in a client folder within the backend repository. I have successfully directed all URLs to the clien ...

403 Error Code - Access Denied when trying to access Cowin Setu APIs

While attempting to create a covid vaccine alert using the Cowin Setu API (India) in nodejs, I encountered a strange issue. Every time I send a get request, I receive a 403 response code from cloudfront stating 'Request Blocked', although it work ...

Load a file in JavaScript without using the cache

I'm working on a function that reads a text file from a specific location: <html> <head> <script> function readTextFile(file){ var out_text='' var rawFile = new XMLHttpRequest(); rawFile ...

Leverage the power of zustand actions in your axios queries!

In my React application, I have implemented Zustand for creating a store: import { create } from 'zustand'; interface IAuth { username: string; isAuthentificated: boolean; updateAuth: (isAuth: boolean) => void; } export const useAuth = ...

Is there a quick way to use AJAX in Rails?

By using the remote:true attribute on a form and responding from the controller with :js, Rails is instructed to run a specific javascript file. For instance, when deleting a user, you would have the User controller with the Destroy action. Then, you woul ...

send back information obtained via an ajax request made by a javascript module

Here is a code snippet featuring an object with a function that makes an AJAX call. The function currently returns an empty array, but we need to figure out how to return the data after receiving the response. var receivedData = []; var AjaxUtil = { ...

Why is it that a JSX element can take a method with parentheses or without as its child?

Why is it that when I attempt to pass a method without parentheses into a React component as a child of one of the JSX elements, an error appears in the console? However, simply adding parentheses resolves the issue. What's the deal? For example: ran ...