{"id":3463,"date":"2017-03-30T15:24:50","date_gmt":"2017-03-30T13:24:50","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3463"},"modified":"2020-02-09T10:59:54","modified_gmt":"2020-02-09T09:59:54","slug":"sass-function-for-calculating-line-height-without-unit","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/sass-function-for-calculating-line-height-without-unit-3463\/","title":{"rendered":"SASS function for calculating line height without unit"},"content":{"rendered":"\n
This post shows you a SASS function, which calculates a line height without a unit. Origin of this action was that I no longer wanted to use Until recently I always used His proposal is to use Good question. I searched a while to find out if em is the right unit for \u00bbFor example, if an inner element has twice the font size of its parent, then the inherited value Part of the answer from Jukka K. Korpela to the question \u00bbEM\u2019s for line-height\u00ab on Stack Overflow<\/a>.<\/cite><\/p>Part of the answer from Jukka K. Korpela to the question \u00bbEM\u2019s for line-height\u00ab on Stack Overflow<\/a>.<\/cite><\/blockquote>\n\n\n\n In short: Use line heights without unit.<\/p>\n\n\n\n Like said, I already had created everything with The result of my considerations is the following SASS function:<\/p>\n\n\n You pass the element\u2019s font size, the ratio of the wanted line height to the base line height, the base line height, and the relative base font size (default is That is our starting point:<\/p>\n\n\n The line height of the Using the functions, that looks like this:<\/p>\n\n\n So we get the corresponding value without a unit and can be happy about a more flexible line height. ?<\/p>\n","protected":false},"excerpt":{"rendered":" This post shows you a SASS function, which calculates a line height without a unit. Origin of this action was that I no longer wanted to use rem.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[6],"tags":[],"wp-worthy-pixel":{"ignored":false,"public":"ae196a11a6ad4c5eb706126a37afcbf3","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/ae196a11a6ad4c5eb706126a37afcbf3"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3463"}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3463"}],"version-history":[{"count":3,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3463\/revisions"}],"predecessor-version":[{"id":5903,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3463\/revisions\/5903"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}rem<\/code>.<\/p>\n\n\n\n\n\n\n\n
What is the problem with
rem<\/code>?<\/h2>\n\n\n\n
rem<\/code> as the unit for line height and font size. Then I read the article \u00bbResponsive sizing with em-based design\u00ab by Keith J. Grant in the net magazine, which gave me a solution for a problem I had at this moment:<\/p>\n\n\n\n
rem<\/code>.<\/li>
em<\/code>.<\/li><\/ul>\n\n\n\n
em<\/code> and set the font size with
rem<\/code> on the top level of each component. With that, you can, for example, adjust the font size and spaces of the sidebar with changing the
rem<\/code> size of the
.sidebar<\/code>.<\/p>\n\n\n\n
What does that all have to do with the line height? ?<\/h3>\n\n\n\n
line-height<\/code>, and found an answer on Stack Overflow, which \u2014 among other things \u2014 says this:<\/p>\n\n\n\n
1.2<\/code> means that 1.2 times its own font size is used, which is OK. But if the parent had
line-height: 1.2em<\/code>, then the child would inherit a value that 1.2 times the parent\u2019s font size \u2013 which is much smaller than its own font size.\u00ab<\/p>
Problem with line height without unit and solution<\/h2>\n\n\n\n
rem<\/code>. Now I wanted to convert the
rem<\/code> line heights into values without unit, and that was not so simple \u2014 the calculation for that is slightly more complicated than for
rem<\/code> or
em<\/code>\u2026<\/p>\n\n\n\n
@function<\/span> line<\/span>-height<\/span>($font-size, $ratio-to-base-line-height, $base-line-height: $line-height, $relative-base-font-size: 1<\/span>)<\/span> <\/span>{\n $abs-base-font-size: $relative-base-font-size * 16<\/span>;\n $line-height: $base-line-height * $ratio-to-base-line-height;\n @return<\/span> ($relative-base-font-size * $abs-base-font-size * $line-height) \/ ($font-size * $abs-base-font-size);\n}<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n
1<\/code>) to the function. The function calculates the line height without a unit and returns it.<\/p>\n\n\n\n
Example<\/h3>\n\n\n\n
h1<\/span> {\n font-size<\/span>: 1.802em<\/span>;\n line-height<\/span>: 2.1rem<\/span>;\n}<\/code><\/div>Code language:<\/span> CSS<\/span> (<\/span>css<\/span>)<\/span><\/small><\/pre>\n\n\n
html<\/code> element is
1.4rem<\/code>, so we want the
h1<\/code> element to have a line height which is one and a half time so large.<\/p>\n\n\n\n
h1<\/span> {\n font-size<\/span>: 1.802em<\/span>;\n line-height<\/span>: line-height<\/span>(1.802<\/span>, 1.5<\/span>, 1.4<\/span>); \/* returns 1.24861 *\/<\/span>\n}<\/code><\/div>Code language:<\/span> CSS<\/span> (<\/span>css<\/span>)<\/span><\/small><\/pre>\n\n\n