How it works
Railings need even spacing with no gap wider than the safety limit, so the page first finds the smallest number of balusters that keeps every opening legal, then spreads them evenly. US residential code commonly caps the opening at 4 inches, measured as a sphere that must not pass, but local rules differ, so the limit is typed here. The even gap always lands a little under the maximum, which is what you want. Measure between the inside faces of the posts, not over them, and allow for the end posts taking up the first and last space.
Formula
even gap = (rail length − count × baluster width) ÷ (count + 1)
centre-to-centre mark = rail length ÷ (count + 1)
Sources
Example
A 72 inch rail with 1.5 inch balusters and a 4 inch limit: count = round up (73.5 ÷ 5.5) = 14. Gap = (72 − 14 × 1.5) ÷ 15 = 51 ÷ 15 = 3.4 inches. Check: 14 × 1.5 + 15 × 3.4 = 21 + 51 = 72.
Frequently asked questions
What gap does code allow?
US residential code commonly limits the opening to 4 inches. Check the rule where you are building.
Where do I measure the rail length?
Between the inside faces of the two posts, since the posts take up the ends.
Why is the real gap under the maximum?
Even spacing with whole balusters always lands slightly under, which is safer than over.
Does this work on stairs?
Yes. Use the sloped rail length and keep the same maximum gap; the openings are measured vertically on stairs in some codes.