Adjusting elements in Material UI without distorting them

Struggling with resizing a select component from material UI led to some unexpected challenges. Despite attempting to adjust the size using the className property, the label ended up misaligned and the selection shade appeared larger than the select box itself (height: 24px). This is the approach I took:

<FormControl variant="outlined" className={classes.formControl}>
    <InputLabel id="demo-simple-select-outlined-label">Age</InputLabel>
    <Select
        labelId="demo-simple-select-outlined-label"
        id="demo-simple-select-outlined"
        value={age}
        onChange={handleChange}
        label="Age"
        className={classes.MuiSelect}

    >
        <MenuItem value="">
            <em>None</em>
        </MenuItem>
        <MenuItem value={10}>Ten</MenuItem>
        <MenuItem value={20}>Twenty</MenuItem>
        <MenuItem value={30}>Thirty</MenuItem>
    </Select>
</FormControl>

Additionally, here are the makestyles I employed:

const useStyles = makeStyles((theme: Theme) =>
    createStyles({
        formControl: {
            margin: theme.spacing(1),
            minWidth: 120,
        },
        selectEmpty: {
            marginTop: theme.spacing(2),
        },
        MuiSelect: {

            width: "338px",
            height: "24px"

        }
    }),
);

You can view the example on codesandbox

My final outcome was less than ideal:

Label misplaced on the box

Selection shade larger than intended box

Answer №1

When adjusting the size of the select element, it's important to also make corresponding changes to the .MuiInputLabel-outlined in relation to the InputLabel element, as well as the MuiSelect-outlined to ensure the shadows of the select box align with its width and height. You can refer to this helpful sandbox demo for guidance.

Start by incorporating the outlined and selectOutlined properties within the useStyles function shown below:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    formControl: {
      margin: theme.spacing(1),
      minWidth: 120
    },
    selectEmpty: {
      marginTop: theme.spacing(2)
    },
    MuiSelect: {
      width: "338px",
      height: "24px"
    },
    outlined: {
      transform: "translate(4px, 3px)"
    },
    selectOutlined: {
      padding: "0px 14px"
    }
  })
);

Next, apply these styles to the InputLabel and Select components like so:

<FormControl variant="outlined" className={classes.formControl}>
        <InputLabel
          className={classes.outlined}
          id="demo-simple-select-outlined-label"
        >
          Age
        </InputLabel>
        <Select
          labelId="demo-simple-select-outlined-label"
          id="demo-simple-select-outlined"
          value={age}
          onChange={handleChange}
          label="Age"
          classes={{
            root: classes.MuiSelect,
            outlined: classes.selectOutlined
          }}
        >
          <MenuItem value="">
            <em>None</em>
          </MenuItem>
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem value={20}>Twenty</MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>

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 activating only one button in React without using radio buttons?

I'm currently working on developing a quiz module where users can choose between two answers. const [selected, setSelected] = useState(true); const toggleSelection = () => { setSelected(!selected) } <div id="quizAnswers&q ...

Creating a personalized, perfectly centered container with a specific width in Twitter Bootstrap

I've been struggling with a unique challenge for the past day without success. I can't seem to figure out where I am making a mistake. My goal is to design a fixed container layout using Bootstrap, which is precisely 33em wide and horizontally c ...

Elements are randomly glitching out with CSS transitions in Firefox

Chrome is working perfectly for me, but when I switch to Firefox it behaves differently than expected I am attempting to create a simple animation (utilizing transitions) that continuously runs on mouseover and smoothly returns to the starting position on ...

What is the reason for nth-of-type selectors not functioning on the third element?

I have a question regarding CSS nth-of-type. In the following code snippet, nth-of-type(2) and nth-of-type(3) are functioning properly, however, nth-of-type(4) and nth-of-type(5) are not working as expected. Could there be an issue with my code? <div i ...

What is the process for customizing link and hover colors in Bootstrap 4?

I am struggling to change the link and hover color in a specific section of my design, especially since I am new to Bootstrap. Can someone guide me on how to achieve this in Bootstrap 4? Below is a simple code snippet I have attempted so far: <div cl ...

Placing an absolutely positioned element on top of other elements

Currently, I am working on a frontendmentor website and encountering difficulty in positioning the shopping cart div above all the other elements. Initially, I attempted to use z-index for this purpose, but it seems that it does not work with elements havi ...

What steps should I take to resolve the warning message: "npm WARN deprecated [email protected] : This project is no longer being maintained."?

Seeking assistance with understanding mimelib. I am attempting to install this REACT script but encountering numerous errors due to my limited react experience... The script I am using can be found here: https://github.com/webdesignleader/referBeam This ...

issue with web worker not sending data to react component

I have incorporated the audio-recorder-polyfill into my React project to enable audio recording for Safari. While the recording seems to initiate successfully, there is an issue with the availability of audio data. The "dataavailable" event does not trig ...

The latest version of Material UI, v4, does not currently support React 18

Looking to incorporate MUI (Material UI) into my website design. Encountering difficulties with installing this library, receiving the error message below: -npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While ...

I just finished crafting a dynamic line chart with d3.js within a React environment. However, I am now looking to add some personalized touches to enhance its appearance. Could you kindly review the details and code

I want to create a line chart using React and D3, similar to the one depicted in this image. Presently, I have partially implemented the chart with the code provided below. You can see it here. The code snippet I've developed so far is outlined as f ...

How can the issue of the navbar color not being able to disappear or turn transparent be resolved, given its connection to the body color?

@import url('https://fonts.googleapis.com/css2?family=Bai+Jamjuree:wght@400;500;600;700&display=swap'); :root { --color-body: #b6cbce; --color-heading: #eef3db; --color-base: #033f47; --color-base2: #022a30; --color-brand ...

What are the ways to personalize the material UI select component?

I am looking to customize a MUI select component that I have rendered on a dark background. My goal is to make the outline and all its contents light colors, specifically grey and white. https://i.sstatic.net/HFUBj.png So far, I have successfully changed ...

"Unlocking Privileges: Delving into Access Tokens, Refresh Tokens, and

I have implemented a JWT authentication scheme for my application. After conducting research on storing and utilizing access and refresh tokens, I still have some unanswered questions. My frontend is built with React and my backend is using .NET 6 Web API. ...

How to incorporate text into the white circle object using three.js

View the current state of my project on this JS Fiddle. I am looking to incorporate rotating text, whether in 3D or 2D. The text should rotate in sync with the white circle. I am open to any method that achieves the desired outcome. Below is the provided c ...

The endIconTint feature in the TextInputLayout seems to be malfunctioning

While working on a login form with TextInputLayout and TextInputEditText, I encountered an issue where the toggle password icon always appeared in white regardless of the color I assigned to it as endIconTint. I conducted tests on Android 6 Android 10 ...

Following the migration to Typescript, the React component is having trouble locating the redux store props and actions

Here is the structure of my app: export default class App extends Component { render() { return ( <Provider store={store}> <Router> <Header/> ...

A method to apply a class to the third <li> element using javascript

Can someone help me figure out how to add a class to the third element using Javascript? Here is the structure I am working with: <ul class="products-grid row four-columns first"> <li class="item"></li> <li class="item"></li&g ...

Button with improperly aligned text

I'm having an issue with two buttons where the text on one button is not centered. .mail_download_csv_btn{ width: 100px !important; font-size: 12px !important; } .margin_right_10{ margin-right:10px; } <link rel="stylesheet" href="https: ...

Tips for aligning 2 columns when both table cells contain inner tables

Concerning my HTML table, cells #3 and #4 contain inner tables. I am facing an issue with aligning the rows within each table in cell #3 and cell #4 properly. Sometimes, the length of text in a row exceeds a single line, causing misalignment with the othe ...

I could use some input on the best way to make a background video fill the screen while incorporating overlay text

Struggling to make the background video fit perfectly on all devices and browsers? While it looks great in Chrome now, I'm still facing some clipping issues with IE Edge. Any experts out there who can offer their validation or suggest improvements? I ...