Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created November 7, 2019 11:00
Show Gist options
  • Save bartwttewaall/9273c594ffd59ee22e2aa3e22008cc7b to your computer and use it in GitHub Desktop.
Save bartwttewaall/9273c594ffd59ee22e2aa3e22008cc7b to your computer and use it in GitHub Desktop.
Calculate best fitting column size for dynamic list of content (bootstrap 12-columns layout)
{% set colClass = 'col-' ~ (12 / max(2, min(blogPosts.length, 4))) %}
<div class="row">
{% for article in blogPosts %}
<article class="{{ colClass }}">
...
</article>
{% endfor %}
</div>
const calcColumnSize = (amount: number, min: number = 2, max: number = 4, total: number = 12) => {
return total / Math.max(min, Math.min(amount, max));
}
const blogsColumnClass = "col-" + calcColumnSize(blogPosts.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment