Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my textarea higher up than its neighbor?

Tags:

html

css

Picture:

enter image description here

.left {
  border: 1px solid red;
}
textarea {
  border: 1px solid blue;
}
.parent {
  border: 1px solid green;
}
<div class='parent'>
    <span class='left'>
        <span>one</span>
        <span>two</span>
     </span>
     <textarea></textarea>
</div>

Codepen

like image 518
Adam Zerner Avatar asked Sep 05 '15 15:09

Adam Zerner


People also ask

How do I fix the text area size?

To prevent a text field from being resized, you can use the CSS resize property with its "none" value. After it you can use the height and width properties to define a fixed height and width for your <textarea> element.

How set textarea size in HTML?

We can set the size for the text area by using rows and columns. rows*columns will be the size of the text area. For columns, we have to use the cols keyword.

Is textarea block or inline?

<textarea> is a replaced element — it has intrinsic dimensions, like a raster image. By default, its display value is inline-block .


2 Answers

Why is my textarea higher up than its neighbor?

It's not.

Let me explain.

First, a bit of background:

Your span and textarea elements are (by default) inline elements.

Browsers normally provide a little bit of whitespace underneath inline elements for descenders.

To understand descenders...

Look at this line of text. Notice there are no letters that breach the baseline.

Now look at the following sentence:

By just crossing the bridge he probably got away.

Note the letters "y", "j", "p" and "g". These letters breach the baseline and are known in typography as "descenders".

[enter image description here

Source: Wikipedia.org

The gap you're seeing is not margin or padding, but simply the browser providing room to accommodate these lowercase letters. In short, this is called baseline alignment.

baseline

The line upon which most letters "sit" and below which descenders extend.

[enter image description here

Source: Wikipedia.org

So why, somebody might ask, does the browser provide this space for textarea, img, input and other inline elements, that don't need space for descenders?

Because the browser adjusts for the possibility that you may have text before or after one of those elements on the same line.

Now, to your image and code...

On first glance it clearly appears that the textarea is higher up than the span element. But if you take a closer look...

enter image description here

...you'll see they are perfectly aligned. Both span and textarea are providing space for descenders.

enter image description here

The borders you've added contribute to the appearance of misalignment because the textarea border wraps a clearly delineated box while excluding descender space, but the span border wraps the text and the descender space. If you remove the red border the misalignment is less pronounced.

In terms of a solution, here are two options:

  1. Add vertical-align: bottom to the textarea rule, OR
  2. Add display: block to the textarea rule.
like image 166
Michael Benjamin Avatar answered Sep 22 '22 23:09

Michael Benjamin


Adam,

If you add the following to your existing css, you should get your desired results.

.left{
    display:inline-block;
    vertical-align: text-bottom;
}

textarea{
    margin:0px;
    vertical-align: text-bottom;
}

You can see a working example at the following url: https://jsfiddle.net/YOOOEE/z8pwpms6/

like image 45
YOOOEE Avatar answered Sep 20 '22 23:09

YOOOEE