Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Help auto padding and centering divs Re: Help auto padding and centering divs

#77277
luxurychair
Member

This would require javascript.

With jQuery it would be something like this:

Code:
var numChildren = $(“#wrapper div”).length();
var excessRoom = $(“#wrapper”).width() – (numChildren * $(“#wrapper div”).first().width());
$(“#wrapper div”).css(“padding”, (excessRoom / (2 * numChildren)));
// 2 * numChildren in the denominator because we add equal padding to both sides.

This code is untested but the principal should stand up. I don’t know you’re handling more than 5 (the most you could fit in that width), so I’ll leave that to you. Also not sure what kind of padding you wanted. Left/right only? all the way around? you can change it to whatever you want.