Last active
November 25, 2018 13:29
-
-
Save klummy/3845eb7aef137c9b4afb6e7cf101bf95 to your computer and use it in GitHub Desktop.
Precise control over responsive typography for Sass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source - https://www.smashingmagazine.com/2016/05/fluid-typography/ | |
// ---- | |
// libsass (v3.5.0.beta.2) | |
// ---- | |
// ========================================================================= | |
// | |
// PRECISE CONTROL OVER RESPONSIVE TYPOGRAPHY FOR SASS | |
// --------------------------------------------------- | |
// Indrek Paas @indrekpaas | |
// | |
// Inspired by Mike Riethmuller's Precise control over responsive typography | |
// http://madebymike.com.au/writing/precise-control-responsive-typography/ | |
// | |
// Borrowed `strip-unit` function from Hugo Giraudel | |
// https://css-tricks.com/snippets/sass/strip-unit-function/ | |
// | |
// 02.04.2018 Remove `screen` keyword from media queries | |
// 11.08.2016 Remove redundant `&` self-reference | |
// 31.03.2016 Remove redundant parenthesis from output | |
// 02.10.2015 Add support for multiple properties | |
// 24.04.2015 Initial release | |
// | |
// ========================================================================= | |
@mixin fluid-type($properties, $min-vw, $max-vw, $min-value, $max-value) { | |
@each $property in $properties { | |
#{$property}: $min-value; | |
} | |
@media (min-width: $min-vw) { | |
@each $property in $properties { | |
#{$property}: calc(#{$min-value} + #{strip-unit($max-value - $min-value)} * (100vw - #{$min-vw}) / #{strip-unit($max-vw - $min-vw)}); | |
} | |
} | |
@media (min-width: $max-vw) { | |
@each $property in $properties { | |
#{$property}: $max-value; | |
} | |
} | |
} | |
@function strip-unit($number) { | |
@if type-of($number) == "number" and not unitless($number) { | |
@return $number / ($number * 0 + 1); | |
} | |
@return $number; | |
} | |
/* Single property */ | |
html { | |
@include fluid-type(font-size, 320px, 1366px, 14px, 18px); | |
} | |
/* Multiple properties with same values */ | |
h1 { | |
@include fluid-type(padding-bottom padding-top, 20em, 70em, 2em, 4em); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Single property */ | |
html { | |
font-size: 14px; | |
} | |
@media (min-width: 320px) { | |
html { | |
font-size: calc(14px + 4 * (100vw - 320px) / 1046); | |
} | |
} | |
@media (min-width: 1366px) { | |
html { | |
font-size: 18px; | |
} | |
} | |
/* Multiple properties with same values */ | |
h1 { | |
padding-bottom: 2em; | |
padding-top: 2em; | |
} | |
@media (min-width: 20em) { | |
h1 { | |
padding-bottom: calc(2em + 2 * (100vw - 20em) / 50); | |
padding-top: calc(2em + 2 * (100vw - 20em) / 50); | |
} | |
} | |
@media (min-width: 70em) { | |
h1 { | |
padding-bottom: 4em; | |
padding-top: 4em; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment