0

I try to construct an expression for a variable line height depending on the width using calc(). The result should be a fraction without units. Is it possible?

To be more precise, I would like to get something like line-height: 2; when 200rem / width < 2 and line-height: 1; when 200rem / width > 2

So, the above 200rem / width should somehow evaluate to a plain fraction. It may be using 100wh or similar. Please note that we don't know the width, we must use a variable!

For example the following:

li > a {
    line-height: calc(22px + (28 - 22) * ((100vw - 480px) / (3840 - 480)));
}

could do the job, but it evaluates to some pixels minus some width units instead of plain fraction without any units as I need.

Sorin GFS
  • 477
  • 3
  • 18
  • Could you use a media query? – ssc-hrep3 Nov 20 '19 at 21:20
  • @ssc-hrep3 No, my goal is to build something variable, not something conditional. – Sorin GFS Nov 20 '19 at 21:24
  • Does this answer your question? [css calc - round down with two decimal cases](https://stackoverflow.com/questions/37754542/css-calc-round-down-with-two-decimal-cases) – 04FS Nov 20 '19 at 21:40
  • @04FS Thank you, but no. That round function does work only if you specify the number, it does't do the math calculations. – Sorin GFS Nov 20 '19 at 21:50
  • @SorinGFS the main point was supposed to be this part, _"Unfortunately, there is not a native way in CSS to round (or ceil/floor) numbers."_ (SO's new phrasing for the "possible duplicate of" auto-comment is terrible IMHO.) – 04FS Nov 20 '19 at 21:56
  • @04FS Yes, I studied that function, is useless IMHO, it rounds .. a given string :)) – Sorin GFS Nov 20 '19 at 22:05

2 Answers2

0

Use a media query to specify styles depending on properties of the screen (width, for example). I learned the hard way not to use rem units when specifying screen limits so I did these calculations assuming a 14px rem value. You may need to adjust for your needs.

@media screen and (min-width: 1400) {
    body {
        line-height: 2;
    }
}

@media screen and (max-width: 1399) {
    body {
        line-height: 1;
    }
}

Read here for more on media queries

isick
  • 1,467
  • 1
  • 12
  • 23
  • Please read the question, I don't want to solve a problem here, I;m curious if is possible to get a width variable in calc to result plain fraction. – Sorin GFS Nov 20 '19 at 21:28
  • @SorinGFS CSS doesn't do variables (like that), so the short answer is no. If you want change styles dynamically you'll need to use JS to respond to changes in layout. – isick Nov 20 '19 at 21:29
  • @isick [CSS supports variables/custom properties.](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) – ssc-hrep3 Nov 20 '19 at 21:32
  • @ssc-hrep3 Those "variables" are not assignable at runtime. i.e., you can't store the width of an element into them (as asked in the question) – isick Nov 20 '19 at 21:35
  • @ssc-hrep3 I disagree. For example: `calc(22px + (28 - 22) * ((100vw - 480px) / (3840 - 480)))` , but in this case it evaluates to some pixels minus width units. I want plain fraction. – Sorin GFS Nov 20 '19 at 21:39
  • @SorinGFS what is a "plain fraction"? – isick Nov 20 '19 at 21:56
  • @isick A plain fraction is like: 1/3, 0.33 (without rem, px, or any other possible units) – Sorin GFS Nov 20 '19 at 22:13
  • @SorinGFS calc only does that under certain conditions `opacity: calc(1/3)` will give you an opacity value of 0.3. My understanding is the output is determined by the units of the input. If you want a number type, you may only use number or integer types in the calculation. Read more here: https://drafts.csswg.org/css-values-3/#calc-notation – isick Nov 20 '19 at 22:33
  • @isick Yes, for that we can use `100vw`, the example given in post is working without issues, except I need calc() function to evaluate to a plain fraction without units. For example (not related to our case) `20vw / 100vw` shoud evaluate to a plain fraction. I don't know if is possible, for that reason I asked the question! :) – Sorin GFS Nov 20 '19 at 22:45
0

You could use something like this:

.test-class {
  font-size: 20px;
  $width: 100px;

  @if (200rem / $width > 2) {
    line-height: 1;
  } @else {
    line-height: 2;
  }
}
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
  • Lol, how did you decide that the width is 100px? :) – Sorin GFS Nov 20 '19 at 21:35
  • This is not CSS - this is Sass - and it will not be executed by the browser; only the sass compiler at compile time. – isick Nov 20 '19 at 21:58
  • @isick You wrong again, this is plain css: https://developer.mozilla.org/en-US/docs/Web/CSS/calc – Sorin GFS Nov 20 '19 at 22:00
  • @isick I dod not mention any if anywhere, I have mentioned calc() – Sorin GFS Nov 20 '19 at 22:16
  • @SorinGFS I'm commenting on this answer submitted by ssc-hrep3... His answer uses Sass and would not work for your purposes. – isick Nov 20 '19 at 22:22
  • @isick Then you shoud address to his nick, not mine. – Sorin GFS Nov 20 '19 at 22:25
  • @SorinGFS I didn't address it to anyone's username, I simply posted it as a response to his answer (not your question). As someone who's been on SO for 4 years, you should know how it works by now... – isick Nov 20 '19 at 22:28
  • @isick I wasn't paying attention to your first comment, my bad. My last comment was for the comment when you said `There is no @if statement in CSS. That is Sass.` Sorry. – Sorin GFS Nov 20 '19 at 22:35
  • @SorinGFS You tagged your question with SASS, so an answer based on SASS should be valid. In this scenario, these 100px come from somewhere in your code at compile time. It was not absolutely clear, that you do want a runtime solution. It was also not clear what width stands for. I recommend you to clarify your question. – ssc-hrep3 Nov 20 '19 at 23:02
  • @ssc-hrep3 Ok, I remove the sass tag , but my question is cristal clear from the first phrase: `I try to construct an expression for a variable line height depending on the width using calc().` – Sorin GFS Nov 20 '19 at 23:04
  • It is not, as you do not describe what the width is and where you get it from. Each element has a width, the window has a width, a width could be a variable in compile time etc. – ssc-hrep3 Nov 20 '19 at 23:39