SASS和@ font-face

时间:2009-10-14 15:44:04

标签: css sass

我有以下CSS - 我如何在SASS中描述它?我试过用css2sass反向编译它,只是不断收到错误....是我的CSS(有效吗;-))?

@font-face {
  font-family: 'bingo';
    src: url("bingo.eot");
    src: local('bingo'),
       url("bingo.svg#bingo") format('svg'),
       url("bingo.otf") format('opentype');
}

3 个答案:

答案 0 :(得分:52)

万一有人想知道 - 这可能是我的css ......

@font-face
  font-family: "bingo"
  src: url('bingo.eot')
  src: local('bingo')
  src: url('bingo.svg#bingo') format('svg')
  src: url('bingo.otf') format('opentype')

将呈现为

@font-face {
  font-family: "bingo";
  src: url('bingo.eot');
  src: local('bingo');
  src: url('bingo.svg#bingo') format('svg');
  src: url('bingo.otf') format('opentype'); }

似乎足够接近......只需要检查SVG渲染

答案 1 :(得分:37)

我一直在努力解决这个问题。 Dycey的解决方案是正确的,因为指定src多次在css文件中输出相同的内容。但是,这似乎在OSX Firefox 23中破解(可能还有其他版本,但我没有时间进行测试)。

来自Font Squirrel的跨浏览器@font-face解决方案如下所示:

@font-face {
    font-family: 'fontname';
    src: url('fontname.eot');
    src: url('fontname.eot?#iefix') format('embedded-opentype'),
         url('fontname.woff') format('woff'),
         url('fontname.ttf') format('truetype'),
         url('fontname.svg#fontname') format('svg');
    font-weight: normal;
    font-style: normal;
}

要使用逗号分隔值生成src属性,您需要在一行上写入所有值,因为Sass中不支持换行符。要生成上述声明,您可以编写以下Sass:

@font-face
  font-family: 'fontname'
  src: url('fontname.eot')
  src: url('fontname.eot?#iefix') format('embedded-opentype'), url('fontname.woff') format('woff'), url('fontname.ttf') format('truetype'), url('fontname.svg#fontname') format('svg')
  font-weight: normal
  font-style: normal

我觉得多次写出这条路似乎很愚蠢,而且我不喜欢代码中过长的行,所以我通过编写这个mixin来解决这个问题:

=font-face($family, $path, $svg, $weight: normal, $style: normal)
  @font-face
    font-family: $family
    src: url('#{$path}.eot')
    src: url('#{$path}.eot?#iefix') format('embedded-opentype'), url('#{$path}.woff') format('woff'), url('#{$path}.ttf') format('truetype'), url('#{$path}.svg##{$svg}') format('svg')
    font-weight: $weight
    font-style: $style

用法:例如,我可以使用之前的mixin来设置Frutiger Light字体,如下所示:

+font-face('frutigerlight', '../fonts/frutilig-webfont', 'frutigerlight')

答案 2 :(得分:4)

对于那些正在寻找SCSS混音的人,包括 woff2

@mixin fface($path, $family, $type: '', $weight: 400, $svg: '', $style: normal) {
  @font-face {
    font-family: $family;
    @if $svg == '' {
      // with OTF without SVG and EOT
      src: url('#{$path}#{$type}.otf') format('opentype'), url('#{$path}#{$type}.woff2') format('woff2'), url('#{$path}#{$type}.woff') format('woff'), url('#{$path}#{$type}.ttf') format('truetype');
    } @else {
      // traditional src inclusions
      src: url('#{$path}#{$type}.eot');
      src: url('#{$path}#{$type}.eot?#iefix') format('embedded-opentype'), url('#{$path}#{$type}.woff2') format('woff2'), url('#{$path}#{$type}.woff') format('woff'), url('#{$path}#{$type}.ttf') format('truetype'), url('#{$path}#{$type}.svg##{$svg}') format('svg');
    }
    font-weight: $weight;
    font-style: $style;
  }
}
// ========================================================importing
$dir: '/assets/fonts/';
$famatic: 'AmaticSC';
@include fface('#{$dir}amatic-sc-v11-latin-regular', $famatic, '', 400, $famatic);

$finter: 'Inter';
// adding specific types of font-weights
@include fface('#{$dir}#{$finter}', $finter, '-Thin-BETA', 100);
@include fface('#{$dir}#{$finter}', $finter, '-Regular', 400);
@include fface('#{$dir}#{$finter}', $finter, '-Medium', 500);
@include fface('#{$dir}#{$finter}', $finter, '-Bold', 700);
// ========================================================usage
.title {
  font-family: Inter;
  font-weight: 700; // Inter-Bold font is loaded
}
.special-title {
  font-family: AmaticSC;
  font-weight: 700; // default font is loaded
}

$type参数对于堆叠具有不同权重的相关族非常有用。

@if是由于需要支持Inter font(类似于Roboto),它具有OTF,但目前没有SVG和EOT类型。

如果出现无法解决错误,请记住仔细检查字体目录($dir)。

相关问题