Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tonydolan/6e7fc28b0b6dd5b24270adda0c293cd2 to your computer and use it in GitHub Desktop.
Save tonydolan/6e7fc28b0b6dd5b24270adda0c293cd2 to your computer and use it in GitHub Desktop.
Fluid responsive typography with ModularScale, Vertical rhythm

Fluid responsive typography with ModularScale, Vertical rhythm

In this pen, I'm going to examine how to create scalable, fluid typography across multiple breakpoints and predefined font sizes using well-supported browser features and some basic algebra. The best part is that you can automate it all by using SCSS. 📝 Reference

A Pen by Siamak Mokhtari on CodePen.

License.

.wrapper
h1 Thinking with type
h2 A Critical Guide for Designers, Writers, Editors, & Students
p
| Our all time best selling book is now available in a revised and expanded second edition.
em Thinking with Type
| is the definitive guide to using typography in visual communication, from the printed page to the computer screen. This revised edition includes forty-eight pages of new content, including the latest information on style sheets for print and the web, the use of ornaments and captions, lining and non-lining numerals, the use of small caps and enlarged capitals, as well as information on captions, font licensing, mixing typefaces, and hand lettering. Throughout the book, visual examples show how to be inventive within systems of typographic form—what the rules are and how to break them.
em Thinking with Type
| is a type book for everyone: designers, writers, editors, students, and anyone else who works with words. The popular online companion to
em Thinking with Type
| (www.thinkingwithtype.com) has been revised to reflect the new material in the second edition.
h3 More about the author
p Ellen Lupton is the author, coauthor, or editor of 13 books with PAPress, including Design Culture Now; Skin: Surface, Substance + Design; Inside Design Now; Thinking with Type; D.I.Y.: Design It Yourself; and D.I.Y. Kids. She is Curator of Contemporary Design, Cooper-Hewitt, National Design Museum, Smithsonian Institution, New York and Director, Graphic Design MFA Program, Maryland Institute College of Art, Baltimore. she is hte recipient of numerous awards including I.D. Forty, 1992; Chrysler Design Award, 1996; and AIGA Gold Medal, 2007.
hr
h1.large
| Let's create
br
| beautiful
br
| things together
h4 Sea Appear Gathering
p
b Face Second Have Female For All
br
| Morning bearing stars said brought life.
br
br
| So replenish.
br
br
| Also dominion, isn't open creature years.
p
b Signs Land
br
| You'll a greater first female, deep creature fifth.
br
br
b Grass Beginning Gathering The Living Seas Wherein Creepeth
br
| In for fifth were fruit.
br
br
b Lesser Divide Hath You A Own
br
| Made whales every.
ul
li Divided give. Unto brought void.
li Moving our so together multiply creature life.
li Earth greater. Evening subdue.
li Of bring days one.
li Saw is gathering stars which. God.
li Day blessed you i.
label.toggle--grid(for="toggleGrid")
input(type="checkbox" id="toggleGrid" checked)
span Grid view
const html = document.querySelector('html');
const checkbox = document.querySelector('#toggleGrid');
checkbox.addEventListener('change', function() {
if(this.checked) {
html.classList.add('has-grid')
} else {
html.classList.remove('has-grid')
}
});
// Variables:
$type-settings: (
320:
(
"ratio": 1.333,
"base": 14,
"types": (1: (line-height: 1), 2: (line-height: 1), 3: (line-height: 1), 4: (line-height: 2), 5: (line-height: 3))
),
576:
(
"ratio": 1.333,
"base": 16,
"types": (1: (line-height: 1), 2: (line-height: 1), 3: (line-height: 1), 4: (line-height: 2), 5: (line-height: 3))
),
768:
(
"ratio": 1.5,
"base": 18,
"types": (1: (line-height: 1), 2: (line-height: 1), 3: (line-height: 2), 4: (line-height: 3), 5: (line-height: 4))
),
992:
(
"ratio": 1.618,
"base": 20,
"types": (1: (line-height: 1), 2: (line-height: 2), 3: (line-height: 2), 4: (line-height: 4), 5: (line-height: 6))
)
);
:root {
--baseline: 1.75rem;
--gap: 1.75rem;
}
@media (max-width: 576px) {
:root {
--baseline: 1.5rem;
}
}
// Functions:
// poly-fluid-sizing
// Generate linear interpolated size values through multiple break points
// @param $property - A string CSS property name
// @param $map - A SASS map of viewport unit and size value pairs
// @requires function linear-interpolation
// @requires function map-sort
// @example: @include poly-fluid-sizing('font-size', (576px: 22px, 768px: 24px, 992px: 34px));
@mixin poly-fluid-sizing($property, $map) {
// Get the number of provided breakpoints
$length: length(map-keys($map));
// Error if the number of breakpoints is < 2
@if ($length < 2) {
@error "poly-fluid-sizing() $map requires at least values";
}
// Sort the map by viewport width (key)
$map: map-sort($map);
$keys: map-keys($map);
$minItem: map-get($map, nth($keys, 1));
// Minimum size
font-size: map-get($minItem, "font-size");
line-height: map-get($minItem, "line-height");
// Interpolated size through breakpoints
@for $i from 1 through ($length - 1) {
$item: map-get($map, nth($keys, $i));
@media (min-width: nth($keys,$i)) {
font-size: linear-interpolation(
(
nth($keys, $i): map-get($item, "font-size"),
nth($keys, ($i + 1)): map-get(map-get($map, nth($keys, ($i + 1))), "font-size")
)
);
line-height: map-get(map-get($map, nth($keys, ($i + 1))), "line-height");
}
}
// Maxmimum size
@media (min-width: nth($keys,$length)) {
$maxItem: map-get($map, nth($keys, $length));
font-size: map-get($maxItem, "font-size");
line-height: map-get($maxItem, "line-height");
}
}
// linear-interpolation
// Calculate the definition of a line between two points
// @param $map - A SASS map of viewport widths and size value pairs
// @returns A linear equation as a calc() function
// @example: font-size: linear-interpolation((320px: 18px, 768px: 26px));
@function linear-interpolation($map) {
$keys: map-keys($map);
@if (length($keys) != 2) {
@error "linear-interpolation() $map must be exactly 2 values";
}
// The slope
$m: (map-get($map, nth($keys, 2)) - map-get($map, nth($keys, 1)))/ (nth($keys, 2) - nth($keys, 1));
// The y-intercept
$b: map-get($map, nth($keys, 1)) - $m * nth($keys, 1);
// Determine if the sign should be positive or negative
$sign: "+";
@if ($b < 0) {
$sign: "-";
$b: abs($b);
}
@return calc(#{$m * 100}vw #{$sign} #{$b});
}
// list-sort
// Sort a SASS list
// @param $list - A SASS list
// @returns A sorted SASS list
// @requires function list-remove
@function list-sort($list) {
$sortedlist: ();
@while length($list) > 0 {
$value: nth($list, 1);
@each $item in $list {
@if $item < $value {
$value: $item;
}
}
$sortedlist: append($sortedlist, $value, "space");
$list: list-remove($list, index($list, $value));
}
@return $sortedlist;
}
// map-sort
// Sort map by keys
// @param $map - A SASS map
// @returns A SASS map sorted by keys
// @requires function list-sort
@function map-sort($map) {
$keys: list-sort(map-keys($map));
$sortedMap: ();
@each $key in $keys {
$sortedMap: map-merge($sortedMap, ($key: map-get($map, $key)));
}
@return $sortedMap;
}
// list-remove
// Remove an item from a list
// @param $list - A SASS list
// @param $index - The list index to remove
// @returns A SASS list
@function list-remove($list, $index) {
$newList: ();
@for $i from 1 through length($list) {
@if $i != $index {
$newList: append($newList, nth($list, $i), "space");
}
}
@return $newList;
}
// modular-scale
// Calc size with ratio
// @param $value - Base of ratio
// @param $ratio - 1.618 or something
// @param $muliple - Number of Sequece
// @returns scale
@function modular-scale($value, $ratio, $multiple) {
$modular-scale: $value;
@if ($multiple == 0) {
@return $modular-scale;
}
@for $i from 1 through $multiple {
$modular-scale: $modular-scale * $ratio;
}
@return $modular-scale;
}
// type-scale
// convert $type-settings to modular-scale function
// @param $level - level rate
// @param $_baseline - line-height "default"
// @returns $typescale-map
@function type-scale($level, $_baseline: var(--baseline)) {
$typescale-map: ();
@each $query, $options in $type-settings {
$map: ();
$ratio: map-get($options, "ratio");
$base: map-get($options, "base");
$types: map-get($options, "types");
$getLevel: map-get($types, $level);
$getLines: map-get($getLevel, "line-height");
$fontSize: modular-scale($base, $ratio, $level - 1);
$map: map-merge($map, ($query * 1px: (font-size: $fontSize * 1px, line-height: line-height($getLines))));
$typescale-map: map-merge($typescale-map, $map);
}
@return $typescale-map;
}
// line-height
// Calc line-height with level
// @param $level - line rate
// @returns line-height
@function line-height($level) {
@return calc(#{$level} * var(--baseline));
}
// Styles:
* {
box-sizing: border-box;
}
html {
box-sizing: border-box;
height: 100%;
&.has-grid {
background-size: 1px var(--baseline);
background-image: linear-gradient(rgba(#00bcd4, .23) 1px, transparent 0%, transparent);
h1, h2, h3 {
background-color: rgba(#ff0, 0.25);
}
}
}
body {
line-height: var(--baseline);
padding: var(--baseline) var(--gap);
padding-bottom: calc(var(--baseline) * 2);
font-weight: 400;
margin: 0 auto;
width: 70rem;
max-width: 100%;
font-family: Circular Std, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
letter-spacing: -.01em;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.wrapper {
display: grid;
width: 48rem;
margin: 0 auto;
max-width: 100%;
grid-column-gap: 1em;
}
h1,
h2,
h3,
h4 {
margin: 0;
font-weight: 700;
margin-bottom: var(--baseline);
}
h1 {
@include poly-fluid-sizing("font-size", type-scale(4));
font-family: Playfair Display;
font-weight: 900;
word-spacing: 0.02em;
letter-spacing: -.026em;
// margin-bottom: 0;
&.large {
@include poly-fluid-sizing("font-size", type-scale(5));
}
}
h2 {
@include poly-fluid-sizing("font-size", type-scale(3));
color: #CFCFD1;
word-spacing: 0.03em;
letter-spacing: -.032em;
}
h3 {
@include poly-fluid-sizing("font-size", type-scale(2));
letter-spacing: -.036em;
}
p, h4, ul, ol {
margin-top: 0;
margin-bottom: var(--baseline);
@include poly-fluid-sizing("font-size", type-scale(1));
}
/* Let's make sure all's aligned */
pre, table, blockquote {
margin-top: 0;
margin-bottom: var(--baseline);
}
ul, ol {
padding: 0;
padding-left: var(--gap);
list-style-type: disc;
}
ul ul, ol ol, ul ol, ol ul {
margin-top: 0;
margin-bottom: 0;
}
hr, .hr {
margin: 0;
padding: 0;
padding-bottom: var(--baseline);
border: 0;
border-top: 1px solid #eee;
}
a, b, i, strong, em, small, code {
line-height: 0;
}
sub, sup {
line-height: 0;
position: relative;
vertical-align: baseline;
}
sup {
top: -0.5em;
}
sub {
bottom: -0.25em;
}
//
// DEMO
//
.toggle--grid {
position: fixed;
padding: 1rem;
background-color: rgba(white, 0.92);
box-shadow: 1px -1px 12px rgba(black, 0.16);
bottom: 0;
border-radius: 0 10px 0 0;
left: 0;
cursor: pointer;
user-select: none;
transition: background-color 0.2s ease-in;
&:hover {
background-color: #fff;
}
span {
padding-left: 10px;
}
}
<link href="https://fonts.googleapis.com/css?family=Playfair+Display:900" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment