Achieve uniform height for two elements using Material UI

Here is the code snippet I have:

<div className="center">
  <TextField
    variant="outlined"
    className="manualUserFollowTxt"
    required
    id="manualUserFollowTxt"
    label="Username"
    name="username"
    autoComplete="username"
    autoFocus
  />
  <Button variant="contained" color="primary" className="manualUserFollowButton"
    onClick={(e) => this.followButtonClick(e, document.getElementById("manualUserFollowTxt").value)}
  > 
    Follow
  </Button>
</div>

This code gives a result as seen in this screenshot.

The desired layout should have the TextField and Button elements of the same height, with some space between them on a single line and centered. Something similar to: this desired outcome.

Would appreciate tips on how to achieve this look!

Answer №1

If you want to ensure that the TextField and Button are the same height and aligned properly, one solution is to utilize Flexbox in CSS on the parent div.
Simply set the display property to flex and the flex-direction property to row to achieve this layout.
To add spacing between the TextField and Button, you can apply margin-right specifically to the TextField using material-ui styling:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';

const useStyles = makeStyles(theme => ({
  textField: {
    marginRight: theme.spacing(2),
  },

}));

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

  return (
  <div style={{display: 'flex', flexDirection: 'row'}}>
    <TextField
      variant="outlined"
      className={classes.textField}
      required
      id="manualUserFollowTxt"
      label="Username"
      name="username"
      autoComplete="username"
      autoFocus
    />
    <Button variant="contained" color="primary" className="manualUserFollowButton"
      onClick={(e) => this.followButtonClick(e, document.getElementById("manualUserFollowTxt").value)}
    > 
      Follow
    </Button>
</div>
  );
}

https://codesandbox.io/s/material-demo-19jhr?fontsize=14

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

Jest is throwing a TypeError, indicating that the provided function in the ClassName is not recognized as a valid function

I've encountered an issue with my JavaScript files. Here's a snippet of the code: import axios from "axios"; export default class SomeService { static getSomething(id) { return axios .get(API_BASE_URL + API_URL_Something ...

Can the default Bootstrap classes in the ExtLib application layout control be modified?

I am currently using the responsive Bootstrap theme in combination with the application layout control in extlib, but I have been unable to locate any documentation on whether it is possible to modify the predefined Bootstrap classes. In the example below ...

Using Vuetify to display text fields in a single row

Check out this Vue component template (View it on CODEPEN): <div class="some-class"> <v-form > <v-container @click="someMethod()"> <v-row> <v-col cols="3" sm="3" v-for="p in placeholders" ...

Using Flexbox to align content in a column with space between items

Is there a way to move the read more button to the bottom of the card using flexbox in a column layout? The top section, middle paragraph, and see more button are each contained within their own div inside the parent card div. https://i.stack.imgur.com/NG ...

Trick for adjusting padding on top and bottom for responsive blocks of varying widths

Hello there, I've been working on a project where I need to create a responsive grid of cards, like news articles, that maintain their proportions even when the browser's width changes. To achieve this, I decided to use an aspect-ratio hack by s ...

Compiling SCSS to CSS in Vue.js is a breeze

I am working on a vue js project that includes scss files. My goal is to compile these scss files into css and store them in the public folder. How can I achieve this? Should I make changes to the vue.config.js file? const path = require('path'); ...

A function that generates a table by rendering multiple arrays conveniently includes placeholders for empty data cells

When I combine arrays to display their data in a table, everything seems to go smoothly at first. However, there appears to be an issue where empty td tags are created after each loop from the map function, causing the rows to shift down. Test Data: expo ...

Protect an API with React utilizing CAS (Centralized Authentication Service) for front end sign in and Spring Boot for backend and making Rest API requests

Currently, I have successfully implemented CAS to secure the frontend of my React app using the package found here: https://www.npmjs.com/package/react-cas-client Now my aim is to secure the backend as well and restrict access to API calls from the app by ...

What are the best ways to utilize React events in conjunction with Material-UI components?

Is there a way to use standard React events like onMouseLeave with the Material-UI DataGrid? The DataGrid exposes an onCellHover prop but lacks an event for when hovering ends. <DataGrid onMouseLeave={handleMouseLeave} /> Unfortunately, this does no ...

Challenges arise with data updating following a mutation in @tanstack/react-query

As I work on building an e-commerce website using React, I have a specific feature where users can add products to their favorites by clicking a button. Following this action, I aim to update the profile request to display the user's information along ...

Table featuring identical submit buttons in each row: initial submit button is nonfunctional (potential issue with multiple identical IDs)

My table displays product files, with the option to add notes. If a note is added, it shows in a row. If not, a text area field with a submit button appears instead. Everything seems to be working well, except for the first row without a note. After addin ...

Exploring the Implementation of Multiple Values within React-Table Cells

In the code snippet I provided, I have the following: { Header: "Amount", accessor: "reward_amount", Cell: ({ value }) => `$${value / 100.0}`, } Here, the value represents the reward_amount, but there is also another val ...

Enhance autocomplete functionality by incorporating a left icon feature for text fields within the autocomplete component

I have a component with autocomplete functionality that displays tags Autocomplete with tags and I am trying to add a left icon, but only the right icon is functioning correctly. Current Issue When I add a left icon, it shows up but prevents the renderi ...

What could be the reason for typescript not issuing a warning regarding the return type in this specific function?

For instance, there is an onClick event handler attached to a <div> element. The handler function is supposed to return a value of type React.MouseEventHandler<HTMLDivElement> | undefined. Surprisingly, even if I return a boolean value of fal ...

Mui Drawer transitions

I'm currently working on a project for a company and using Mui drawer with a variant drawer. However, I've noticed that the opening and closing of the drawer happens very quickly and I would like it to have a slower transition. The usage of the ...

What is the solution to customizing the default inline styles of Material UI icons?

For my current project, I am required to use material ui 0.16.6. I am struggling with sizing the icons as they come with default inline styles. How can I override these styles? I've attempted styling them in the following manner but have been unsucces ...

Exploring Vaadin 14 Slider Components

I'm looking for help on how to incorporate a slider into my Vaadin 14 project. I discovered that the slider component was removed in Vaadin 10, so I turned to this alternative: Once I added the Maven dependency and repository to my pom.xml, I success ...

Asynchronous data fetching with React Hook useEffect does not properly populate the tooltip in Material UI component

After using useEffect to fetch data, I encountered a problem in passing the data to my component. Here is some of my code: User Data Types (UPDATED) export interface IUser { display_name: string; id: string; images: Image[]; } expo ...

Adjust the size and color of text in Chart.js using Angular 5

Why does the font color in chartjs appear as light gray and not print when you want to do so from the page? I tried changing the font color of chartjs in the options attribute, but it didn't work. How can I change the font color in chartjs angular? ...

Utilizing a React component as a function: A step-by-step guide

I am looking to implement a material ui dialog that can handle the result from JavaScript for a simple yes/no prompt. This is how I envision it working: <MyPromptComponent /> { MyPromptComponent.show('Do you really want to?').then((res ...