Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active September 2, 2019 15:30
Show Gist options
  • Select an option

  • Save morgyface/4c7208975b643d5f49806f737976bc9f to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/4c7208975b643d5f49806f737976bc9f to your computer and use it in GitHub Desktop.
SCSS | Responsive css tiling layout for fixed width spacing and variable width columns
@mixin colcalc($spacing, $columns) {
// First let us reset any previous margins and clears
// As 2 columns is the first use there's no need to reset, so we make it conditional
@if $columns > 2 {
$previous_columns: $columns - 1;
&:nth-child(#{$previous_columns}n) {
margin-right: $spacing;
}
&:nth-child(#{$previous_columns}n+1) {
clear: none;
}
}
// Now for the width calculation
$total_spacing: ( $columns - 1 ) * $spacing;
width: -moz-calc( ( 100% - #{$total_spacing} ) / #{$columns} );
width: -webkit-calc( ( 100% - #{$total_spacing} ) / #{$columns} );
width: calc( ( 100% - #{$total_spacing} ) / #{$columns} );
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
width: calc( ( 100% - #{$total_spacing} - 1px ) / #{$columns} );
}
@supports (-ms-ime-align: auto) {
width: calc( ( 100% - #{$total_spacing} - 1px ) / #{$columns} );
}
&:nth-child(#{$columns}n){
margin-right: 0;
}
&:nth-child(#{$columns}n+1){
clear: left;
}
}
@morgyface
Copy link
Author

Well, I've finally had chance to test this properly in IE and Edge, and both browsers struggled with rounding numbers when divided by 3. So I've found IE and Edge specific queries which deduct 1 pixel off the width and this has resolved the issue. All is now good.

@morgyface
Copy link
Author

Must update this to be the fallback for CSS grid. It's on my to-do list, please don't hurt me.

@morgyface
Copy link
Author

This verbose gist should now die. Grid is supported widely, use this gist instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment