Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I vertically align placeholder text in textarea?

Tags:

html

css

I am trying to vertically align the placeholder text in textarea (textbox). I am using textarea instead of text input because I need to use multiple lines.

.textbox1 { 
  width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
like image 911
user3619057 Avatar asked Mar 24 '16 04:03

user3619057


People also ask

How do you vertically align a placeholder text?

To center it vertically, I multiplied the height of the element to 7.5% and make it line-height. Show activity on this post. Show activity on this post. Use the line-height property to make the placeholder vertically centered.

Can you adjust the alignment of text within a placeholder?

Select the text you want to modify. Select one of the four alignment options in the Paragraph group. The alignment commands align the text within the placeholder or text box it is in, not across the slide.

Can we use placeholder in textarea?

The placeholder attribute of the <textarea> element is used to set a placeholder text in the textarea. A placeholder is considered as a hint, like “Enter you name”, “Enter mobile number”, “Write in 100 words”, etc. Above, text is the hint you want to set for the textarea as a placeholder text.


4 Answers

One option is to use line-height:

.textbox1 { 
    width: 440px;
    height:30px;
    line-height:30px;
}

.textbox1 { 
    width: 440px;
    height:30px;
    line-height:30px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>

You can also use padding to control the position of the text.

Here's an example using padding-top

.textbox1 { 
    width: 440px;
    padding-top:15px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>

UPDATE

Since the requirements include multi-line support, I'd recommend setting the top and bottom padding i.e:

.textbox1 { 
    width: 440px;
    height:6px;
    padding: 30px 5px;
}

.textbox1 { 
    width: 440px;
    height:60px;
    padding: 30px 5px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
like image 60
dev7 Avatar answered Oct 19 '22 01:10

dev7


This works for latest Firefox, IE/Edge, Chrome in pure CSS:

textarea { 
    width: 440px;
    height:600px; /* Note this is the same height as the placeholders line-height */
}

::-moz-placeholder { /* Mozilla Firefox 19+ */
    line-height:600px;
}
::-webkit-input-placeholder { /* Webkit */
    line-height:600px;
}
:-ms-input-placeholder { /* IE */
    line-height:600px;
}

See this fiddle. The key is to set the height of the textarea to the same line-height as the placeholder.

Sadly vertical-align: middle; seems not to be supported yet.

like image 21
Christian Gollhardt Avatar answered Oct 19 '22 02:10

Christian Gollhardt


Instead of using padding-top which will increase the height of the textarea and extra space down use the line-height here is a sample, you can vary the line-height.

textarea::placeholder {
line-height: 90px;

}

you can also use transform property like this:

   textarea::placeholder {
transform: translateY(-20px);

}

like image 38
Sunday Etom Avatar answered Oct 19 '22 02:10

Sunday Etom


Your best bet is to use padding, as line height will not work over multiple lines.

Additionally, make sure to take into account the line height / font size when calculating your padding.

like image 38
typo Avatar answered Oct 19 '22 03:10

typo