17

I am currently trying to justify text in a textarea, unfortunately the CSS:

text-align: justify;

Doesn't work on the text like center, left and right do. I've tried this in both Firefox 3 and IE 7 with no luck.

Is there any way around this?

Matt
  • 494
  • 1
  • 5
  • 13

7 Answers7

23

I dealt with same issue and found out very stupid solution. Make sure that the text to be displayed falls within the start and end tag elements in the same line and not in the next line

<textarea  name="description" readonly="readonly" rows="4" cols="66">Text aligned toward left</textarea>

and not like

<textarea  name="description" readonly="readonly" rows="4" cols="66">
Text aligned toward left
</textarea>
shweta
  • 231
  • 2
  • 3
20

Depending on your target browser... this solution works in Chrome. It does not work work in Firefox however... but I'll post it anyway.

In addition to setting text-align: justify, you must also set white-space: normal.

    textarea {
        text-align: justify;
        white-space: normal;
    }

JSFIDDLE: http://jsfiddle.net/cb5JN/

Norman Breau
  • 201
  • 2
  • 2
  • 1
    However, adding "white-space: normal" causes IE to insert spaces instead of new lines when you press enter! – m6k Aug 01 '14 at 19:25
  • 1
    In order to allow to show the new lines you must use **white-space: pre-line;**. See also [here](https://developer.mozilla.org/it/docs/Web/CSS/white-space). – Paolo Gibellini Mar 08 '16 at 13:30
  • 1
    Now it works without the white-space property as well. – tibotka Nov 30 '20 at 12:47
3

For me (in Firefox), this code works perfectly:

textarea{
    resize: none;
    text-align: justify;
    white-space: pre-line;
    -moz-text-align-last: left;
    text-align-last: left;
}
Olivier
  • 31
  • 2
2

I believe that common practice is to use the TEXTAREA for input without worying about justification; and then, once the input is processed (i.e. the FORM is submitted, or an event of the TEXTAREA is captured), the contents are displayed in a non-editable text element (such as P, SPAN, TD) where the text-align: justify; style attribute will be honored.

Ken Paul
  • 5,685
  • 2
  • 30
  • 33
  • I agree, the client is after this as part of the design though, so unfortunately that that's not appropriate for the situation. Thanks though. – Matt Jan 09 '09 at 04:27
  • Then I think the simple answer is NO. A Google search on "textarea justify" shows lots of similar questions with no usable solution except maybe to replace the `TEXTAREA` with a Rich Text widget. – Ken Paul Jan 09 '09 at 04:32
  • Thats what I had come up with as well before asking here unfortunately. Thanks for your help. – Matt Jan 11 '09 at 06:24
1

Using a common div with contenteditable="true" worked in my case. Doesn't work for most mobile browsers though.

<div contenteditable="true">Some content</div>
Edo
  • 3,311
  • 1
  • 24
  • 25
0

It works fine on Chrome, but not on IE.

text-align: justify; white-space: normal;

0

i dont think this is possible in the html textarea element. you might be able to use some sort of wysiwyg editor (editable div). ie. fckeditor

John Boker
  • 82,559
  • 17
  • 97
  • 130
  • Thats what I've found as well trawling through Google search results, just hoping that someone here may know of something I might have missed. Thanks. – Matt Jan 09 '09 at 04:28
  • See also [this](http://stackoverflow.com/questions/25413143/justify-and-center-textarea-text-html-css) answer. – Paolo Gibellini Feb 28 '16 at 23:38