Troubleshooting problem with margin sizing in Material UI Autocomplete

My project is currently using Material-UI Autocomplete, and I've made modifications to ensure that the font and component resize appropriately when the viewport changes size. To achieve this, I used vw units for widths, heights, and font sizes. However, after resizing, I noticed that the gap between elements increased disproportionately as shown in the gif below. How can I maintain the initial proportion of the gap while shrinking the entire component? I've tried adjusting various heights and margins without success. You can view all the code on the sandbox link provided below.

https://i.sstatic.net/pSRmQ.gif

Answer №1

Adjust the input height to 3vw for consistent height across elements.

import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  input: {
    width: "100%",
    height: "3vw", // Updated from 2vw
    fontSize: "1.25vw",
    color: "#02112E",
    backgroundColor: "green"
  },
  option: {
    fontSize: "0.8vw",
    height: "3vw",
    width: "100%",
    color: "#02112E"
  },
  noOption: {
    fontSize: "0.8vw",
    height: "3vw",
    width: "100%",
    color: "#02112E"
  },
  root: {
    "& label + .MuiInput-formControl": {
      marginTop: "1vw"
    },
    "& label.Mui-focused": {
      color: "#02112E",
      fontSize: "0.97vw"
    },
    "& .MuiInput-underline:after": {
      borderBottomColor: "#02112E",
      borderBottomWidth: "0.21vw",
      left: "0",
      transformOrigin: "left center",
      transition: "all 0.3s ease"
    },
    "& .MuiInput-underline:before": {
      borderBottomColor: "#02112E",
      borderBottomWidth: "0.07vw"
    },
    "& .MuiInput-underline:hover::before": {
      borderBottomColor: "#02112E",
      borderBottomWidth: "0.07vw"
    },
    fontSize: "1.25vw",
    width: "100%",
    height: "3vw",
    backgroundColor: "red"
  },
  inputRoot: {
    color: "#02112E",
    fontSize: "1.25vw",
    backgroundColor: "blue",
    transform: "translate(0, 2vw) scale(1)"
  }
});

export default function StyledAutoComplete() {
  const classes = useStyles();

  return (
    <Autocomplete
      style={{ width: "60%" }}
      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
          style={{ width: "100%" }}
          {...params}
          label="Option"
          variant="standard"
          inputProps={{
            ...params.inputProps,
            autoComplete: "new-password" // disable autocomplete and autofill
          }}
          InputLabelProps={{
            classes: {
              root: classes.inputRoot
            }
          }}
        />
      )}
    />
  );
}

const list = [{ title: "opt 1" }, { title: "opt 2" }];

Demo:

Output:

https://i.sstatic.net/KYvfu.gif

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

Div that dynamically adjusts its size to fit the content before switching to a fixed size

Apologies for the confusing title - finding it difficult to summarize in just one line! I have dynamic content loading into divs and a smooth resizing function that animates the height of these divs to auto. function adjustHeight(div) { var $selector = ...

Currently, I am expanding my knowledge of PHP and MySQL by working on a dynamic form that utilizes arrays. As I progress through the project,

One of my recent projects involved creating dynamic rows in a form using code. I managed to save the data in an array and display it using a foreach loop, but I'm facing challenges when trying to insert this data into a database. Here's a glimps ...

Animating the mobile menu transition using Bootstrap CSS

Can anyone help me with animating the mobile menu when I click the menu icon? This is what I have for the menu: .overlay-menu { position: fixed; display: none; z-index : 1040; width: 100vw; height: 100vh; background: rgba(0, 0,0, 0 ...

Tips for ensuring that each flexbox element fills up a single line

My goal is to have each child element fill up one line by itself. See this screenshot for reference: https://i.stack.imgur.com/F40Xm.png Below is the code I am currently using: main { display: flex; justify-content: space-around; align-items: ...

Tips for showcasing the currently active item in a Bootstrap dropdown menu using jQuery

I am trying to highlight the menu item that is selected in a drop-down menu. I attempted the following code, which works if I prevent the default behavior of the link element, but I do not want to do that. So I tried using local storage instead, but it d ...

Customize border color for a single cell in a table using Bootstrap 4

I'm facing an issue where I'm trying to apply a red border color to a specific cell, but it's not appearing on the top and left sides of the cell. It seems like the border color of other cells is taking precedence over the one I'm tryin ...

Increase the number of columns in ion-grid to a maximum of 24 columns by customizing the ion-grid columns

Can anyone assist me in achieving an ion-grid with 24 columns? I was researching the Ionic page (https://ionicframework.com/docs/layout/grid) and came across the property "--ion-grid-columns" under the "Number of columns" section. Unfortunately, there was ...

Resizing images next to lengthy text on mobile screens

My text and image keep behaving oddly when viewed in a smaller window or on mobile. The image resizes unpredictably based on the length of the text, as shown in my preview. I attempted to use flex wrap to contain the longer text, but it only pushed the ima ...

What is the best way to navigate to the bottom of a page when new data is added?

I've created a chat feature in my Ionic app, but the issue is that when a user receives a new message while being on the chat screen, the view doesn't automatically scroll down to show the new message. The user has to manually scroll down to see ...

Height of Material-UI Grid Item

I am working on a row layout with multiple items and I need all the items to have the same height, matching the height of the tallest item in the grid. <Grid container layout={'row'} spacing={8}> <Grid item className={classes.sect ...

Responsive Design and Advertising with CSS Media Queries

For my application, I am utilizing Twitter's bootstrap (responsive) css. My goal is to incorporate banner ads in the sidebar section. However, due to different media query settings, the sidebar's width will vary, resulting in the need for the ban ...

Update the CSS styling in Angular when the mouse enters

I have a unique dart and event with their own ids created in separate controllers. My goal is to create a functionality where hovering over the dart highlights the corresponding event with the same id, and vice versa. The events are displayed on the image& ...

Disappear Image Overlay when Mouse Hovers

Currently, I am in the process of working on a new webpage. You can view the progress at the following link: . When hovering over the Referrals Button, a Background Overlay will appear. To enhance the visual effect, I am looking to implement a gradual appe ...

mui Grid inside a container - adjusting both horizontal and vertical spacing

My goal is to create a grid of multiple cards with consistent horizontal and vertical spacing. Below is the code I have tried, where I need to maintain the different Box elements for future additions. Despite my attempts to adjust the flexbox options in th ...

The jquery click event is not working as expected

Hey there, I've been working on creating a dropdown menu that can be activated by clicking for better usability on touch screens. $('a.dropsmall2').click(function() { $(this).next('ul').slideToggle(500,'easeInOutQuad'); ...

What could be causing appendChild to malfunction?

I'm having an issue trying to create three elements (parent and one child) where the third element, an <a> tag, is not appending to modalChild even though it's being created correctly. modal = document.createElem ...

The pure css overlay does not display correctly on Chrome

Could you take a look at the link provided below (just widen the result window a bit) and let me know why there are white lines on top and on the sides of the boxes when the hover overlay is fully loaded? Even when I use a background image for those divs, ...

Resetting the "clear: both" property for the nth-child element when multiple nth-child elements are being inherited

Currently utilizing CSS (specifically SCSS) to style the same element on various media sizes - A, B, and C. Utilizing the following for A: &:nth-of-type(2n+1) { clear: both; } For B: &:nth-of-type(3n+1) { clear: both; } Finally, for C: ...

Is there a way to utilize the reset() function within an input form?

Is there a way to automatically reset the textbox when submitting a value? Below is my code: <!DOCTYPE html> <html> <body> <ul id="myList"> </ul> <input type="text" id="myText" value="&q ...

Step-by-step guide on incorporating edit, update, and discard functionalities within an Angular Material table component (mat-table)

I am currently working on implementing edit, update, and discard functions in an angular material table. While I have been able to successfully edit and update the table row wise, I am struggling with how to discard table rows. If you would like to see wh ...