Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Last active December 15, 2015 11:39
Show Gist options
  • Save jimmynotjim/5254575 to your computer and use it in GitHub Desktop.
Save jimmynotjim/5254575 to your computer and use it in GitHub Desktop.
PX to % converter mixin. Example shows how you could build a grid with it. Can take any property so you don't need separate mixins for widths, margins or positioning properties (left, right, etc)
$page: 960; //This is our ideal width
$third-col: ( $page - ($gutter * 2) ) / 3;
$gutter: 20; //This is our gutter
@mixin pxtoper($property, $target, $context) {
#{$property}: ($target / $context) * 100%;
}
.foo {
width: 31.9444444%;
margin-left: 2.0833333%;
}
.foo:first-child {
margin-left: 0;
}
.foo-child {
width: 48.9130541%;
margin-left: 6.5217405%;
}
.foo-child:first-child {
margin-left: 0;
}
.foo { //Lets build a grid that has 20px gutters and three columns at 960px
@include fluid(width, $third-col, $page);
@include fluid(margin-left, $gutter, $page);
&:first-child {
margin-left: 0;
}
}
.foo-child { //This is a child column to a 1/3 column
@include fluid(width, 150, $third-col);
@include fluid(margin-left, $gutter, $third-col);
&:first-child {
margin-left: 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment