日本語

Fixing textarea Size with CSS resize

​​CSS​Forms,HTML,Playwright

By default, a textarea in a contact form or comment field can be resized by dragging its bottom-right corner. To keep the layout from breaking, set resize: none on the textarea. The corner grip disappears, and dragging no longer changes the size. I also checked resize: vertical, which allows only vertical resizing, and how to set the maximum size with max-height.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I used Playwright to move the mouse, pressed on the bottom-right corner of a textarea, dragged it, and measured the size before and after. I did not test Firefox, Safari, or mobile browsers.

Preventing resizing with resize: none

css
textarea {
  resize: none;
}

In the test environment, the resize value of an unstyled textarea was both. The MDN page for the resize property also says that in many browsers a textarea can be resized by default, and that resize can override this.

I placed three textareas, each 200px wide and 60px high, and dragged the bottom-right corner of each 100px to the right and 70px down.

Three boxes stacked vertically. The top box, labeled "default (unset)" in the Japanese image, grows from 200×60px to 300×130px and has a diagonal grip at the bottom-right. The middle pink box, labeled resize: none, stays at 200×60px and has no grip. The bottom box, labeled resize: vertical, keeps its width of 200px while only its height grows to 130px, and it has a grip at the bottom-right. The labels in the image are in Japanese.
Differences by resize value after the same drag

The unstyled one grew in both width and height. The resize: none one showed no grip at the bottom-right, and it stayed at 200×60px when dragged. The resize: vertical one kept its width and only its height increased.

Allowing vertical or horizontal resizing only

The resize property has values that control the direction of resizing. I dragged a textarea that was 240px wide and 80px high by 120px to the right and 100px down. The results were:

resize Width×height after dragging
Unset (both) 360×180px
none 240×80px (unchanged)
vertical 240×180px (height only)
horizontal 360×80px (width only)
block 240×180px (height only)
inline 360×80px (width only)

block and inline depend on the writing direction of the text. According to the MDN page for the resize property, whether the resizable direction is horizontal or vertical depends on the values of writing-mode and direction. For a horizontal textarea, block controlled the height and inline controlled the width. When I set writing-mode: vertical-rl to make a textarea vertical and applied resize: block, the same drag changed the width (from 240px to 360px), not the height.

For a field where users are likely to enter long text, you might use vertical to let them increase only the height, instead of fixing the size with none.

Setting minimum and maximum sizes for resizing

The CSS specification (CSS Basic User Interface Module Level 4) says that the limits on user resizing are set only by min-width, max-width, min-height, and max-height. So the range within which a user can grow or shrink an element is controlled by these four properties.

css
textarea {
  resize: vertical;
  min-height: 60px;
  max-height: 160px;
}

In the test environment, I dragged this textarea, which was 80px high, 100px down. Its height stopped at 160px. When I dragged it 70px up to shrink it, it stopped at 60px.

If you also allow horizontal resizing, adding max-width: 100% keeps the element from overflowing its parent. I put a 240px-wide textarea with resize: both and max-width: 100% inside a 300px-wide div, and dragged it 120px to the right. Its width stopped at 300px.

The dragged size is written to the style attribute

When a user drags a resize handle, the browser writes the new size into the width and height in the element's style attribute, in px. In the test environment, after I enlarged an unstyled textarea, the style attribute contained width: 360px; height: 180px;. With vertical, which changes only the height, only height was written.

Even when the size stopped at max-height: 160px, the style attribute still contained height: 180px, the position the drag reached. The display showed 160px because the max-height limit was in effect.

After a user has enlarged a textarea, changing resize to none with JavaScript does not restore the original size. The CSS specification also says that changing the resize value does not remove values that user actions have placed in the style attribute. To return to the original size, remove the width and height in the style attribute.

js
textarea.style.removeProperty('width');
textarea.style.removeProperty('height');

In the test environment, this returned the size from 360×180px to the original 240×80px.

What happens when height has !important

I also tested a textarea with no resize setting and height: 80px !important in the stylesheet. When I dragged it 120px to the right and 100px down, its width grew to 360px, but its height stayed at 80px. The style attribute contained height: 180px. The height stayed at 80px because the !important declaration in the stylesheet takes precedence over the style attribute. The CSS specification also says that an !important declaration can make user resizing appear to be ignored.

If you want to prevent users from changing the size, resize: none, which removes the grip itself, is a better fit than forcing the size with !important.

When adding resize to a div, also set overflow

You can apply resize to elements other than textarea. However, as the MDN page for the resize property notes, it has no effect on block elements whose overflow is visible or clip.

In the test environment, when I dragged a div with resize: both, its size did not change while overflow kept its initial value of visible. Adding overflow: auto or overflow: hidden made it grow in both width and height. The default resize value of a div was none.

If you want a textarea to grow automatically as the user types, I cover that in the post "Fitting Input Field Size to Its Content with CSS field-sizing" on this blog.

​Posted by 86