Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ram-prasad-1/8ee261791ec929c81ff8b01da22089c3 to your computer and use it in GitHub Desktop.

Select an option

Save ram-prasad-1/8ee261791ec929c81ff8b01da22089c3 to your computer and use it in GitHub Desktop.
A Sass mixin for maintaining vertical rhythm
$font-size: 16px;
$line-height-ratio: 1.5;
$line-height-base: 1rem * $line-height-ratio;
// base line-height = 16px * 1.5 = 24px
html {
font-size: $font-size;
line-height: $line-height-ratio;
background: url(http://basehold.it/i/24);
}
* {
margin-top: 0;
margin-bottom: 0;
padding-top: 0;
padding-bottom: 0;
}
@mixin vertical-rhythm($font-size, $bottom-rows: 1, $top-rows: 0, $use-height: false, $border: 0rem) {
$font-size: $font-size;
$border: $border;
$multiplier: ceil($font-size / $line-height-base);
// check if height-based element
// if so use height property for height, otherwise use line-height property
@if $use-height {
$leftover: $line-height-base * $multiplier - $font-size;
height: $font-size;
margin-top: $line-height-base * $top-rows + $leftover / 2;
margin-bottom: $line-height-base * $bottom-rows + $leftover / 2;
}
@else { // text based element (uses line-height)
font-size: $font-size;
line-height: $line-height-base * $multiplier;
margin-top: $line-height-base * $top-rows;
margin-bottom: $line-height-base * $bottom-rows - $border;
}
}
h1 {
@include vertical-rhythm(2.4rem, 1, 1);
}
h2 {
// example usage
@include vertical-rhythm(2rem);
}
p + p {
margin-top: 1.5rem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment