Kniffen Web Design

Text Align Justify

CSS only allows you to text-align center, left, right or justify. Occassionaly though you may find yourself wanting to implement a variation of justify.

There are four variations of justified text:

  • left justify - last line of text is left aligned
  • right justify - last line of text is right aligned
  • center justify - last line of text is centered
  • full justify - last line of text is spread over full width

In CSS when you set text-align:justify the browser uses left justify

Unfortunatly the only way I have found to implement these variations is to hard code what the last line in a paragraph is and wrap the text with an element. Also I have been unable to create the full justify behavior.

<style type="text/css"> 
    .left-justify,
    .right-justify,
    .center-justify
    {
        text-align: justify;
    }

    .last-line {
        display: inline-block;
        width: 100%;    
    }

    .left-justify .last-line {
        // nothing needs to be done because this is default behavior anyway.
    }

    .right-justify .last-line {
        text-align: right;
    }

    .center-justify .last-line{
        text-align: center;
    }
</style>
<div style="width:300px;">
    <p class="left-justify">
        Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies
        <span class="last-line">mi vitae est. Mauris placerat eleifend leo.</span>
    </p>

    <p class="right-justify">
        Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies
        <span class="last-line">mi vitae est. Mauris placerat eleifend leo.</span>
    </p>

    <p class="center-justify">
        Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies
        <span class="last-line">mi vitae est. Mauris placerat eleifend leo.</span>
    </p>
</div>
Comments powered by Disqus