| 1 |
// Extracted from compass/typography/vertical_rhythm.scss |
| 2 |
|
| 3 |
|
| 4 |
// The base font size. |
| 5 |
$base-font-size: 16px !default; |
| 6 |
|
| 7 |
// The base line height determines the basic unit of vertical rhythm. |
| 8 |
$base-line-height: 24px !default; |
| 9 |
|
| 10 |
// Set the default border style for rhythm borders. |
| 11 |
$default-rhythm-border-style: solid !default; |
| 12 |
|
| 13 |
// The default font size in all browsers. |
| 14 |
$browser-default-font-size: 16px; |
| 15 |
|
| 16 |
// Set to false if you want to use absolute pixels in sizing your typography. |
| 17 |
$relative-font-sizing: true !default; |
| 18 |
|
| 19 |
// Allows the `adjust-font-size-to` mixin and the `lines-for-font-size` function |
| 20 |
// to round the line height to the nearest half line height instead of the |
| 21 |
// nearest integral line height to avoid large spacing between lines. |
| 22 |
$round-to-nearest-half-line: false !default; |
| 23 |
|
| 24 |
// Ensure there is at least this many pixels |
| 25 |
// of vertical padding above and below the text. |
| 26 |
$min-line-padding: 2px !default; |
| 27 |
|
| 28 |
// $base-font-size but in your output unit of choice. |
| 29 |
// Defaults to 1em when `$relative-font-sizing` is true. |
| 30 |
$font-unit: if($relative-font-sizing, 1em, $base-font-size) !default; |
| 31 |
|
| 32 |
// The basic unit of font rhythm. |
| 33 |
$base-rhythm-unit: $base-line-height / $base-font-size * $font-unit; |
| 34 |
|
| 35 |
// The leader is the amount of whitespace in a line. |
| 36 |
// It might be useful in your calculations. |
| 37 |
$base-leader: ($base-line-height - $base-font-size) * $font-unit / $base-font-size; |
| 38 |
|
| 39 |
// The half-leader is the amount of whitespace above and below a line. |
| 40 |
// It might be useful in your calculations. |
| 41 |
$base-half-leader: $base-leader / 2; |
| 42 |
|
| 43 |
// True if a number has a relative unit. |
| 44 |
@function relative-unit($number) { |
| 45 |
@return unit($number) == "%" or unit($number) == "em" or unit($number) == "rem" |
| 46 |
} |
| 47 |
|
| 48 |
// True if a number has an absolute unit. |
| 49 |
@function absolute-unit($number) { |
| 50 |
@return not (relative-unit($number) or unitless($number)); |
| 51 |
} |
| 52 |
|
| 53 |
@if $relative-font-sizing and not relative-unit($font-unit) { |
| 54 |
@warn "$relative-font-sizing is true but $font-unit is set to #{$font-unit} which is not a relative unit."; |
| 55 |
} |
| 56 |
|
| 57 |
// Establishes a font baseline for the given font-size. |
| 58 |
@mixin establish-baseline($font-size: $base-font-size) { |
| 59 |
// IE 6 refuses to resize fonts set in pixels and it weirdly resizes fonts |
| 60 |
// whose root is set in ems. So we set the root font size in percentages of |
| 61 |
// the default font size. |
| 62 |
* html { |
| 63 |
font-size: 100% * ($font-size / $browser-default-font-size); |
| 64 |
} |
| 65 |
html { |
| 66 |
font-size: $font-size; |
| 67 |
@include adjust-leading-to(1, if($relative-font-sizing, $font-size, $base-font-size)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Resets the line-height to 1 vertical rhythm unit. |
| 72 |
// Does not work on elements whose font-size is different from $base-font-size. |
| 73 |
// |
| 74 |
// @deprecated This mixin will be removed in the next release. |
| 75 |
// Please use the `adjust-leading-to` mixin instead. |
| 76 |
@mixin reset-baseline { |
| 77 |
@include adjust-leading-to(1, if($relative-font-sizing, $base-font-size, $base-font-size)); |
| 78 |
} |
| 79 |
|
| 80 |
// Show a background image that can be used to debug your alignments. |
| 81 |
// Include the $img argument if you would rather use your own image than the |
| 82 |
// Compass default gradient image. |
| 83 |
@mixin debug-vertical-alignment($img: false) { |
| 84 |
@if $img { |
| 85 |
background: image-url($img); |
| 86 |
} @else { |
| 87 |
@include baseline-grid-background($base-rhythm-unit); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Adjust a block to have a different font size and line height to maintain the |
| 92 |
// rhythm. $lines specifies how many multiples of the baseline rhythm each line |
| 93 |
// of this font should use up. It does not have to be an integer, but it |
| 94 |
// defaults to the smallest integer that is large enough to fit the font. |
| 95 |
// Use $from-size to adjust from a font-size other than the base font-size. |
| 96 |
@mixin adjust-font-size-to($to-size, $lines: lines-for-font-size($to-size), $from-size: $base-font-size) { |
| 97 |
@if not $relative-font-sizing and $from-size != $base-font-size { |
| 98 |
@warn "$relative-font-sizing is false but a relative font size was passed to adjust-font-size-to"; |
| 99 |
} |
| 100 |
font-size: $font-unit * $to-size / $from-size; |
| 101 |
@include adjust-leading-to($lines, if($relative-font-sizing, $to-size, $base-font-size)); |
| 102 |
} |
| 103 |
|
| 104 |
// Adjust a block to have different line height to maintain the rhythm. |
| 105 |
// $lines specifies how many multiples of the baseline rhythm each line of this |
| 106 |
// font should use up. It does not have to be an integer, but it defaults to the |
| 107 |
// smallest integer that is large enough to fit the font. |
| 108 |
@mixin adjust-leading-to($lines, $font-size: $base-font-size) { |
| 109 |
line-height: rhythm($lines, $font-size); |
| 110 |
} |
| 111 |
|
| 112 |
// Calculate rhythm units. |
| 113 |
@function rhythm( |
| 114 |
$lines: 1, |
| 115 |
$font-size: $base-font-size, |
| 116 |
$offset: 0 |
| 117 |
) { |
| 118 |
@if not $relative-font-sizing and $font-size != $base-font-size { |
| 119 |
@warn "$relative-font-sizing is false but a relative font size was passed to the rhythm function"; |
| 120 |
} |
| 121 |
$rhythm: $font-unit * ($lines * $base-line-height - $offset) / $font-size; |
| 122 |
// Round the pixels down to nearest integer. |
| 123 |
@if unit($rhythm) == px { |
| 124 |
$rhythm: floor($rhythm); |
| 125 |
} |
| 126 |
@return $rhythm; |
| 127 |
} |
| 128 |
|
| 129 |
// Calculate the minimum multiple of rhythm units needed to contain the font-size. |
| 130 |
@function lines-for-font-size($font-size) { |
| 131 |
$lines: if($round-to-nearest-half-line, |
| 132 |
ceil(2 * $font-size / $base-line-height) / 2, |
| 133 |
ceil($font-size / $base-line-height)); |
| 134 |
@if $lines * $base-line-height - $font-size < $min-line-padding * 2 { |
| 135 |
$lines: $lines + if($round-to-nearest-half-line, 0.5, 1); |
| 136 |
} |
| 137 |
@return $lines; |
| 138 |
} |
| 139 |
|
| 140 |
// Apply leading whitespace. The $property can be margin or padding. |
| 141 |
@mixin leader($lines: 1, $font-size: $base-font-size, $property: margin) { |
| 142 |
#{$property}-top: rhythm($lines, $font-size); |
| 143 |
} |
| 144 |
|
| 145 |
// Apply leading whitespace as padding. |
| 146 |
@mixin padding-leader($lines: 1, $font-size: $base-font-size) { |
| 147 |
padding-top: rhythm($lines, $font-size); |
| 148 |
} |
| 149 |
|
| 150 |
// Apply leading whitespace as margin. |
| 151 |
@mixin margin-leader($lines: 1, $font-size: $base-font-size) { |
| 152 |
margin-top: rhythm($lines, $font-size); |
| 153 |
} |
| 154 |
|
| 155 |
// Apply trailing whitespace. The $property can be margin or padding. |
| 156 |
@mixin trailer($lines: 1, $font-size: $base-font-size, $property: margin) { |
| 157 |
#{$property}-bottom: rhythm($lines, $font-size); |
| 158 |
} |
| 159 |
|
| 160 |
// Apply trailing whitespace as padding. |
| 161 |
@mixin padding-trailer($lines: 1, $font-size: $base-font-size) { |
| 162 |
padding-bottom: rhythm($lines, $font-size); |
| 163 |
} |
| 164 |
|
| 165 |
// Apply trailing whitespace as margin. |
| 166 |
@mixin margin-trailer($lines: 1, $font-size: $base-font-size) { |
| 167 |
margin-bottom: rhythm($lines, $font-size); |
| 168 |
} |
| 169 |
|
| 170 |
// Shorthand mixin to apply whitespace for top and bottom margins and padding. |
| 171 |
@mixin rhythm($leader: 0, $padding-leader: 0, $padding-trailer: 0, $trailer: 0, $font-size: $base-font-size) { |
| 172 |
@include leader($leader, $font-size); |
| 173 |
@include padding-leader($padding-leader, $font-size); |
| 174 |
@include padding-trailer($padding-trailer, $font-size); |
| 175 |
@include trailer($trailer, $font-size); |
| 176 |
} |
| 177 |
|
| 178 |
// Apply a border and whitespace to any side without destroying the vertical |
| 179 |
// rhythm. The whitespace must be greater than the width of the border. |
| 180 |
@mixin apply-side-rhythm-border($side, $width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 181 |
@if not $relative-font-sizing and $font-size != $base-font-size { |
| 182 |
@warn "$relative-font-sizing is false but a relative font size was passed to apply-side-rhythm-border"; |
| 183 |
} |
| 184 |
border-#{$side}: { |
| 185 |
style: $border-style; |
| 186 |
width: $font-unit * $width / $font-size; |
| 187 |
}; |
| 188 |
padding-#{$side}: rhythm($lines, $font-size, $offset: $width); |
| 189 |
} |
| 190 |
|
| 191 |
// Apply borders and whitespace equally to all sides. |
| 192 |
@mixin rhythm-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 193 |
@if not $relative-font-sizing and $font-size != $base-font-size { |
| 194 |
@warn "$relative-font-sizing is false but a relative font size was passed to rhythm-borders"; |
| 195 |
} |
| 196 |
border: { |
| 197 |
style: $border-style; |
| 198 |
width: $font-unit * $width / $font-size; |
| 199 |
}; |
| 200 |
padding: rhythm($lines, $font-size, $offset: $width); |
| 201 |
} |
| 202 |
|
| 203 |
// Apply a leading border. |
| 204 |
@mixin leading-border($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 205 |
@include apply-side-rhythm-border(top, $width, $lines, $font-size, $border-style); |
| 206 |
} |
| 207 |
|
| 208 |
// Apply a trailing border. |
| 209 |
@mixin trailing-border($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 210 |
@include apply-side-rhythm-border(bottom, $width, $lines, $font-size, $border-style); |
| 211 |
} |
| 212 |
|
| 213 |
// Apply both leading and trailing borders. |
| 214 |
@mixin horizontal-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 215 |
@include leading-border($width, $lines, $font-size, $border-style); |
| 216 |
@include trailing-border($width, $lines, $font-size, $border-style); |
| 217 |
} |
| 218 |
|
| 219 |
// Alias for `horizontal-borders` mixin. |
| 220 |
@mixin h-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) { |
| 221 |
@include horizontal-borders($width, $lines, $font-size, $border-style); |
| 222 |
} |
| 223 |
|
| 224 |
#test-0 { |
| 225 |
unit: relative-unit(10px); |
| 226 |
unit: relative-unit(50%); |
| 227 |
rhythm: rhythm(); |
| 228 |
size: lines-for-font-size(15px); |
| 229 |
size: lines-for-font-size(16px); |
| 230 |
size: lines-for-font-size(17px); |
| 231 |
size: lines-for-font-size(27px); |
| 232 |
size: lines-for-font-size(37px); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
#test-1 { |
| 237 |
@include rhythm(5, 6, 7); |
| 238 |
} |
| 239 |
|
| 240 |
#test-2 { |
| 241 |
@include rhythm-borders; |
| 242 |
} |
| 243 |
|
| 244 |
#test-3 { |
| 245 |
@include horizontal-borders; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|