Unable to center label when adjusting TextField height beyond default

I was tasked with reducing the size of the MUI Component TextField by 4px. To achieve this, I modified the root & .MuiOutlinedInput-input. Although it adjusted the height as desired, the label (Email) is no longer vertically centered. I attempted to solve this issue by applying the following code to the sx:

.MuiInputLabel-outlined": transform: "translateY(-50%)"

Unfortunately, this caused the label to be displaced from the input field and shifted to the left. Is there a more effective approach to address this problem? Below is my code with only the TextField height adjusted and without any translateY implementation. It's important to note that I have not made any changes to the Password TextField yet.

<Box sx={{marginTop: "3rem"}}> 
    <TextField type="Email"
               label="Email"
               variant="outlined"
               value={loginEmail ? loginEmail : ""}
               name="Email"
               onChange={(e) => {
                   setloginEmail(e.target.value);
               }}
               sx={{
                  width: "100%",
                  maxWidth: "400px",
                  marginBottom: "0.5rem",
                  background: "transparent",
                  borderColor: "#ccc", // Setting a lighter border color
                  color: "#aaa", // Modifying the label text color to be lighter
               "& .MuiInputLabel-outlined": {
                color: "#aaa !important", // Adjusting the outline color of the label to be lighter
              },
              "& .MuiOutlinedInput-notchedOutline": {
                borderColor: "#ccc",
              },
              "& .MuiOutlinedInput-input": {
                color: "#aaa", // Changing the input text color to be lighter
                height:17
              },
              "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
                borderColor: "#946bde",
                
              },
             
            }}
            inputProps={{
              style: {
                color: "#aaa", // Adjusting the input text color to be lighter
              },
            }}
          />
          
          <TextField
            label="Password"
            type={showPassword ? 'text' : 'password'}
            variant="outlined"
            value={loginPassword ? loginPassword : ""}
            name="password"
            onChange={(e) => {
              setLoginPassword(e.target.value);
            }}
            InputProps={{
              endAdornment: (
                <InputAdornment position="end">
                  <IconButton
                    onClick={() => setShowPassword(!showPassword)}
                    onMouseDown={(e) => e.preventDefault()}
                    edge="end"
                    style={{ color: "#aaa" }}
                    
                    sx={{
                      
                      
                      transform: 'translateY(-17%)',
                      
                    }}
                  >
                    {showPassword ? <VisibilityOffOutlinedIcon /> : <VisibilityOutlinedIcon />}
                  </IconButton>
                </InputAdornment>
              ),
            }}
            sx={{
              width: "100%",
              marginTop: "2rem",
              maxWidth: "400px",
              marginBottom: "0.5rem",
              background: "transparent",
              borderColor: "#ccc", // Setting a lighter border color
              color: "#aaa", // Modifying the label text color to be lighter
              "& .MuiInputLabel-outlined": {
                color: "#aaa !important", // Adjusting the outline color of the label to be lighter
              },
              "& .MuiOutlinedInput-notchedOutline": {
                borderColor: "#ccc",
              },
              "& .MuiOutlinedInput-input": {
                color: "#aaa", // Changing the input text color to be lighter
              },
              "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
                borderColor: "#946bde",
              },
            }}
            inputProps={{
              style: {
                color: "#aaa", // Adjusting the input text color to be lighter
              },
            }}
          />
        </Box>

Answer №1

After some consideration, I ultimately decided to utilize the size prop in my implementation of a TextField component:

<TextField
    type="Email"
    label="Email"
    variant="outlined"
    size="small"
    value={loginEmail ? loginEmail : ""}
    name="Email"
    onChange={(e) => {
        setloginEmail(e.target.value);
    }}
    sx={{...............rest of code

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 include multiple HTML elements along with their attributes in a React array?

I need to include HTML elements with class properties in a React array. colArray1.push( <div className={classes.imgContainer} key={875643}>); However, I am encountering an error message saying "Cannot read property 'imgContainer' of u ...

React redux-thunk promise is currently in a state of waiting

Recently delving into the world of React Redux and experimenting with thunk middleware, I've encountered a puzzling issue. I'm struggling to explain it myself and hope someone can shed some light on the matter. The problem arises when I call a f ...

When implementing protected routes in React JS, the this.props.match.params may sometimes appear empty

I am currently working on a React.js app with react-router v5. I am facing an issue where the this.props.match.params is showing up as empty: Object { path: "/", url: "/", params: {}, isExact: false } This is how my App.js looks like: ...

Instructions for displaying a React component in the <main> element when employing react-burger-menu

Check out my code here. Utilizing react-burger-menu has allowed me to successfully implement the sidebar feature. The sidebar functionality is working as expected. My current challenge involves figuring out how to open the content for Home or GG within ...

What is the best way to show an HTML response from an API call on the DOM?

After making an API call, I receive the following HTML response and I am trying to display it on the DOM as actual HTML tags, rather than just showing text with HTML tags. API response: <strong>Hello</strong> Current DOM rendering: <str ...

Which CSS property is utilized to position the parent level of a menu below the children when they are shown on the screen?

Recently, my Drupal superfish menu has been acting up after updating the module. The issue I encountered was that the children no longer appear above their parent items. Can anyone help me figure out which setting dictates the order in which they are dis ...

The MUI toggle button does not indicate the selected input despite being configured with a value

Although the MUI toggle successfully registers user input, the selected option is not highlighted when clicked. I have included a value parameter and implemented an onChange function to update the value parameter on change. Initially, it does highlight " ...

Error: Could not retrieve reactjs - promise unhandled

Encountered an error while trying to fetch data in ReactJS I attempted to modify the code as follows: headers: { 'Access-Control-Allow-Origin': '*', "Content-type": "application/json; charset=UTF-8", 'mode':&apo ...

Vue component always renders a new line

Whenever I include a <component> tag in my main application, it always renders on a new line. Is there a way to make it inline? I am trying to achieve something like this: <p>This is a component :<component></component></p> & ...

Implement a function to delete an item from an array within a React object by utilizing the UseState hook

I am facing a challenge in React as I attempt to remove an element from an array within an array of objects using the UseState hook. Despite my efforts, the interface does not re-render and the object remains in place. From what I understand, in order for ...

I need help customizing the text on the bubble head of the Slider component in Material UI. Can anyone provide instructions on

https://i.stack.imgur.com/disqW.png Is it possible to use words instead of numbers for the values? Can I choose which values have labels? Is there an option to add icons as well? Screenshot source: https://material-ui.com/components/slider/#customized-sl ...

Designing Interactive Circular Dates on Website

Currently, I am working on a webpage using HTML, CSS, JavaScript, and PHP. The goal is to design a page that features 7 circles representing the current date and the next 6 days. Users should be able to click an arrow to navigate to the following 7 days. ...

Tips for decreasing the height of a cell or row in a React component

Currently, I am incorporating the table component in my React UI using Material-UI. Here is a link to the Material-UI Tables demo. I am looking to adjust the height of the rows or content cells within this table. The content rows are styled in alternating ...

Refresh a function following modifications to an array (such as exchanging values)

Looking to re-render a function after swapping array values, but the useEffect hook is not triggering it. I need assistance with this as I plan to integrate this code into my main project. Below are the JSX and CSS files attached. In App.js, I am creating ...

Connect the dots with a seamless line using Material-UI stepper functionality

I am currently working on implementing material-ui steppers, specifically the "small dot stepper." One challenge I am facing is connecting the dots with a line. I attempted to set the StepConnector position to absolute, but it does not function well when s ...

What is the best way to ensure that the larger child divs always fit perfectly within the parent div?

Creating a Responsive Layout <div class="container"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> CSS Styling .box1, .box2, .box3{ display: block; f ...

Exploring the Various Style Options in React

Can React handle multiple classes similar to CSS? const useStyles = makeStyles((theme) => ({ header1, header2, header3, header4: { display: 'flex', justifyContent: 'center', }, })); ...

Transitioning from clip to clip-path in order to utilize percentage values

My current approach involves using a scss-based animation to create border animations with movement. The animation's core functionality relies on the use of clip. $box-width: 80px; $box-height: 50px; @keyframes clipMe { 0%, 100% { ...

Switching images with Jquery

Seeking help to create an FAQ page using HTML, CSS, and jQuery. My goal is to rotate an arrow when a question is opened and revert it back when another question is clicked. I've successfully achieved this with <p> tags but facing issues with the ...

Connecting an onclick event to trigger an external file

I have a scenario where I need to link an array of buttons with respective mp3 files. For example, if button number 5 is clicked, the 5th mp3 file should be played. How can I modify the code below to achieve this functionality? Any examples or suggestions ...