Search code examples
cssjustifytext-align

Justify the last line of a div?


I don't think the "why?" of this question is important...but what I need to do is to text-align:justify the last line of text from a DIV. Normally, the last line (or the first if there are no other lines, which is the current case) of a div isn't justified, but aligned left. I know it might not make sense at all, but I absolutely need the last line to be justified!


Solution

  • Here's a cross-browser method that works in IE6+

    It combines text-align-last: justify; which is supported by IE and a variation of the :after pseudo-content method. It includes a fix to remove extra space added after one line text elements.

    CSS:

    p, h1{
       text-align: justify;
       text-align-last: justify;
    }
    
    p:after, h1:after{
       content: "";
       display: inline-block;
       width: 100%;
    }
    

    If you have one line of text, use this to prevent the blank line the above CSS will cause

    h1{
       text-align: justify;
       text-align-last: justify;
       height: 1em;
       line-height: 1;
    }
    
    h1:after{
       content: "";
       display: inline-block;
       width: 100%;
    }
    

    More details: http://kristinlbradley.wordpress.com/2011/09/15/cross-browser-css-justify-last-line-paragraph-text/