Skip to content

Instantly share code, notes, and snippets.

@juanigaray
Created August 14, 2020 19:38
Show Gist options
  • Save juanigaray/90ffe032ca9cea07cf6787a051de9e86 to your computer and use it in GitHub Desktop.
Save juanigaray/90ffe032ca9cea07cf6787a051de9e86 to your computer and use it in GitHub Desktop.
Line spacing in Sass css
/* The line-height value doesn't only define the distance between different lines
* but also distances the lines from everything around them.
* This mixin allows one to distance lines in a text while keeping the distance between a
* text and its surrounding elements constant.
* Assumes $line-height >= 1
*/
@mixin line-spacing($line-height: 1) {
line-height: $line-height;
// Negative margin to compensate for line-height pushing the first line downward / the last line upward
$compensation-margin: ($line-height - 1) / -2;
&:first-child {
margin-top: $compensation-margin;
}
&:last-child {
margin-bottom: $compensation-margin;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment