Having difficulty adjusting the height of the textarea

I encountered an unusual bug while working on a project involving a textarea. Initially, I suspected an error in my code. However, I was able to reproduce the bug by simplifying it in a JsFiddle.

The issue is as follows:
When I programmatically change the height of the textarea without any user interaction beforehand, the height remains fixed at that size. For instance, if you click on the "Change size" button (in the provided jsfiddle) and then attempt to resize it back to its original size using the bottom-right corner grip, you are unable to do so. It seems like the new height becomes the new ''minimum allowed size'' of the textarea.


What am I missing here? What could be the mistake I am making?
Thank you for your assistance.

Answer №1

Height takes precedence over min-height in Chrome.

In previous versions of Chrome, there were no restrictions.

Therefore, if you specify a height, it will override the min-height property. It is recommended to set min-height and max-height values only. In Chrome, height overrides min-height.

textarea{
  max-height: auto;
  min-height: 50px;
  resize: both;
}

This solution works for me on Chrome Version 47.0.2526.111: https://jsfiddle.net/ezsz8xr5/9/

I came across this interesting link: https://code.google.com/p/chromium/issues/detail?id=94583

It appears to be a known issue.

Answer №2

While this question may have been asked previously without a satisfactory answer, I have stumbled upon a solution that seems to work.

Upon experimenting, I discovered that setting a percentage height for the textarea does not adjust according to its parent container.

By using the CSS rule resize: vertical, you can manually adjust the height, but your browser will interpret it in pixels.

My workaround involved simply setting height: 150px, which successfully resolved the issue.

Here are two potential solutions:

  1. To avoid sizing issues, specify the height of the textarea in pixels rather than percentages.
parent {
    width: 150px;
}

parent textarea {
    height: 100%; /* won't work as expected */
}

/* Instead, use : */

/* css variable */
:host {
    --height: 150px;
}

parent {
    height: var(--height);
}

parent textarea {
    height: var(--height);
}

If you prefer a SCSS approach:

parent {
    $height: 150px;

    height: $height;

    textarea {
        height: $height;
    }
}
  1. Another method involves declaring the textarea parent's display property as flex. By default, this should automatically resize the textarea. If not, try the following:
parent {
    display: flex;
    /* if flex alone doesn't work, include this + textarea rule : */
    flex-flow: column nowrap;
}

parent textarea {
    flex: 1; /* instructs the textarea to occupy all available space */
}

It appears that this behavior could be browser-dependent and remains unresolved as of 2022. Unfortunately, the reason behind it is not clearly documented.

I hope this information proves useful to someone facing similar challenges in the future.

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

What steps can I take to modify the peeking effect from hover to scrolling?

I have successfully integrated a peeking effect on my website. Check it out here: http://jsfiddle.net/7yrWL/1/ Currently, the effect triggers when I hover over the image. What I want now is for the peeking effect to only activate when the user scrolls to t ...

Modify CSS image according to the user interface language in asp.net

Is there a way to dynamically change the image based on different cultures in my ASP.NET webpage? I have successfully been able to switch strings using a resource file, but I am unsure how to handle images. Currently, I have an A tag with a specific clas ...

utilizing React.js, learn how to extract the most recent user input and store it within an array

My Input component generates input tags dynamically based on JSON data. I've implemented the onChange method in the input tag, which triggers a function called "handleChange" using contextAPI to record the values in another component. The issue aris ...

How can we create and improve the visibility of the navigation for the UIKit slider

Is there a way to keep the navigation arrows of the Uikit slider always visible or add navigation circles below the slider? Your help is greatly appreciated. ...

Angular asynchronous request does not yield any results

After executing ngOnChanges, the method _generateLocationFormForApproval is called, resulting in the output this.absoluteUri. Following this, _pageEventDealsForApprovalList is invoked. However, when trying to access the value of this.absoluteUri inside _pa ...

Is there a way to ensure that the function within the 'for loop' is executed prior to sending the response in NodeJS?

Utilizing bluebird for this task, my main goal is to ensure that the function `get_like_status(email, results[i].id)` located at the end of the for loop runs before sending out the headers. Currently, I am encountering an issue where the array list retur ...

What is the best way to add a new project and make updates to an existing project within an array?

Looking to add a new project and update existing projects within an array. Here's my plan in pseudo-JSON format. How can I update Project A in MongoDB? { _id : ..., userName: milad , projets : [ { .. projectId, pName, location .... A }, ...

Dynamically altering the CSS4 variables in real time

I am facing the challenge of managing multiple CSS4 variables, including primary, for various companies. How can I dynamically change the primary CSS4 variable color based on the company? Note: My specific requirement is to update the primary variable glo ...

Receiving a 401 error code stating "unauthorized" when attempting to invoke a method using AJAX in .NET

I am working on a .NET project within the framework 4.5.1 When I incorporate ajax as shown below, the debugger successfully hits the page_load event: $.ajax({ "async": true, "crossDomain": true, type: "POST", url: "Advance ...

Is it a common issue for links to not work on the first click after ajax loads the page?

I've implemented a search box drop-down menu, and everything seems to be working well. However, I've noticed that the links are not functioning properly on the first click. I have to click somewhere else on the body before I can click on the link ...

Is there a way to conceal form fields for city and state until the zip code is provided?

Is there a way to only display the city and state form fields after the zip field has been filled out? I have a geo-based program that automatically fills in the city and state based on the zip code entered, but I want to hide these fields until the data ...

Troubleshooting: jQuery's clone( true ) function fails to function properly with dynamic

Consider this scenario: $.fn.foo = function() { var $input = $('<input type="text"/>'); var $button_one = $('<button>One</button>'); var $button_two = $('<button>Two</button>'); ...

Retrieving information from various datasets through inquiry

First Model const mongoose = require("mongoose"); const finalApprovalSchema = mongoose.Schema({ formId: String, designApproval: String, rejectionReason: String, date: { type: Date, default: Date.now, }, }); const FinalApproval ...

Why is it that when I refresh the page in Angular, my logged-in user is not being recognized? What could be causing this

I am developing a Single Page Application using the combination of Angular JS and Node JS. In my HTML file, I have defined two divs and based on certain conditions, I want to display one of them using ng-if. However, I noticed that the model value used in ...

Terminating a function during execution in JavaScript/TypeScript

Currently, I am in the process of developing a VSCODE extension using TypeScript. Within this extension, there is a particularly large function that is frequently called, but only the final call holds significance. As a solution, I am attempting to elimina ...

Troubles with getting Bootstrap dropdown to function on Ruby on Rails platform

I'm struggling to make the bootstrap dropdown feature work on my website. I have bootstrap installed correctly, but for some reason, the dropdown functionality is not working. I suspect it might be related to jquery, although jquery seems to be functi ...

Adjusting the appearance of a JavaScript element based on its hierarchy level

Currently, I am utilizing Jqtree to create a drag and drop tree structure. My main goal is to customize the appearance of the tree based on different node levels. Javascript Tree Structure var data = [ { name: 'node1', id: 1, chi ...

A tutorial on implementing a "Back to Top" button that appears dynamically while scrolling in Angular

I've developed a scroll-to-top feature for my Angular project, but I'm facing an issue. The scroll icon appears immediately upon page load instead of only showing after the user scrolls down. Any ideas or suggestions on how to achieve this? Here ...

Calculating the total percentage and total number with a specific subject can be done effectively using the reduce method

I have a special function called subjectTotal that calculates the total score for a subject based on multiple choice and true/false questions. I am attempting to incorporate a percentage calculation directly within the subjectTotal function. Additionally, ...

Learn the step-by-step process of clicking on a button to modify its properties and deactivate it

I could really use some assistance. I'm trying to make a button: <v-btn dark color="primary" class="square">Tile 1</v-btn> Is there a way I can modify it so that when clicked, it changes to flat, becomes disabled, and switches its color ...