简体   繁体   English

无法调整文本区域高度

[英]Unable to resize the textarea height

I've noticed a strange bug while using a textarea in a my project.在我的项目中使用textarea时,我注意到一个奇怪的错误。
At first I thought there was a problem in my code but then I was able to replicate that bug extracting the essenial in a JsFiddle .起初我认为我的代码有问题,但后来我能够复制那个错误,提取JsFiddle中的本质。

The problem is:问题是:
If I change the height of the textarea by code without any interactions from the user before, the height get stuck at that size.如果我在之前没有与用户进行任何交互的情况下通过代码更改textarea的高度,则高度会停留在该大小。
For example, if you click to the button "Change size" (in the jsfiddle above) and then try to resize back to the initial size using the element grip (at the bottom-right corner), you can't.例如,如果您单击“更改大小”按钮(在上面的 jsfiddle 中),然后尝试使用元素夹点(在右下角)将大小调整回初始大小,则不能。 It's like if that new height is the new ''minimum allowed size'' of the textarea .就像那个新高度是textarea的新“最小允许大小”一样。


What am I doing wrong?我究竟做错了什么? what am I missing?我错过了什么?
Thank you in advance.先感谢您。

Height overrides min-height in Chrome.高度会覆盖 Chrome 中的最小高度。

In older versions of chrome there was no restriction.在旧版本的 chrome 中没有限制。

So if you use height min-height will be your height.因此,如果您使用 height min-height 将是您的身高。 So you need to set min-height and max-height only.所以你只需要设置 min-height 和 max-height 。 Height overrides min-height in Chrome.高度会覆盖 Chrome 中的最小高度。

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

Okay this solution works for me ( chrome Version 47.0.2526.111 m ): https://jsfiddle.net/ezsz8xr5/9/好的,这个解决方案对我有用( chrome 版本 47.0.2526.111 m ): https ://jsfiddle.net/ezsz8xr5/9/

I found this interestining link: https://code.google.com/p/chromium/issues/detail?id=94583我发现了这个有趣的链接: https ://code.google.com/p/chromium/issues/detail?id=94583

Seems that it is a known issue.似乎这是一个已知问题。

I know this question has been asked a while ago, but there's no good answer to it, and I found a solution.我知道这个问题已经被问过一段时间了,但是没有很好的答案,我找到了解决方案。

What I found is that putting a % height for the textarea will not change it according to its parent.我发现为 textarea 放置 % 高度不会根据其父级更改它。

If you put the rule resize: vertical , changing manually the height you'll notice that your browser will put the style.height in pixels .如果您设置规则resize: vertical ,手动更改高度,您会注意到您的浏览器会将 style.height 以像素为单位。

So I tried putting height: 150px and it worked.所以我试着把height: 150px和它的工作。

So here are two solutions :所以这里有两个解决方案:

  1. First, put your textarea's height in pixels , not in percentage.首先,将 textarea 的高度以像素为单位,而不是百分比。
parent {
    width: 150px;
}

parent textarea {
    height: 100%; /* not working ! */
}

/* Instead, do : */

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

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

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

For a SCSS way :对于 SCSS 方式:

parent {
    $height: 150px;

    height: $height;

    textarea {
        height: $height;
    }
}
  1. Second, declare the textarea parent's display as flex , and without any additional rule, it will automatically resize your textarea.其次,将 textarea 父级的 display 声明为flex ,并且无需任何附加规则,它会自动调整您的 textarea 大小。 If it does not, then do this :如果没有,请执行以下操作:
parent {
    display: flex;
    /* if flex doesn't work by itself, add this + textarea rule : */
    flex-flow: column nowrap;
}

parent textarea {
    flex: 1; /* tells the textarea to fit all the free space it finds */
}

It seems like a browser issue yes, still not fixed in 2022, sadly.很遗憾,这似乎是一个浏览器问题,但在 2022 年仍未解决。 If it is intentional, then it's even not documented.如果是故意的,那么它甚至没有记录在案。

Hope this will help someone in the future.希望这对将来的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM