Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 424
JCOC611 Avatar asked Jan 22 '11 23:01

JCOC611


People also ask

How do I justify text in a div?

Using the Justify-Content Property Instead, you have to use the justify-content property and define it with the value "center." Let's create the same div element with a yellow border around it as above. In your HTML, you'd give the div the class name "center" and use the same class selector to style it with CSS.

How do I change alignment to last line of text?

The text-align-last property specifies how to align the last line of a text. Notice that the text-align-last property sets the alignment for all last lines within the selected element. So, if you have a <div> with three paragraphs in it, text-align-last will apply to the last line of EACH of the paragraphs.

How do you select the last line in CSS?

The text-align-last property in CSS is used to set the last line of the paragraph just before the line break. The line break may be due to the natural ending of a paragraph, or it may be due to the use of <br> tag.

How do you go to the last line in HTML?

n/ to break the HTML into lines, then grab the last element of the array. Note: since this is HTML, you may want to split by /<br(?: \/)?>/ or /<br>/ , depending on the situation.


2 Answers

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/

like image 60
Kristin Avatar answered Sep 22 '22 07:09

Kristin


This is the cleanest hack I could come up with/find. Your mileage may vary.

I tested my example in IE8, Firefox, Chrome, Opera, Safari.

IE7 does not implement the :after pseudo-class, so it won't work there.

If you need IE7 support, it would probably work to stick the " ___" inside an extraneous span at the end of the div (use JS?).

Live Demo (see code)

CSS:

div {     width: 618px;             text-align: justify } div:after {     content: " __________________________________________________________";     line-height: 0;     visibility: hidden } 

HTML:

<div> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dui dolor, bibendum quis accumsan porttitor, fringilla sed libero. Phasellus felis ante, egestas at adipiscing lobortis, lobortis id mi. Praesent pulvinar dictum purus. Duis rhoncus bibendum vehicula. Vestibulum mollis, metus a consectetur semper, urna enim sollicitudin lacus, vel imperdiet turpis nisl at metus. Integer iaculis pretium dui, a viverra dolor lobortis pellentesque. Aliquam quis felis nec purus semper interdum. Nam ac dolor in sem tincidunt egestas. Ut nisl tortor, laoreet eu vestibulum id, bibendum at ipsum. Maecenas elementum bibendum orci, ac eleifend felis tincidunt in. Fusce elementum lacinia feugiat. </div> 

Unfortunately, it seems to make the div a line taller than it needs to be.

like image 45
thirtydot Avatar answered Sep 20 '22 07:09

thirtydot