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!
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
This method worked for me:
This trick is called "Ben Justification" *
Well, it's not quite all with css, you need to add a little extra to the end of the line. The markup for the heading above is...
The little addition at the end of the line triggers the justification of the line by starting a new line. The additional content (
<span> </span>
) is forced onto a new line because the space is made 1000px wide (with word-spacing) and
is treated like a word. The additional line does not display because the fontsize is set to 0px.Update, 23-Jan-11: I've just updated the markup to include a space after the within the span and setting the font size to 1px for the span: this is needed for IE8. Thanks to Mamoun Gadir for pointing out IE's problems.
The css for the heading above is...
* Unless evidence demonstrates that this technique has been published before, I hereby name this technique "Ben Justification" and declare it free for all to use.
Ben Clay, Jan 2010.
Source: http://www.evolutioncomputing.co.uk/technical-1001.html
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 extraneousspan
at the end of thediv
(use JS?).Live Demo (see code)
CSS:
HTML:
Unfortunately, it seems to make the
div
a line taller than it needs to be.If the closing div tag is followed by a line break or if the div tag makes use of an equivalent bottom padding/margin, then you can simply replace this with a concluding invisible line of non-breaking spaces such as this:
While this may not be the prettiest solution, it's probably the safest cross-browser solution as it doesn't rely on any kind of non-standard tweak. Just make sure that there's enough "nbsp" characters to always cause a line break while keeping within a safe margin from the maximum allowed on a single line.
As for why one would want this, well, I can give my own rationale to whom it may be of interest; there are situations where you want to divide up a continuous text block or text paragraph in order to insert images, tables or as in my case custom footnotes right before a page break. If you do that, then you want the last line right before the break to be justified as the text will resume right after the arbitrary break. In my case, the footnotes are of variable length so I have to enter them manually and therefore I also need to manually divide the text block and manually force alignment of the last line before the break. I'm not aware of any automatized solution that's also cross-browser compatible, but adding a bunch of non-breaking spaces does the job for me at least...
CSS3 offers a solution for this in the form of
text-align-last
, see http://www.w3.org/TR/2010/WD-css3-text-20101005/#text-align-lastHere'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:
If you have one line of text, use this to prevent the blank line the above CSS will cause
More details: http://kristinlbradley.wordpress.com/2011/09/15/cross-browser-css-justify-last-line-paragraph-text/
There is a
CSS3IE 5.5/6 (thanks, CSS3.com, NOT!) property calledtext-justify
that can do what you want with:Not sure about browser support.What a ripoff.