What is the best way to ensure that the child element of a Material UI TableCell Component occupies the full height of the cell?

I am currently using MUI version 5 to create a table. In this table, there are two columns with cells that contain multiline TextFields. My goal is to have the TextFields completely fill the cell area. However, I am encountering an issue where when I start entering lines into the text field in one column, the height of the textfield in the other column remains the same and does not adjust to fill out the table cell. Image: https://i.stack.imgur.com/vLCeZ.png

In the image, red represents the backgroundColor of the TableCell, while yellow represents the backgroundColor of the TextField. I want to avoid seeing any red; I would like the textfield in the left column to increase its height as data is added to the right textfield.

I have experimented with different CSS combinations involving height 100% and positions. Additionally, I have tried using flexbox on the TableCell and flexGrow on the Input, but this caused issues with the TableCell not filling up the height of the row.

For reference, here is a code example: https://codesandbox.io/s/mui-playground-forked-xst8h?file=/src/containers/app.js

import React from "react";
import * as material from "@material-ui/core";

const App = () => (
  <material.TableContainer component={material.Paper}>
    <material.Table aria-label="simple table" stickyHeader={true} size="small">
      <material.TableBody>
        <material.TableRow>
          <material.TableCell
            align="center"
            style={{
              backgroundColor: "red",
              padding: 0,
              height: "100%"
            }}
          >
            <material.TextField
              multiline
              variant="outlined"
              style={{
                flexGrow: 1,
                height: "100%",
                width: "100%",
                backgroundColor: "yellow"
              }}
            />
          </material.TableCell>

          <material.TableCell
            align="center"
            style={{
              backgroundColor: "red",
              padding: 0
            }}
          >
            <material.TextField
              multiline
              variant="outlined"
              style={{
                height: "100%",
                width: "100%",
                backgroundColor: "yellow"
              }}
            />
          </material.TableCell>
        </material.TableRow>
      </material.TableBody>
    </material.Table>
  </material.TableContainer>
);
export default App;

Answer №1

To achieve the desired result, first apply the CSS rule height: 0 to the td element. Then, ensure that the textarea element has a height: 100%. Lastly, don't forget to set the height: 100% for the .MuiOutlinedInput-inputMultiline class.

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

Changing the structure of divs by using elements from different divs

I'm looking for some help with adjusting the CSS of a div when hovering over certain elements. Here is my current code: <div class = "container-main"> <div class = "container-inner"> <ul class = "list"> &l ...

HTML text-indent is a way to add extra space to

I cannot understand why my text is not indenting properly. At the very bottom left corner, the "Educational Specifications" text is enclosed within a p element as follows: <p style="text-indent: 3em">Educational Specifications</p> Why doesn& ...

One of the best features of Uno Material is its sleek

I'm currently working on implementing the bottomNavigationBar feature in the Uno Platform for my app. I've managed to get everything else up and running smoothly, but I'm facing some difficulty in customizing the color or opacity of the ripp ...

The Render function in ReactJS is not getting refreshed

My goal is to implement a chat feature using material UI. I have set up a function where users can submit new chat messages, which then go through the reducer and are stored in the redux store. The process seems to be working fine, except for the fact that ...

Error: The react.js application is unable to access the property 'classList' of a null object

I've encountered a bug that's been causing me some trouble. Every time I try to run my react application, I keep getting an error message that reads TypeError: Cannot read property 'classList' of null. As someone who is new to react, I& ...

Struggling to overcome the TS2322 error when assigning generic values

I am currently working on developing higher-order React components that will include default values for components labeled as "named". Here is a basic implementation example: type SomeProps = { a: string } type Variants = 'variantA' | 'var ...

Next.js Firebase Analytics Error: Undefined Window Object

I am currently working on implementing and exporting the Firebase analytics module in Next.js using Firebase v9. When attempting to run the code snippet below, I encountered an error message stating "ReferenceError: window is not defined". Howev ...

Generate three separate inline blocks and position elements within them without any impact on the neighboring blocks

In my attempt to create three blocks with set height and width, aligned on top/bottom with a couple of elements inside each one, I encountered an issue. Whenever I add additional DIVs to one of the elements (resulting in a different number of DIVs inside e ...

Is it acceptable to make network requests inside a setState function in React?

I am facing a situation where I need to update the state based on certain conditions, either by using the previous state or data fetched from a server. How can I ensure that this is done safely? this.setState((prevState: State) => { if (prevStat ...

The localhost server in my Next.js project is currently not running despite executing the 'npm run dev' command. When trying to access the site, it displays an error message saying "This site can't be

I attempted to install Next.js on my computer and followed the documentation provided to set up a Next.js project. You can find the documentation here. The steps I took were: Ran 'npx create-next-app@latest' Was prompted for the name of my proj ...

Unable to retrieve React state within the callback function

As I work with the following components: const ParentComponent: React.FC = () => { // Setting newType to some value within the code const [newType, setNewType] = useState<any>(undefined); // Enabling addEdge to true in another part o ...

What is the best way to horizontally center an image in Bootstrap 4 while having a fixed-top navbar and preventing vertical scrollbars?

I am struggling to achieve the desired outcome, which is depicted in this image: https://i.sstatic.net/i6X0j.jpg Specific requirements for this project include: The image must be responsive A fixed-top navbar should remain visible No vertical scrolling ...

Encountering a connection issue while attempting to install styled components using npm

Hello everyone, I am currently in the process of setting up my development environment. To start off, I created a new React project using npx create-react-app PROJECTNAME Now, I'm exploring the use of Styled Components. However, when I try to inst ...

When a class and ID are dynamically applied, the click event may not fire and the CSS may not be applied

Hey everyone, I am facing an issue with declaring id and class when creating a table dynamically. I have tried multiple functions to make my click event work but without success. Can anyone help me with this? Below is the code for my dynamic table where I ...

The value of the state variable remains constant within the useEffect hook

Here's the code block where data is fetched and logged successfully using useEffect: const [allBooks, setAllBooks] = useState([]); useEffect(() => { axios.get('/all_books').then(res => { setAllBooks(res.data); console.log(re ...

Adjusting Autocomplete input with react-hook-form and Material-UI

I am currently working with two Autocomplete fields. My objective is to update the value of the second field based on the value selected in the first field. However, I am encountering an issue where when I attempt to send the new value to the "setValue" f ...

An absence of data causes the function to malfunction

One of the components in my application is responsible for rendering select elements on the screen, allowing users to dynamically order data. The initial state is as follows: state = { sortBy: [ { author: 'asc' } ] }; In this s ...

Exploring the capabilities of React Emotion to implement themes from a ThemeProvider

I've created custom styling using Material UI theme. import { createTheme } from "@mui/material/styles"; export default createTheme({ typography: { tab: { fontFamily: "Raleway", textTransform: "none", ...

Is there a way to prevent links from opening in an iframe and have them open in the main window instead?

Imagine you have an iframe on your webpage: <iframe style="border-radius:15px;" width="190" height="450" frameborder="0" scrolling="no" marginheight="5" marginwidth="10" src="../img/interactive_map/interactive_sverige.htm"> </iframe> ...

What is the best way to include an arrow in a dropdown menu?

I've been working on restyling a Select box and I want to add a CSS arrow that rotates as the Select box expands. However, I'm struggling to align the arrow properly with the Select box. I've tried using code from the internet, but it hasn&a ...