How can I center-justify text?
Currently, justify does not center the last line.
Solution (not the best, but still working for some cases) for non-dinamic text with fixed width.Usefull for situations when there are a little space to "stretch" text to the end of the penultimate line. Make some symbols in the end of the paragraph (experiment with their length) and hide it; apply to the paragraph absolute position or just correct free space with padding/marging.
Good compabitity/crossbrowser way for center-justifying text.
Example (paragraph before):
.paragraph {
width:455px;
text-align:justify;
}
.center{
display:block;
text-align:center;
margin-top:-17px;
}
<div class="paragraph">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna,<br><center>vel scelerisque nisl consectetur et.</center></div>
And after the fix:
.paragraph {
width:455px;
text-align:justify;
position:relative;
}
.center{
display:block;
text-align:center;
margin-top:-17px;
}
.paragraph b{
opacity:0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-moz-opacity: 0;
-khtml-opacity: 0;
}
<div class="paragraph">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, <b>__</b><br><div class="center">vel scelerisque nisl consectetur et.</div></div>
For people looking for getting text that is both centered and justified, the following should work:
<div class="center-justified">...lots and lots of text...</div>
With the following CSS rule (adjust the width
property as needed):
.center-justified {
text-align: justify;
margin: 0 auto;
width: 30em;
}
Here's the live demo.
text-align: justify;
makes sure the text fills the full width of the div
it is enclosed in.margin: 0 auto;
is actually a shorthand for four rules:
margin-top
and margin-bottom
rules.
The whole thing therefore means margin-top: 0; margin-bottom: 0
, i.e. no margins above or below the div
.margin-left
and margin-right
rules.
So this rule results in margin-left: auto; margin-right: auto
.
This is the clever bit: it tells the browser to take whatever space is available on the sides and distribute it evenly on left and right.
The result is centered text.width: 30em;
, which limits the width of the div
.
Only when the width is restricted is there some whitespace left over for margin: auto
to distribute.
Without this rule the div
would take up all available horizontal space, and you'd lose the centering effect.its working with this code
text-align: justify; text-align-last: center;
Most of the solutions here don't take into account any kind of responsive text box.
The amount of text on the last line of the paragraph is dictated by the size of the viewers browser, and so it becomes very difficult.
I think in short, if you want any kind of browser/mobile responsiveness, this isn't possible :(