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!
@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?
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;.
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.
To center text using HTML, you can use the <center> tag or use a CSS property.
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;
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With