Adjust the spacing between the inputRoot and input in Material UI Autocomplete

Recently, in one of my projects, I've been utilizing Material-UI's Autocomplete component. Despite its lack of flexibility with resizing, I managed to tweak certain width, height, and font size attributes successfully. However, I'm currently facing an issue with a substantial margin between the inputRoot and input elements when resizing the viewport. Images below showcase this problem, highlighting the fixed margin that doesn't scale proportionally with the rest of the components:

https://i.stack.imgur.com/QYRoM.png https://i.stack.imgur.com/mtVvf.png


import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import { makeStyles , withStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
    // CSS styles here
  });

  
  
  export default function CountrySelect() {
    const classes = useStyles();
  
    return (
      <Autocomplete
        style={{ width: "60%", height: "3.47vw" }}
        options={list}
        classes={{
          root: classes.root,
          option: classes.option,
          noOptions: classes.noOption,
          input: classes.input
        }}
        disableClearable
        freeSolo
        noOptionsText={'No Options'}
        autoHighlight
        getOptionLabel={(option) => option.title}
        renderOption={(option) => (
          <React.Fragment>
            {option.title}
          </React.Fragment>
        )}
        renderInput={(params) => (
          <TextField
            {...params}
            label="Option"
            variant="standard"
            inputProps={{
              ...params.inputProps,
              autoComplete: 'new-password',
            }}
            InputLabelProps={{
              classes: {
                root: classes.inputRoot
              }
            }}
          />
        )}
      />
    );
  }

  
const list = [
  { title: 'Option 1'},
  { title: 'Option 2'},
];  

If you'd like to see a demo, check it out here.

Answer №1

Make sure to include the following code snippet in your root:

"& label + .MuiInput-formControl":{
  marginTop:"1vw"
},

Additionally, consider adding this code snippet to your inputRoot to prevent it from dropping below the dropdown on smaller screens:

transform: "translate(0, 2vw) scale(1)"

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

My REACT App is experiencing difficulties with routing

I am facing an issue with my REACT App. It works perfectly on my local host, but when deployed on Heroku, it behaves differently. Here is the problem: On my local host, the main page of the app has a navbar with various options, including Tickets. Clickin ...

Step by step guide to applying a personalized "margin" to your CSS

I have set the body element's margin to 0 in my CSS stylesheet. However, I am struggling to successfully set the margin for the div elements. Therefore, I would like to set all other things' margin to 0; except for the div elements with the cl ...

Is there a way to retrieve match.params from within a Redux Thunk action?

Is there a way to achieve the functionality shown below, without manually passing data into the function from a component? function doSomething() return (dispatch, getState) => { console.log(getState().router.match.params) } } This is i ...

The function signature '(_event: React.SyntheticEvent, value: number) => void' cannot be assigned to the type 'FormEventHandler<HTMLUListElement>'

I am facing an issue with my component "PageFooter" being duplicated in three other components. I am trying to refactor it as a UI component. " I am getting the error message: 'Type '(_event: React.SyntheticEvent, value: number) = ...

Properties of the State Object in React Redux

I'm curious as to why my state todos were named todo instead of todos in the redux dev tools. Where did that name come from? There is no initial state, which makes me wonder. I'm currently following a Udemy course by Stephen Grider, but I am wor ...

Is there a way to establish a dynamic relationship between the edit-mode field type of a specific material-table column and the value of another column, while keeping the changes isolated to

Issue Description I am seeking a solution where the input field displayed in edit mode varies based on the value of another column for that specific row. For example, when editing, if 'Column X' contains 'A', then 'Column Y' ...

Modify the background color of a pseudo-element's property value dynamically

How do I change the background color of my burger menu by clicking on an icon? This is the CSS code I have: :root{ --pseudo-backgroundcolor: yellow; } .menu__icon span, .menu__icon::before, .menu__icon::after{ background: va ...

Looking to incorporate nested rules in `JSS` using `CSS`?

Can someone guide me on how to use CSS class-nesting in Material-UI or JSS in general? I have been attempting the following: card: { cardHeader:{ marginTop:"30px" } } ...

typescript ways to exclude enum values

I am working with enums in TypeScript. enum Status { Cancelled = 'cancelled', Completed = 'completed', Created = 'created' } Now, I need to create another enum that includes only the values Completed and Created. enum S ...

How can I utilize Hooks to effectively filter arrays?

Currently, I am working on transitioning from a class-based function to a stateless function. As part of this process, I am refactoring my code by updating each event handler that uses this.SetState to use useState instead (in this case setMovies). Below ...

How can I adjust the flex spacing based on the width of the window?

My figures are evenly spaced using flex spacing, but when I resize the window, they extend beyond the parent section. Here is the HTML code snippet: <section class="features"> <figure> <img src="Images/Vanilla-Cup ...

Discovering the maximum font size that can be displayed on a single line in CSS

I am working with a wrapper div tag that has the capability of adjusting its dimensions. <div class="wrapper"> <p class="inside-text">Some text</p> </div> How can I use CSS to set the font-size of inside-text to be as large as ...

Converting text to icons and adding tooltips using only CSS

I am faced with the challenge of transforming a hyperlink into an icon while converting the text content into a tooltip using only CSS and without the ability to modify the HTML or add JavaScript. Data is being extracted from an external source in the form ...

Dealing with unresponsive sidenav in MaterializeCSS when trying to click and pop-out with React

One particular challenge I am encountering relates to user experience when it comes to authentication. If a user is authenticated, the AuthLinks will be displayed, but if not, the GuestLinks will show instead. The issue arises when switching to responsive ...

Locate a specific tag based on its content using Scrapy

What is the best way to locate a tag based on its content? My current method works well for finding elements, but it can be unreliable due to varying page structures. yield { ... 'Education': response.css('.provider- ...

Click to Resize Window with the Same Dimensions

I have a link on my website that opens a floating window containing more links when clicked. <a href='javascript:void(0);' onclick='window.open("http://mylink.html","ZenPad","width=150, height=900");' target='ZenPad'>&l ...

How do you trigger the playback of a specific audio file when a user clicks on it?

I'm currently working on an interactive music app that mimics the functionality of a piano. Users are able to click on different boxes on the screen, triggering a unique musical note to play. While I initially considered manually collecting all the I ...

Transferring Server Port from Mac OS to Ubuntu results in failure (Express + ReactJS)

After porting the exact copy and settings to Ubuntu, I encountered an issue where I receive a 'Failed to load resource: net::ERR_EMPTY_RESPONSE' error via Chrome DevTools. The Express server is running with PM2, and MongoDB is also running as a ...

New to CSS Media Queries? Discover the Correct Way to Define Element Height!

Sorry for the oversized images! I'm diving into media queries for the first time. When I adjust my browser window to 320px, here's how my site displays: This is what I want it to look like on mobile phones. However, when I view it on my iPhone, ...

Steps to retrieve accurate data from a database and display it in Autocomplete MUI

I am exploring MUI and attempting to replace my Select combo with Autocomplete. I have managed to load the proper value and save it in a SQL Server DB, but I am unsure about what and how to input in the value property to visualize the component for loading ...