Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sass: calculating percentage with pixels

Tags:

css

sass

In one case, I need to combine a percentage variable with pixels in calculation.

Note It's not about keeping mixed units in generated css, so calc wouldn't work.

$fooPercent: 40%;

.abc {
  width: 100% - $fooPercent; // This works
}
.bar {
  width: 100px * (100% - $fooPercent);  // Intend to get 60px
}
like image 450
Xun Yang Avatar asked Dec 14 '25 17:12

Xun Yang


1 Answers

Dividing the percentage by 100% should give you a decimal which you can then multiply by the pixel value.

width: 100px * ((100% - $fooPercent) / 100%)
like image 195
dukedevil294 Avatar answered Dec 17 '25 14:12

dukedevil294



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!