Last active
August 13, 2020 07:35
-
-
Save halofx/635ac22fd88a45e06480 to your computer and use it in GitHub Desktop.
Responsive Font Size with Sass Maps
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
| // ---- | |
| // libsass (v3.2.5) | |
| // From https://www.smashingmagazine.com/2015/06/responsive-typography-with-sass-maps/ | |
| // ---- | |
| // Breakpoint values for min-width operators. | |
| $breakpoints: ( | |
| small : 480px, | |
| medium: 700px, | |
| large : 1024px | |
| ); | |
| // Font sizes, w/ optional line-heights for each breakpoint. | |
| // Null is mobile, no breakpoint. | |
| $p-font-sizes: ( | |
| null : (15px, 1.3), | |
| small : 16px, | |
| medium : (17px, 1.4), | |
| large: 19px | |
| ); | |
| $h1-font-sizes: ( | |
| null : 28px, | |
| small : 31px, | |
| medium : 33px, | |
| large: 36px | |
| ); | |
| @mixin font-size($fs-map, $fs-breakpoints: $breakpoints) { | |
| @each $fs-breakpoint, $fs-font-size in $fs-map { | |
| @if $fs-breakpoint == null { | |
| @include make-font-size($fs-font-size); | |
| } | |
| @else { | |
| // If $fs-font-size is a key that exists in | |
| // $fs-breakpoints, use the value | |
| @if map-has-key($fs-breakpoints, $fs-breakpoint) { | |
| $fs-breakpoint: map-get($fs-breakpoints, $fs-breakpoint); | |
| } | |
| @media screen and (min-width: $fs-breakpoint) { | |
| @include make-font-size($fs-font-size); | |
| } | |
| } | |
| } | |
| } | |
| // Utility function for mixin font-size | |
| @mixin make-font-size($fs-font-size) { | |
| // If $fs-font-size is a list, include | |
| // both font-size and line-height | |
| @if type-of($fs-font-size) == "list" { | |
| font-size: nth($fs-font-size, 1); | |
| @if (length($fs-font-size) > 1) { | |
| line-height: nth($fs-font-size, 2); | |
| } | |
| } | |
| @else { | |
| font-size: $fs-font-size; | |
| } | |
| } | |
| p { | |
| @include font-size($p-font-sizes); | |
| } | |
| h1 { | |
| @include font-size($h1-font-sizes); | |
| } |
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
| p { | |
| font-size: 15px; | |
| line-height: 1.3; | |
| } | |
| @media screen and (min-width: 480px) { | |
| p { | |
| font-size: 16px; | |
| } | |
| } | |
| @media screen and (min-width: 700px) { | |
| p { | |
| font-size: 17px; | |
| line-height: 1.4; | |
| } | |
| } | |
| @media screen and (min-width: 1024px) { | |
| p { | |
| font-size: 19px; | |
| } | |
| } | |
| h1 { | |
| font-size: 28px; | |
| } | |
| @media screen and (min-width: 480px) { | |
| h1 { | |
| font-size: 31px; | |
| } | |
| } | |
| @media screen and (min-width: 700px) { | |
| h1 { | |
| font-size: 33px; | |
| } | |
| } | |
| @media screen and (min-width: 1024px) { | |
| h1 { | |
| font-size: 36px; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment