HTML justify text-align with <pre> tag
Asked Answered
A

3

13

I have a problem with the text-align "justify" inside a paragraph with format and fixed width, because I want the paragraph text in HTML to appear totally justified (like it would happen with a MS Word file). However, this does not happen. What I need to modify to achieve it?

     <pre style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: medium; width: 500px; text-align: justify;"> 

I have a problem with the text-align "justify" inside a paragraph with format and fixed width, because I want the paragraph text in HTML to appear totally justified (like it would happen with a MS Word file). However, this does not happen. What I need to modify to achieve it?

        </pre>

EDIT: if I replace the <pre> tag with <p> tag, my code works properly. Can somebody explain me why? Perhaps I am using a wrong use of <pre> tag.

Audacious answered 19/1, 2012 at 1:2 Comment(0)
E
18

To justify text in <pre> you need to add white-space: normal; in your styles, but in my opinion this misses the point using the <pre> tag.

-- Edit --

Instead of <pre> tag you can just use <div> with appropriate styles. It can imitate <pre> tag very well and is easier to play with.

Everson answered 19/1, 2012 at 1:11 Comment(5)
Thank you for your answer. I have tried it but it does not work.Audacious
Why do you need to use the <pre> element?Geosyncline
^ Good point, @jfpeji you can use <div> with appropriate styles. It can imitate <pre> very well.Everson
@MrLister: Thank you for your comment. I realize that I really need the <p> tag instead of the <pre> tag (I thought this latter is used for formatted text).Audacious
@Kobra: Thank you for your comment. It does works. If you edit your answer to include the <div> solution, I would be pleased to accept itAudacious
D
27
  pre {
    text-align: left;
    white-space: pre-line;
  }
Delainedelainey answered 13/3, 2015 at 14:52 Comment(3)
Thanks for the answer Devalli - could you offer some explanation about why this works as well as just the code? Thanks.Monumentalize
w3schools.com/cssref/pr_text_white-space.asp "Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks"Delainedelainey
seems like all I needed was text-align: left, thanks!Spanner
E
18

To justify text in <pre> you need to add white-space: normal; in your styles, but in my opinion this misses the point using the <pre> tag.

-- Edit --

Instead of <pre> tag you can just use <div> with appropriate styles. It can imitate <pre> tag very well and is easier to play with.

Everson answered 19/1, 2012 at 1:11 Comment(5)
Thank you for your answer. I have tried it but it does not work.Audacious
Why do you need to use the <pre> element?Geosyncline
^ Good point, @jfpeji you can use <div> with appropriate styles. It can imitate <pre> very well.Everson
@MrLister: Thank you for your comment. I realize that I really need the <p> tag instead of the <pre> tag (I thought this latter is used for formatted text).Audacious
@Kobra: Thank you for your comment. It does works. If you edit your answer to include the <div> solution, I would be pleased to accept itAudacious
T
1

Use tags:

<pre><code>
Your text
Your text
</code></pre>

Remove all spaces preceding your text, all indentation.

Tropic answered 4/8, 2017 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.