Skip to content

Instantly share code, notes, and snippets.

@mirisuzanne
Created February 18, 2010 23:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mirisuzanne/308199 to your computer and use it in GitHub Desktop.
Save mirisuzanne/308199 to your computer and use it in GitHub Desktop.
//** Vertical Rhythm **//
// By Chris Eppstein and Eric Meyer, living here on a temporary basis
// set the default border style for rhythm borders
!default_rhythm_border_style = "solid"
// The IE font ratio is a fact of life. Deal with it.
!ie_font_ratio = 16px / 100%
// The base line height is the basic unit of line hightness.
!base_line_height ||= 18px
// The base font size
!base_font_size ||= 12px
// The basic unit of font rhythm
!base_rhythm_unit= !base_line_height / !base_font_size * 1em
// The leader is the amount of whitespace in a line.
// It might be useful in your calculations
!base_leader = (!base_line_height - !base_font_size) * 1em / !base_font_size
// The half-leader is the amount of whitespace above and below a line.
// It might be useful in your calculations
!base_half_leader = !base_leader / 2
// Establishes a font baseline for the given font-size in pixels
=establish-baseline(!font_size = !base_font_size)
body
font-size= !font_size / !ie_font_ratio
+adjust-leading-to(1, !font_size)
html>body
font-size= !font_size
// Show a background image that can be used to debug your alignments.
=debug-vertical-alignment
background: url(underline.png)
// Adjust a block to have a different font size and leading to maintain the rhythm.
// !lines is a number that is how many times the baseline rhythm this
// font size should use up. Does not have to be an integer, but it defaults
// to the smallest integer that is large enough to fit the font.
// Use !from_size to adjust from a non-base font-size.
=adjust-font-size-to(!to_size, !lines = ceil(!font_size / !base_line_height), !from_size = !base_font_size)
font-size= 1em * !to_size / !from_size
+adjust-leading-to(!lines, !font_size)
=adjust-leading-to(!lines, !font_size = !base_font_size)
line-height= 1em * !lines * !base_line_height / !font_size
// Apply leading whitespace
=leader(!lines = 1, !font_size = !base_font_size, !property = "margin")
#{!property}-top= 1em * !lines * !base_line_height / !font_size
=padding-leader(!lines = 1, !font_size = !base_font_size)
+leader(!lines, !font_size, "padding")
=margin-leader(!lines = 1, !font_size = !base_font_size)
+leader(!lines, !font_size, "margin")
// Apply trailing whitespace
=trailer(!lines = 1, !font_size = !base_font_size, !property = "margin")
#{!property}-bottom= 1em * !lines * !base_line_height / !font_size
=padding-trailer(!lines = 1, !font_size = !base_font_size)
+trailer(!lines, !font_size, "padding")
=margin-trailer(!lines = 1, !font_size = !base_font_size)
+trailer(!lines, !font_size, "margin")
// Apply a border width to any side without destroying the vertical rhythm
=apply-side-rhythm-border(!side, !width = 1px, !lines = 1, !font_size = !base_font_size, !border_style = !default_rhythm_border_style)
border-#{!side}:
style= !border_style
width= 1em * !width / !font_size
padding-#{!side}= (1em / !font_size) * ((!lines * !base_line_height) - !width)
// Aplly rhythm borders equally to all sides
=rhythm-borders(!width = 1px, !lines = 1, !font_size = !base_font_size, !border_style = !default_rhythm_border_style)
border:
style= !border_style
width= 1em * !width / !font_size
padding= (1em / !font_size) * ((!lines * !base_line_height) - !width)
// Apply a leading rhythm border
=leading-border(!width = 1px, !lines = 1, !font_size = !base_font_size, !border_style = !default_rhythm_border_style)
+apply-side-rhythm-border("top", !width, !lines, !font_size, !border_style)
// Apply a trailing rhythm border
=trailing-border(!width = 1px, !lines = 1, !font_size = !base_font_size, !border_style = !default_rhythm_border_style)
+apply-side-rhythm-border("bottom", !width, !lines, !font_size, !border_style)
// Apply both leading and trailing rhythm borders
=horizontal-borders(!width = 1px, !lines = 1, !font_size = !base_font_size, !border_style = !default_rhythm_border_style)
+leading-border(!width, !lines, !font_size, !border_style)
+trailing-border(!width, !lines, !font_size, !border_style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment