(function($){ $.fn.counterFix = function(){ return this.each(function(){ var $this = $(this); var $box = $this.children().eq(0); var $counters = $box.children(); var box_w = 0; var box_m = 0; var len = $counters.length; $counters.each(function (i) { box_w += $(this).outerWidth(true); if((i + 1) >= len) { var width = $this.width(); box_m = Math.floor((width - box_w) / 2); $box.css({ width : box_w, marginLeft : (box_m > 0) ? box_m : 0 }); } }); }); }; })(jQuery)