Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slashes (`/`) in CSS values when using Less (e.g. in `font` shorthand)

Tags:

css

less

i noticed an issue when using Less with font shorthand

.font(@weight: 300, @size: 22px, @height: 32px) {
    font: @weight @size/@height "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif;
}

the above fails with

this.a.toCSS is not a function
http://localhost/tumblr/modern1/css/style.less on line 1, column 0:
1. @highlight: #cb1e16;
2. @shade1: #cb1e16;

when i split the properties up it works

.font(@weight: 300, @size: 22px, @height: 32px) {
  font-weight: @weight;
  font-size: @size;
  line-height: @height;
  font-family: "Yanone Kaffeesatz", "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif;

}

i think its because of the slash / thats causing the problem, i think since Less can do calculations, eg. 2px + 5 = 7px its trying to do a divide?

like image 520
Jiew Meng Avatar asked Jun 22 '10 04:06

Jiew Meng


People also ask

When using the font shorthand property What are the required properties You must include?

Two of the values in font shorthand are mandatory: font-size and font-family . If either of these is not included, the entire declaration will be ignored.

Which one of the following is the correct shorthand notation for fonts in CSS?

CSS Font Property To shorten the code, it is also possible to specify all the individual font properties in one property. The font property is a shorthand property for: font-style. font-variant.

Which is the correct order for font shorthand property?

5.5 CSS font shorthand The font property regroups (in this particular order): font-style. font-variant. font-weight.

What is forward slash in CSS?

The forward slash is required syntax for separating background position values and background size values. This is to eliminate misinterpretation of the values specified. If you're going to specify both, then background-position has to come first, then the forward slash, then the background-size.


2 Answers

Just ran into this issue, the escape function (for less.js anyway) is: e() Like this

font: @weight @size e('/') @height "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif;
like image 173
Jan Drewniak Avatar answered Oct 18 '22 16:10

Jan Drewniak


The forward slash / character is causing the LESS compiler to divide your font-size by your line-height. You can:

  1. Separate your CSS into non-shorthand, separate rules

    font-size: @size; line-height: @height;

  2. Escape some or all of your LESS font shorthand rule. The slash / itself is the best part to escape. You can use the e, e("/") escape syntax, or the newer, better documented tilde-quote ~"/". You can also use the LESS string interpolation @{} syntax to insert your variables.

Try this:

font: @weight @size~"/"@height "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif;

Or this:

font: @weight ~"@{size}/@{height}" "Helvetica Neue", Arial, "Liberation Sans", FreeSans, sans-serif;
like image 36
Nathan Strutz Avatar answered Oct 18 '22 17:10

Nathan Strutz