There :
$('a.tooltip').hover(function() { var title = $(this).attr('title'); var offset=$(this).offset(); var width=$(this).outerWidth(); var height=$(this).outerHeight(); $content=$('<div class="tooltip">' + title + '</div>').fadeIn(); $(this).append($content); var middle = offset.left+(width-$content.outerWidth())/2; // middle = Math.max(offset.left,offset.left+(width-$content.outerWidth())/2 ); $content.offset({ top: offset.top+height, left: middle }); $('div.tooltip').hover(function() { $(this).fadeOut('fast'); }); }, function() { $(this).find('div').fadeOut('fast'); });
You can make the code more readable, but the idea is there ;)
[EDIT] Code changed, the text has to be centered even if it is too long. (I misread the question)