Justify Text in a HTML/XHTML TextArea
Asked Answered
G

7

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?

Goatskin answered 9/1, 2009 at 4:7 Comment(0)
F
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

Fortran answered 9/1, 2009 at 4:22 Comment(2)
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.Goatskin
See also this answer.Auctioneer
E
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>
Escuage answered 9/3, 2011 at 20:8 Comment(3)
inded there is such issue with the <textarea>Ruthenic
very weird how it works when all code textarea code is on the same line! horrible to debug! thanks, reputation++Soraya
dear friends... did you test this before commenting !!! Not working for me in HTML5Laborious
F
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/

Fury answered 13/1, 2014 at 17:45 Comment(3)
However, adding "white-space: normal" causes IE to insert spaces instead of new lines when you press enter!Clegg
In order to allow to show the new lines you must use white-space: pre-line;. See also here.Auctioneer
Now it works without the white-space property as well.Confront
N
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;
}
Nival answered 3/4, 2016 at 1:10 Comment(0)
S
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.

Sapwood answered 9/1, 2009 at 4:17 Comment(3)
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.Goatskin
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.Sapwood
Thats what I had come up with as well before asking here unfortunately. Thanks for your help.Goatskin
M
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>
Musgrave answered 28/2, 2012 at 19:21 Comment(0)
F
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

Fortran answered 9/1, 2009 at 4:22 Comment(2)
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.Goatskin
See also this answer.Auctioneer
T
0

It works fine on Chrome, but not on IE.

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

Telangiectasis answered 13/11, 2014 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.