The 4-inch sphere rule
Most US residential building codes (based on the IRC) require that a 4 in sphere cannot pass through any opening in a deck or porch guard — including the gaps between balusters. The idea is simple: a 4 in sphere is roughly the size of a small child's head, and if it can't fit through, the child can't get stuck or fall through either.
That 4 in figure is a maximum, not a target to divide by. If you take a railing section and simply divide its length by 4 in, you'll almost never get a whole number of balusters — and whatever fraction is left over usually gets tacked onto one gap, which can quietly push that single gap past 4 in and fail inspection. The fix is to work out how many balusters you actually need first (always rounding up), then spread the leftover space evenly across every gap. That's exactly what the calculator above does, which is why the real spacing it gives you almost always lands a bit under 4 in rather than right at it.
The formula and a worked example
The number of balusters a section needs comes from rounding the theoretical gap count up to a whole number:
Balusters = ceil((Section length − Max gap) ÷ (Baluster width + Max gap))
Once you know the baluster count, the actual (always slightly smaller) gap comes from dividing whatever space is left after the balusters by the number of gaps, which is always one more than the number of balusters (every baluster has a gap on both sides, shared with its neighbors):
Actual gap = (Section length − Balusters × Baluster width) ÷ (Balusters + 1)
Take a 72 in railing section using 1.5 in balusters (standard 2x2 lumber) and the standard 4 in max gap. Balusters = ceil((72 − 4) ÷ (1.5 + 4)) = ceil(68 ÷ 5.5) = ceil(12.36) = 13 balusters. The actual gap is then (72 − 13 × 1.5) ÷ 14 = 52.5 ÷ 14 = 3.75 in — comfortably under the 4 in maximum, and identical at every gap along the section. Add the baluster width to that gap and you get the center-to-center spacing, 1.5 + 3.75 = 5.25 in, which is the number you'll actually use when marking layout points on the rail.
Laying it out without doing math on a ladder
Once you have the numbers, you don't need to recalculate anything while you're actually building. Mark the center of the first baluster at the "first baluster center" distance from the post face (spacing + half the baluster width — 4.5 in in the worked example above), then move down the rail marking every following center at the center-to-center distance (5.25 in in that example). Every mark after the first is the same distance from the one before it, so you're just walking a tape measure down the rail and making a pencil mark at each interval.
An even faster approach for a long run of railing is to cut a spacing jig — a scrap block cut to the exact actual gap width (3.75 in in the example). Set each new baluster against the jig, butted up to the previous one, and you get identical spacing without measuring or marking at all, which is both faster and more consistent than working from a tape measure and a pencil.
Common mistakes to avoid
Measuring post-to-post instead of the clear opening. Enter the section length as the distance between the inside faces of the posts, not center to center. Posts are commonly 3.5 to 5.5 in wide, so using the center-to-center distance overstates how much room you actually have and throws off both the baluster count and the spacing.
Forgetting the rule also applies under the bottom rail.The 4 in sphere rule doesn't just apply between balusters — it also applies to the gap between the deck surface (or bottom rail) and the underside of the guard system. A baluster layout can pass perfectly between balusters and still fail inspection if the bottom gap is too tall, so check that measurement separately.
Assuming the maximum gap is always 4 in. Guards around decks and porches generally use the 4 in figure, but some jurisdictions and some situations — like the triangular opening at open stair treads — use a different maximum (commonly 6 in for that specific triangle). This calculator lets you edit the max gap for exactly that reason, but always confirm the correct number with your local building department before you build.