Skip to content

Instantly share code, notes, and snippets.

@cuibonobo
Created July 20, 2014 00:22
Show Gist options
  • Select an option

  • Save cuibonobo/42251b1eda192ad87be2 to your computer and use it in GitHub Desktop.

Select an option

Save cuibonobo/42251b1eda192ad87be2 to your computer and use it in GitHub Desktop.
Use rem units in CSS as if they were pixels. Also includes a pixel fallback for older browsers.
<html>
<head>
<title>This.</title>
</head>
<body>
<p>This.</p>
</body>
</html>
// ----
// Sass (v3.3.10)
// Compass (v1.0.0.alpha.20)
// ----
$rootFontSize: 16px;
@function calculateRem($size) {
$remSize: $size / $rootFontSize;
@return #{$remSize}rem;
}
@mixin rem($propertie, $value) {
/* Fallback */
#{$propertie}: $value;
#{$propertie}: calculateRem($value);
}
html {
font-size: $rootFontSize;
}
p {
font-size: calculateRem(32px);
@include rem(width, 100px);
@include rem(margin, 50px);
}
html {
font-size: 16px;
}
p {
font-size: 2rem;
/* Fallback */
width: 100px;
width: 6.25rem;
/* Fallback */
margin: 50px;
margin: 3.125rem;
}
<html>
<head>
<title>This.</title>
</head>
<body>
<p>This.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment