Managing the height of a TextArea within a Flexbox environment

After exploring various methods for achieving auto-height in a textarea, I decided to go with the contenteditable approach as it seemed cleaner to me. However, while it worked perfectly on its own, it failed when placed within flexbox containers.

I experimented with using max-height and min-height properties in different ways but couldn't find a successful solution. The main issue was that when adding a new line inside the "textarea," the text would overflow outside instead of pushing upwards as desired. Similarly, deleting a line didn't update the element's height properly.

View the full code on Codepen

CSS

html, body { height: 100%; }
body {
  background: lightgrey;
  display: flex;
  flex-direction: column;
}

*:focus { outline: 0; }

.app-title {
  background: cornflowerblue;
  padding: 20px;
}

.tchat-container {
  width: 300px;
  padding: 20px 0 20px 20px;
  margin: 30px auto;
  background-color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);
}

section {
    overflow-y: auto;
    overflow-x: hidden;
}

article {
  background: #FDD835;
  border-radius: 6px;
  padding: 5px;
  margin: 5px;
}

.tchat-footer {
  display: flex;
  padding-right: 20px;
  margin-top: 20px;
  border: 1px dashed;
}

.autoExpand {
  flex: 1;
  margin: 0;
  background-color: white;
  border: 1px solid lightgrey;

  padding: 10px;
  border-radius: 6px;
}

.autoExpand:focus {
  border-color: cornflowerblue;
}

button {
  height: 60px;
  width: 60px;
  border-radius: 50%;
  margin-left: 20px;
  border: none;
}

.app-footer {
  padding: 20px;
  text-align: center;
  background: black;
  opacity: 0.8;
  color: white;
}

HTML

<header class='app-title'>APP TITLE</header>
<div class='tchat-container'>
  <header>Title</header>
  <section>
    (list of articles here)
  </section>
  <footer class='tchat-footer'>
    <p contenteditable='true' class='autoExpand' placeholder='Auto-Expanding Textarea'></p>
    <button>Send</button>
  </footer>
</div>
<footer class='app-footer'>APP FOOTER</footer>

An ideal CSS-only solution proved to be elusive as I couldn't make Chrome, Firefox, and Edge (latest versions) happy.

UPDATE
I finally identified what was missing in my setup:
the class tchat-footer lacked flex: 1 0 auto,
and additionally, a min-height for .autoExpand.
Check out the updated Codepen

Answer №1

Here is a helpful snippet of code:

// This code snippet applies globally to all textareas with the "autoExpand" class
  const autoExpandingTextAreas = document.querySelectorAll('.autoExpand')

  for (let aeta of autoExpandingTextAreas) {
    aeta.addEventListener('input', event => {
      console.log(
          'height', event.currentTarget.style.height,
          '\nscrollHeight', event.currentTarget.scrollHeight
        )
        // event.currentTarget.style.height = event.currentTarget.scrollHeight + 'px'
    })
  }
html, body {
  height: 100%;
}

body {
  background: lightgrey;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
}

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

*:focus {
  outline: 0;
}
  
<!-- more CSS styles here -->
      
<header class='app-title'>APP TITLE</header>
  <div class='tchat-container'>
    <header>Title</header>
    <section>
      <article>Something very long to test content wrapping and overflow...</article>
      <!-- more articles here -->
    </section>
    <footer class='tchat-footer'>
      <p contenteditable='true' class='autoExpand' placeholder='Auto-Expanding Textarea'></p>
      <button>Send</button>
    </footer>
  </div>
  <footer class='app-footer'>APP FOOTER</footer>

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

Trying to connect a webpage to my CSS file

Lately, I've been diving into building a new website. It's still in its early stages and unfortunately, I'm struggling to get the style.css file to cooperate. Despite my best efforts scouring the internet for fixes, I haven't had much l ...

Issue encountered: Compilation error occurred following the update from Angular 11 to Angular 15, stemming from the module build failure within the sass-loader directory

I recently attempted to update angular from version 11 to 15, but encountered an error as shown in the screenshot below ./src/assets/styles.scss - Error: Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: expected "(". ...

What is the best way to ensure that the content on my webpage stays below the navbar, no matter the height of the

I've been working on my homepage layout and encountered an issue with positioning the content below the navbar, which is centered on the page. Every time I adjust the window height, the content shifts. How can I ensure that it stays fixed below the na ...

Experiencing difficulty aligning the Material UI grid to float on the right side

I'm currently in the learning phase of material-ui and encountering an issue with floating the grid content to the right side. Despite trying alignItems="flex-start" justify="flex-end" direction="row" in the container grid, as well as using the css p ...

Creating a banner using four grid elements, with each element containing four child elements, is a simple process that can

Can you assist me in recreating my idea from an image? I am having trouble understanding how to select and place other elements, and then return them to their original positions after deselecting... I attempted to use a grid but it did not work properly. ...

Ways to position an element at the edge of the browser either on the left or right side when the image is not in a centered container

Looking to create a unique layout that involves: Utilizing a three-column structure Incorporating a div element that spans two columns Positioning an image within the two-column div so that it extends to the left edge of the browser window while staying ...

Display the title of an image beneath the image using CSS

Since upgrading my CMS, I've encountered an issue where the captions of images are not showing below the image for thousands of articles. For example: <img src="123.jpg" alt="Texttext" class="caption" style="disp ...

Issue with custom select functionality on Internet Explorer 9

Having some issues with a select element in IE9. The links that should open the wiki page are not functioning properly only on IE9, and there is also an issue with the hover effect - the icon gets overridden by the background color when hovering over help ...

The mobile functionality of the stylesheet is not functioning properly

Recently, I came across an issue with a stylesheet designed for mobile devices that contains the following code: /* Smartphones (portrait) ----------- */ @media only screen and (max-width : 320px) { /* Styles */ } Surprisingly, this code failed to work ...

Get the Bootstrap download that includes CSS files rather than the LESS files

What is the reason behind bootstrap switching to only having LESS files instead of the traditional bootstrap.css file? Is there a way to download the bootstrap framework that includes the CSS files instead of just the LESS files? ...

Optimizing CSS across various pages and screens sizes with critical importance

My CSS file is in need of optimization for quicker loading times. It contains numerous components utilized across various pages (such as the start page, category pages, and detail pages) on different screen sizes (mobile, tablet, desktop). Manual optimizat ...

Arranging a Vertical Element Within a Rotated Holder

Who would have thought that geometry would come into play when working with CSS? I need help figuring out how to perfectly cover a rotated container with an upright background image. The goal is to hide the rotation on the sides and maintain dynamic contro ...

Arrange divs in a stack fashion

Currently, I have a table-styled three-column set of divs. I would like these columns to stack vertically on mobile devices. I think this can be achieved using the float method, but the titles and descriptions for each column are in separate rows (divs), ...

Flex just can't seem to align correctly

I'm having trouble getting this to work properly. I am using React and styled components, but the justify/content alignment is not functioning as expected. The setup includes a div with flex properties, where the flex direction is set to column. With ...

Vertical centering issue with div specifically in Safari

I am facing an issue with centering a nested div containing a UL within another div. Despite trying various methods to achieve vertical alignment, I am unable to get it to work properly. PARENT .splash { background: url(../img/splash.jpg); backgro ...

Tips on displaying the number of items ordered above the cart icon

Hey there, I've been attempting to place a number in the top left corner of a cart icon without success. My goal is to achieve a result similar to the image shown here: enter image description here Here is the code snippet I've been using: < ...

What are the steps for positioning tables using HTML?

As a newcomer to this field (literally just started two days ago), I have managed to generate tables from an XML file using my XSL file. However, aligning these tables properly has become a bit of a challenge. I initially used the align attribute of <ta ...

Sprite hover image in IE9 not aligned properly or blurry by 1 pixel

I have implemented a sprite to display my button image and a slightly darker hover image. Below is the CSS code I am using, with my image being 31px tall: a { background-position: 0 0; } a:hover { background-position: 0 -31px; } Although the ho ...

Navigate to the homepage section using a smooth jQuery animation

I am looking to implement a scrolling feature on the homepage section using jquery. The challenge is that I want the scrolling to work regardless of the page I am currently on. Here is the HTML code snippet: <ul> <li class="nav-item active"> ...

Steps for adjusting an image in a Bootstrap Carousel

Currently tackling a simple bootstrap Carousel for a school project. I've adjusted the max-height of the images to 400px but it seems like only the top part of the images is visible. I attempted to adjust the margins on the image, and even thought abo ...