Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Sass adding spaces before units of measurement? [duplicate]

Tags:

css

sass

I'm attempting to follow this tutorial on responsive layout design, but using SASS/SCSS as my base specification language.

To that end, I have defined the following SCSS:

body {
  font: 100%/1.5;
}

h1 {
  $h1_size: 24;
  font-size: ($h1_size / 16)em;
  line-height:(28 / $h1_size)em;
  margin-top: (24 / $h1_size)em;
}

Unfortunately, the output from sass into CSS format looks like this:

h1 {
  font-size: 1.5 em;
  line-height: 1.167 em;
  margin-top: 1 em;
}

— with the unit separated from the value by a space. Chrome rejects these values as invalid, and only uses them if I manually go and remove the spaces myself.

Is there a way to fix this problem by tweaking my SCSS, or by passing an option to sass?

So far, I have tried:

  • placing the unit inside the calculation:
    • (font-size: ($h1_size / 16em) ) => syntax error
    • (font-size: (($h1_size)em / 16) => font-size: 24 em/16; which is the same problem I'm trying to fix
like image 566
andrewdotnich Avatar asked Jun 05 '12 01:06

andrewdotnich


People also ask

How many syntaxes that sass support?

Sass consists of two syntaxes.

What is main SCSS?

| `– main.scss // Main Sass file. Abstracts (or utilities): holds Sass tools, helper files, variables, functions, mixins and other config files. These files are meant to be just helpers which don't output any CSS when compiled. Base: holds the boilerplate code for the project.

How do I create a list in sass?

In Sass, elements in lists can be separated by commas ( Helvetica, Arial, sans-serif ), spaces ( 10px 15px 0 0 ), or slashes as long as it's consistent within the list. Unlike most other languages, lists in Sass don't require special brackets; any expressions separated with spaces or commas count as a list.


Video Answer


4 Answers

You can push the em into the $h1_size definition, which will let you simply divide by 16 and have a result in ems.

If both sides of the division are in em, your result will be unitless. You can easily multiply by 1em to get your unit back, if needed.

h1 {
  $h1_size: 24em;
  font-size: ($h1_size / 16);
  line-height:(28em / $h1_size);
  margin-top: (24em / $h1_size * 1em);
}

Multiplying by 1em can also make something closer to your original example work. You can generally keep things unitless, then only multiply by 1em when you want the unit to appear. This avoids some of the pointless em proliferation in my first example:

h1 {
  $h1_size: 24;
  font-size: ($h1_size / 16 * 1em);
  line-height:(28 / $h1_size);
  margin-top: (24 / $h1_size * 1em);
}

Which way makes more sense mostly just depends on if your output will mostly be in one unit, or all sorts of different ones (including none).

like image 146
John Flatness Avatar answered Oct 12 '22 01:10

John Flatness


You can remove the space with a +

h1 {
  $h1_size: 24;
  font-size: ($h1_size / 16)+em;
  line-height:(28 / $h1_size)+em;
  margin-top: (24 / $h1_size)+em;
}

Will output.

h1 {
  font-size: 1.5em;
  line-height: 1.16667em;
  margin-top: 1em; }
like image 30
Nick Long Avatar answered Oct 12 '22 01:10

Nick Long


You need to use string interpolation like so: #{$variable}em

Here is the reference: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#interpolation_

like image 44
Rob LaFeve Avatar answered Oct 11 '22 23:10

Rob LaFeve


Put the em into the variable, so $h1_size: 24em;

like image 36
Tom Pietrosanti Avatar answered Oct 12 '22 00:10

Tom Pietrosanti