Haircut trims text down to fit into its container.

Get it

Download .zip Download .tar.gz View on GitHub

Demo

Usage

First, you will need to change up some of your HTML. Haircut expects an element with the full text contained within the title attribute like so:

<abbr title="My excrutiatingly long string"></abbr>

You will then need to wrap that and call Haircut on the parent element:

<h1><abbr title="My excrutiatingly long string"></abbr></h1>

Initialize by calling .haircut(). If the layout changes and your Haircut element might change, call .stringResize(). Like so:

$('h1').haircut();

$(window).resize(function () {
  $('h1').stringResize();
});

You can also give it a few options:

$('h2').haircut({
  // The more haircut items on the page,
  // the more it jitters during animation.
  // Adding padding reduces jitter but
  // gives you less usable space.
  jitterPadding: 30,

  // Decide which part of the string to trim.
  // Can be "start", "middle" or "end".
  placement: 'middle'
});

It's also recommended that you style 2 CSS selectors: ._LVhaircutExpand and ._LVshowHaircutExpand. If configured, Haircut will overlay the full text when a Haircut string is hovered over. See below for an example.

If the containing element has an anchor tag, that will be brought along for the ride. That way long URLs can be truncated at no expense of usability.

Installation

Just include jquery.haircut.js as a script tag on the page. Bonus points for incorporating it into your asset pipeline.

In Action

Here's a little video of Haircut in action on the LayerVault Directory page.

License

Copyright 2013 LayerVault Inc.

Licensed under the MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.