Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created January 7, 2019 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/2a7f0fbe59d6c552a924bf8c981731c7 to your computer and use it in GitHub Desktop.
Save whoisryosuke/2a7f0fbe59d6c552a924bf8c981731c7 to your computer and use it in GitHub Desktop.
JS / CSS-in-JS - Calculate width of grid element using column size and number of columns (e.g. [1,2] = 50% column)
/**
* Calculates width of a grid element.
*
* Accepts an array of two numbers, the column size
* and total number of columns (respectively).
*
* Uses the total columns to determine total width,
* then multiplies by the column size to calculate
* current column width.
*
* For example: a two column grid, with 50% wide columns,
* would be an array of `[1,2]`. 2 total columns, with a
* column taking up 1 of the 2 (so 50%). Same as `[3,6]`.
*
* @param {array} columns [Column size, Number of columns]
*/
const calculateGridWidth = columns => {
if (columns) {
return `width: ${(100 / columns[1]) * columns[0]}%;`;
}
};
export default calculateGridWidth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment