Skip to content

Instantly share code, notes, and snippets.

@peol
Created December 6, 2011 09:51
Show Gist options
  • Save peol/1437595 to your computer and use it in GitHub Desktop.
Save peol/1437595 to your computer and use it in GitHub Desktop.
Algorithm for getting the most optimal percentage that is divisible by X columns (sum up to 100%)

Description

This issue is a really common one and I'm about to get it solved for a customer. I have 100% width to work with, and I got X columns that should span equally (sum up to 100%). Browsers handle decimals in percent values differently, so I want to create an algorithm that finds the optimal total percentage (within a threshold, say 95% - 100%) that creates the total minimum decimals per column.

Prerequisites

  • The algorithm needs to find the optimal percentage within a threshold
  • X column classes should be generated, and should be included in the calculation when looking for the optimal percentage (i.e. .col-1 { width: 10%; } .col-2 { width: 20%; } [...])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment