Skip to content

Instantly share code, notes, and snippets.

@eyy
Last active December 19, 2015 08:39
Show Gist options
  • Save eyy/5927065 to your computer and use it in GitHub Desktop.
Save eyy/5927065 to your computer and use it in GitHub Desktop.
fit element contents to its width
// fit element contents to its width
(function($) {
$.fn.fitter = function() {
this.each(function() {
var el = $(this).css({
//'overflow-x': 'hidden',
'white-space': 'nowrap'
}),
fitter = el.wrapInner('<fitter />').children('fitter').css({
display: 'inline-block'
}),
ratio = el.width() / fitter.width();
if (ratio < 1) {
var margin = (fitter.width() - el.width()) / 2;
fitter.css({
scale: ratio,
'margin-left': '-' + margin + 'px'
});
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment