Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Justify and Center <textarea> text HTML/CSS?

I have a textarea and I want it to be justified so all the lines are equal in width and to be centered to the text stays in the middle of the textarea when it's not at maximum line length.

This is my textarea:

<textarea class="Whiteboard" type="text" placeholder="Type something..."></textarea>

...and the CSS:

textarea.Whiteboard{
    resize: none;
    background-color: #F1F1F1;
    border: none;
    height: 500px;
    width: 800px;
    text-align: center;
    font-size: 50px;
}

Thank you all!

like image 718
James Hewitt Avatar asked Aug 20 '14 19:08

James Hewitt


People also ask

How do I center align textarea in CSS?

@Chris -- just add text-align:center; to your current <div> 's css declaration then. :-) You can add all styling to the same div (color, font-family, text-align,...) or isn't this what you mean?

How do you justify text in textarea?

In addition to setting text-align: justify, you must also set white-space: normal. However, adding "white-space: normal" causes IE to insert spaces instead of new lines when you press enter! In order to allow to show the new lines you must use white-space: pre-line;.

How do you align text Center and justify in HTML?

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property text-align for the center, left and right alignment.

How do you center text in text area in HTML?

To center text using HTML, you can use the <center> tag or use a CSS property.


2 Answers

Unfortunately there is no concrete CSS solution at the time of writing to achieve the desired result.

However CSS level 3 has introduced a feature under the name text-align-last to handle the alignment of the last line of a block:

7.3 Last Line Alignment: the text-align-last property

This property describes how the last line of a block or a line right before a forced line break is aligned.

But it is still in Working Draft state. Hence the browser support is not good enough to rely on this technology (It's still buggy on Chrome 35 and only works on Firefox 12+).

Here is an example which I'm not able to verify (because of my FF4. Yes! shame on me):

textarea {
    white-space: normal;
    text-align: justify;
    -moz-text-align-last: center; /* Firefox 12+ */
    text-align-last: center;
}
like image 185
Hashem Qolami Avatar answered Sep 18 '22 18:09

Hashem Qolami


Alternative: use contenteditable

.container {
  width: 100px;
  height: 150px;
  background: #eee;
  display:flex;
  align-items: center;
  justify-content: center;
}

.text {
  text-align: center;
  word-break: break-all;
  margin: 10px;
}
<div class="container">
  <div class="text" contenteditable=true>click here and edit</div>
</div>
like image 34
Kamil Kiełczewski Avatar answered Sep 20 '22 18:09

Kamil Kiełczewski