40

I'm trying to justify the text within this p tag so that it perfectly fits the width of the p.

<p align="justify" style="text-align: justify !important; color:#fff; margin:0px; font-weight:bold; width:487px; border:Solid 1px red;">blah blah blah</p>

but the text just wont justify! any idea why?

thanks for any help.

Joris Ooms
  • 11,880
  • 17
  • 67
  • 124
phil
  • 761
  • 3
  • 10
  • 16

12 Answers12

42

You can use the solution described here: http://blog.vjeux.com/2011/css/css-one-line-justify.html

This will justify a single line but adds a space after, so if you know the height, you can specify it with overflow:hidden to conceal it and still get the justification.

.fulljustify {
     text-align:justify;
    }
    .fulljustify:after {
        content: "";
        display: inline-block;
        width: 100%; 
    }
    #tagline {
        height: 80px;
        overflow: hidden;
        line-height: 80px; /* vert-center */
    }
<p id="tagline" class="fulljustify">Blah blah blah</p>
sdgluck
  • 24,894
  • 8
  • 75
  • 90
manafire
  • 5,984
  • 4
  • 43
  • 53
38

If your text doesn't span more than one line, justifying doesn't do anything. Your text has to wrap to the next line, and then the FIRST line will be justified, but not the second.

Brett
  • 4,051
  • 2
  • 26
  • 39
  • 2
    darn. so theres no css way to justify 4 words in a 487 width p? – phil Jun 03 '11 at 14:31
  • is correct. You also needed to change the `color` from white on a white background to see this. http://jsfiddle.net/5RpQr/ – Jason Gennaro Jun 03 '11 at 14:34
  • @jason gennaro - yeah, i spent a minute trying to figure out why i didn't see anything in the fiddle... haha – Brett Jun 03 '11 at 14:35
  • .haha @phil, see my answer for a possible other solution re justifying four words – Jason Gennaro Jun 03 '11 at 14:40
  • Wow, that's really confusing, e.g. when in ckeditor setting the first line to justify, you won't see any changes. I wasted an hour figuring that out, thinking the plugin was broken. Hah! – Michiel Cornille Feb 03 '14 at 09:57
13

Chrome doesn't support it but in Firefox and IE, you can use text-align-last: justify;. For a cross-browser solution, we have to use what @onemanarmy posted ;)

alijsh
  • 842
  • 7
  • 7
9

If you wanted to justify four words in 487px you could try using word-spacing in your css.

I used word-spacing:8em; for bla bla bla bla but you could adjust as necessary.

http://jsfiddle.net/5RpQr/1/

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
7

try this

for div

div {
text-align:justify;
text-justify: inter-word;
text-align-last:center;
/* for IE9 */
-ms-text-align-last:center;
}
venkat
  • 449
  • 4
  • 17
  • thanks, you saved my bacon. I love how these CSS styles were created without any planning, by pushing it forward with the belly. – Duck Sep 20 '17 at 21:49
0

There is also something similar, like display: flex; justify-content: space-around; if you would wrap those texts in spans or divs

WoodyDRN
  • 1,221
  • 20
  • 26
0

In my case for < p > tag, works with easy way:

p {
  text-align: justify;
  text-justify: inter-word;
}

https://css-tricks.com/almanac/properties/t/text-justify/

0

To make it look good on Chrome & opera (multiline justify looks bad on them)

I use the following

.fulljustify {
  text-align: justify;
  display: table-row;
  text-align-last: left;
}
Waelmas
  • 1,894
  • 1
  • 9
  • 19
0

Chrome solution: If you don't want to mess with the display properties (in my case aligning an anchor tag of a carousel img):

text-align: -webkit-center;

kfederer
  • 3
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 09 '22 at 21:20
0

It worked for me this way:

<div className={s.element}> {text} </div>

.element {
  text-align:justify;
  word-wrap: break-word;
  hyphens: auto;
}
Gean Rt
  • 1
  • 1
-4

You better try

style="text-align:justifty;display:inline-block;"
-5

Just use style="text-align:justify".
It works in all browsers.

jenzz
  • 7,239
  • 6
  • 49
  • 69
  • 4
    some kind of explanation would be helpful, won't it? he obiviously used your styles... – Ron Jan 26 '13 at 09:29
  • @Ron If you want to justify whole paragraph except last row use above trick, and if you want to justify last line also then use ` .justify:after{content: ""; display: inline-block; width: 100%;}` and no need to define height. – S.K. Dhanda Feb 01 '13 at 16:03
  • he already use the inline css with `text-align: justify` , Please reconsider to give op solution. – Adi Prasetyo Jun 09 '17 at 07:17