Posts Tagged ‘text-align justify’

Character Justify Plugin

Monday, April 6th, 2009

Example of Character Justification plugin

Example of Character Justification plugin

I’ve updated my Character Justify Plugin. I could go into detail but to put it simply I’ve worked to improve the alignment on the right edge particularly when there are spaces between words in the same element.  Previously I was trying to deal with the extra letter spacing caused by multiple words by subtracting an arbitrary percentage from the width that I’d stretch the letters to.  Now I’m replacing the spaces with a character of fixed size (”_”) when calculating the width of the elements.
The example has been updated to show this and I’ve tweaked the animation example to so that the animations on each element don’t get out of sync.

Happy Justifying!

Character Justification using JQuery

Saturday, January 17th, 2009
Example of Character Justification plugin

Justification using pure CSS can be achieved by setting the text-align property to “justify”.  This will tell the browser to attempt to line up the words so that left the and right edges of each line are aligned with those above and below.  This effectiveness of this property can be enhanced by inserting hyphens where necessary to maintain the alignment using Hyphenator (here’s an example).

The “text-align: justify” technique is useless when applied to a list or similar column of text where each item or line only has one or two words in it.  For example in a navigation menu.  This is because the spacing that is added to achieve the justification is only added at the word boundaries.  Justification based on characters will be available in CSS3 using the property text-justify with the value “newspaper”.

Example of character justified navigation bar

I had the requirement to build a navigation sidebar with justified text for Caragh’s still unfinished website so rather than wait for CSS3 cross browser support I decided to find a solution.  One technique would be to use image replacement and insert images of the properly justified text.  The main drawback of that is it doesn’t easily allow for dynamic content so I decided to try writing a JavaScript solution.  The result is this Character Justification JQuery plugin.  All you need to do is call characterJustify() on whatever JQuery object you want to be justified.  The plugin will then insert the correct letter-spacing so that the letters are evenly spaced and the left and right edges more or less line up.


       $(document).ready(function(){
          $(".justify").characterJustify();
       });

The text will be distributed to fit within the bounds of the element(s) you called the method on unless you pass an optional integer parameter to set a particular pixel width.  I also added the regular JQuery animation parameters speed, easing and callback so you can also animate the process if you need to.


       $(document).ready(function(){

          $(".justify").characterJustify(200);

          var anim = function() {
            $(".justify").characterJustify(300, 1000, "swing", function() {
              $(".justify").characterJustify(200, 1000, "swing", anim);
            })};

          anim();
       });

Examples:

Plugin source

UPDATE: I’ve updated the plugin slightly.  Details here.  I’ve updated the above example animation code.