Tips for implementing a Material-UI TextField component without the background-color spilling over the border

Ensuring a Material-UI TextField is styled with a background-color on hover or focus has been a challenging task for me.

The code snippet for my component is shown below:

import React from "react";
import TextField, { TextFieldProps } from "@mui/material/TextField";
import { styled } from "@mui/material/styles";
import { Input, InputBase, InputBaseProps } from "@mui/material";

export const SearchBar = styled(TextField)<TextFieldProps>(({ theme }) => ({
  "& .MuiOutlinedInput-root": {
    color: "var(--grey-30)",
    marginRight: "10px",
    "& .MuiOutlinedInput-notchedOutline": {
      borderRadius: "30px",
      borderColor: "var(--primary)",
      borderSize: "1px",
      height: "56px",
      color: "var(--primary)",
    },
    "&:hover:not(.Mui-focused)": {
      color: "var(--grey-30)",
      borderColor: "var(--primary)",
      backgroundColor: "var(--primary-99)",
      borderRadius: "30px",
    },
    "&.Mui-focused": {
      color: "var(--primary)",
      backgroundColor: "var(--primary-99)",
      borderRadius: "30px",
      borderColor: "var(--primary)",
      "& .MuiInputAdornment-outlined": {
        color: "var(--primary)",
      },
    },
  },
}));

Here is how I use the component:

<SearchBar
  id="search-input"
  placeholder="Search"
  type="search"
  InputProps={{
    startAdornment: (
      <InputAdornment position="start" color="var(--primary)">
        <SearchIcon />
      </InputAdornment>
    ),
  }}
  value={searchParam}
  onChange={onSearch}
/>

View the Image of the styled TextField resembling a Search Bar with background-color overflowing the border

Despite my efforts, I have not found a solution to this specific issue in my research. Any suggestions or guidance would be greatly valued.

I attempted to remove the

"& .MuiOutlinedInput-notchedOutline"
without success. Additionally, I experimented with adding notchedOutline to the hover and .MuiFocused classes, which resulted in the correct background appearance but caused the text and adornment to disappear.

Answer №1

After testing and tweaking your code, I have successfully implemented the desired functionality. It seems that the issue lies in the

& .MuiOutlinedInput-notchedOutline
or fieldset being displayed with a transparent background. By adding a background color to the fieldset, the input value of the Textfield was being covered. To resolve this, I introduced a z-index to the .MuiOutlinedInput-input whenever the input is hovered or focused, ensuring it always displays on top of the fieldset. I refrained from removing any part of your existing code and simply offered possible solutions. I also applied random colors for input focus and hover effects, which you can customize to your liking.

import React from "react";
import TextField, { TextFieldProps } from "@mui/material/TextField";
import { styled } from "@mui/material/styles";
import { Input, InputBase, InputBaseProps } from "@mui/material";

export const SearchBar = styled(TextField)<TextFieldProps>(({ theme }) => ({
  "& .MuiOutlinedInput-root": {
    color: "var(--grey-30)",
    marginRight: "10px",
    "& .MuiOutlinedInput-notchedOutline": {
      borderRadius: "30px",
      borderColor: "var(--primary)",
      borderSize: "1px",
      height: "56px",
      color: "var(--primary)",
    },
    "&:hover:not(.Mui-focused)": {
      color: "var(--grey-30)",
      borderColor: "var(--primary)",
      backgroundColor: "var(--primary-99)",
      borderRadius: "30px",
    },
    "&.Mui-focused": {
      color: "var(--primary)",
      backgroundColor: "var(--primary-99)",
      borderRadius: "30px",
      borderColor: "var(--primary)",
      "& .MuiInputAdornment-outlined": {
        color: "var(--primary)",
      },

      "& > fieldset": {
        background: "red",
      },
      "& .MuiOutlinedInput-input": {
        zIndex: 10,
      },
    },

    "&:hover": {
      "& > fieldset": {
        background: "green",
      },
      "& > .MuiOutlinedInput-input": {
        zIndex: 10,
      },
    },
  },
}));

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

Tips for aligning the contents of multiple tables in HTML to the center

Currently, I am in the process of designing a website where there will be four titles displayed above four images, with corresponding descriptions below each. I have utilized two separate tables for the titles and descriptions. While the layout appears fin ...

Why does the JavaScript background color change take precedence over CSS selectors for my object?

Can someone explain why my list elements are not turning blue when I hover over them, even with the CSS and JS provided below? li { background:red; } li:hover { background:blue; } Here is the JS I am using: document.getElementsByTagName("li")[0].style.b ...

Resize the image to fit the dimensions of a div, but unfortunately, the overflow: hidden property is not effective in this

I'm looking to set up an image gallery using the Bootstrap grid layout. The gallery should span 100% width and consist of two grids in a row, each containing one picture. I want the height of the second picture to match that of the first, with croppin ...

Retrieve the width of a fixed div element based on the parent element's width, set

I attempted to set a fixed div to occupy 100% of its parent, but it is taking 100% of the screen instead. You can find the code I used here: http://codepen.io/rodboc/pen/ZOOEWp HTML <div id="wrapper"> <div class="box"> <div class="h ...

Issue with overflow in TailwindCSS design

I'm working on implementing a unique layout using Tailwind CSS for a dashboard. The height of the initial view should match the screen size and remain within those limits at all times. Initially, the blue section of the dashboard will be empty (but ...

Webkit causing complications with straightforward CSS3 animation

I'm currently working on a website and you can check it out here: Unfortunately, I've encountered an issue where the animations aren't triggering specifically on webkit browsers like Chrome and Safari: @-webkit-keyframes slideright{ 0% {tr ...

Variety of typography styles found within the Material UI design system

I am currently working with the Material UI and trying to implement the Typography component. I have noticed that if I do not specify a variant in the typography tag, everything works fine. <div className="login-details"> <Typography&g ...

What are some ways to enhance the opacity of a Material UI backdrop?

I'm looking to enhance the darkness of the material UI backdrop as its default appearance is not very dark. My goal is to make it dimmer and closer to black in color. ...

Steps to eliminate a horizontal scrollbar

It appears that I am working with CSS 2.1 as the style "overflow-x" is not present in my code. In the snippet below, I have defined headings for a grid and set the Id "pageLength" to introduce a vertical scrollbar for the grid's contents. However, thi ...

How can I create space between a checkbox and its label in Google Web Toolkit (GWT)?

How can I create space between a Checkbox and its content in GWT? Checkbox c = new Checkbox("checkme"); c.setStyleName("checkbox_style"); When I try using padding or margin, the entire checkbox and its content move together. Is there a way to achieve a g ...

I am unable to determine the origin or background of my sidebar

I am having an issue with styling a sidebar using HTML and CSS. The problem I am facing is with setting the background-color property. Despite my efforts to customize the style, the background remains transparent and shows the color of the page beneath it. ...

left-floating div

I am currently working on designing a page that requires multiple columns to be displayed. The columns should float left and never appear stacked on top of each other. Ideally, I would like the columns to extend beyond the screen without a scrollbar, makin ...

Below are the steps to handle incorrect input after receiving only one letter:

H-I This is my input .centered-name { width: 80%; margin: auto; } .group { width: 100%; overflow: hidden; position: relative; } .label { position: absolute; top: 40px; color: #666666; font: 400 26px Roboto; cursor: text; transit ...

What is the best method for inserting CSS into a webpage using a Chrome extension?

I am seeking guidance on how to incorporate CSS into a webpage using a Chrome extension. The CSS code I am attempting to inject is as follows: body { background: #000 !important; } a { color: #777 !important; } Here is the content of my manifest.j ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

Are the JQuery appended elements exceeding the width of the parent div?

I have an HTML div that is styled using the Bootstrap class span9. <div class="span9"> <div id = "selectedtags" class="well"> </div> <button class="btn" type="button" id="delegatecontent">Generate report</button&g ...

To add an image, simply click on the designated area instead of using the traditional file input button. The image will then be displayed in the specified image container and resized on the client-side

Objective: I am aiming to enhance the image upload functionality by enabling users to click on an <img> element to initiate the file selection process, instead of using the traditional <input type="file"> button. There will be three placeholde ...

Direct your attention towards the specific target div element using the get function

I need assistance with a webpage where items are listed using unique ids. I want the page to scroll up or down, depending on which link the user clicks, to find the element with the id specified in the address bar as foo=or2. Additionally, I would like the ...

Can anyone tell me how to find the total number of rows in a material react table component using a prop?

displayDataRowsCount I need a way to display the total number of rows in the table above the data ...

Using Font Awesome alongside Bootstrap Social Icons, displaying each icon's color in individual squares rather than the complete icons

This snippet displays the CSS: <head> <!-- Required meta tags always come first --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-u ...