Extra space under textarea, differs along browsers
Asked Answered
A

3

109

There`s some extra space under textarea tag. From 1 to 4 pixels in different browsers. The markup is very simple:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
    <head>
        <style>
            body {
                margin: 0;
                padding: 0;
            }
            .main {
                background-color: red;
            }
            textarea {
                background-color: gray;
                resize: none;
                margin: 0;
                border: 0 none;
                padding: 10px;
                height: 50px;
                overflow: hidden;
            }
        </style>
    </head>
    <body>
        <div class="main">
            <textarea></textarea>
        </div>
    </body>
</html>

Here's how it is rendered in browsers:

Screenshot

Why is this happening? How to remove this extra space?

Americanize answered 22/8, 2011 at 8:33 Comment(0)
A
262

Add vertical-align: top to textarea.

The reason for the gap is that textarea is an inline (or inline-block) element, and the gap is the space reserved for descenders in text. I don't know exactly why the gap is different between different browsers.

Artifice answered 22/8, 2011 at 8:43 Comment(6)
Have my children – Pannonia
It always feels so validating when a couple hundred other people have the same problem as me, 😁. – Sidman
Ten years later. This is still a thing. – Orpah
This solved my problem, but after that the textarea's bottom was too close to the next textarea bellow, so I fixed it adding "margin-bottom: 1px;". – Bed
@thirtydot, why not bottom and middle? – Adult
@Pacerier: Any of them work, I probably picked the shortest one. – Artifice
W
29

In my case, thirtydot's answer didn't work well with the parent <div>'s bottom border.

display: block suited me nicely though.

Wimbush answered 6/11, 2015 at 3:20 Comment(1)
This worked well for me, too. – Breana
S
2

I also found that the space goes away if the textarea's parent is using display:flex:

/* The relevant part: */
#FlexLayout { display: flex; flex-direction: column; }

/* The boring part: */
.ShowChildBorders * { border: 1px solid; }
#DefaultLayout * { border-color: red; }
#FlexLayout * { border-color: green; }
#SideBySide { display: flex; }
#SideBySide > div { flex: 1; margin: 4px; }
#SideBySide * { margin: 0; padding: 0; }
<div id="SideBySide">
  <div class="ShowChildBorders">
    <div id="DefaultLayout">
      <div>Default Layout</div>
      <textarea>Text Area</textarea><br/>
      <textarea>Text Area</textarea>
    </div>
  </div>
  <div class="ShowChildBorders">
    <div id="FlexLayout">
      <div>Flexbox Layout</div>
      <textarea>Text Area</textarea>
      <textarea>Text Area</textarea>
    </div>
  </div>
</div>

On Chrome 91.0.4472.77, Windows 10 64-bit, that renders as:

enter image description here
Sidman answered 4/6, 2021 at 22:47 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.