【问题标题】:Font Face Mixin for Less更少的字体混合
【发布时间】:2013-08-02 14:23:46
【问题描述】:

使用 Less 我将字体系列定义如下:

@georgia: georgia, times, 'times new roman', 'nimbus roman no9 l', serif; 

然后我有更少的mixin:

.font(@family: arial, @size: 1.0em, @lineheight: 1.0, @weight: normal, @style: normal, @variant: normal) {
  font: @style @variant @weight @size~"/"@lineheight @family;
} // font

最后我是这样使用的:

p {
  .font(@georgia, 1.0em, 1.5)
}

但我也想用自定义字体定义字体系列:

@something: 'custom-font', arial, ...;

但首先我需要注册自定义字体:

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

是否可以为@font-face 创建一个 LESS mixin,这样我就可以传递字体名称和路径并注册字体而无需重复所有这些代码?

我不确定如何将它与我的字体混合...

或者如果有更好的方法来做到这一点......

谢谢你, 米格尔

【问题讨论】:

    标签: css less


    【解决方案1】:

    您可以定义自定义 mixin 以包含自定义字体,但由于 LESS 不实现任何控制指令,仅保护 mixin(在当前问题方面无用),您不能缩短字体的 mixin 代码定义。

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

    【讨论】:

      【解决方案2】:

      我知道它是一个旧帖子但是,我这样解决了这个问题,希望它可以帮助其他人。

      首先,我创建了一个参数 mixin,其中包含将在 @font-face 内重复的所有内容:

      .loadFont(@Weight; @Name; @Local; @Eot:'@{Local}.eot'; @Woff:'@{Local}.woff'){
          font-family: 'Open Sans';
          font-style: normal;
          font-weight: @Weight;
          src: url(@Eot);
          src: local(@Name), local(@Local), url(@Eot) format('embedded-opentype'), url(@Woff) format('woff');
      }
      

      然后加载我所有的网络字体(在这种情况下打开 sans)

      @font-face {.loadFont(100;'Roboto Thin';'Roboto-Thin')}
      @font-face {.loadFont(300;'Roboto Light';'Roboto-Light')}
      @font-face {.loadFont(400;'Roboto Regular';'Roboto-Regular')}
      @font-face {.loadFont(500;'Roboto Medium';'Roboto-Medium')}
      @font-face {.loadFont(700;'Roboto Bold';'Roboto-Bold')}
      @font-face {.loadFont(900;'Roboto Black';'Roboto-Black')}
      

      然后使用 CSS 规则创建第二个参数 mixin 以应用于元素

      .font(@weight:400; @size:14px; @font-family:'Open Sans', sans-serif){
          font:@arguments;
      }
      

      最后我把它用在我的元素上

      div.someClass{
          .font(); //to accept all default parameters 
      }
      div.otherClass{
          .font(100; 40px); //to specify weight and size
      }
      

      作为旁注。我在LESS 文件旁边有我所有的*.eot*.woff 文件并命名为@Local 参数(Open-Sans.woff || Open-Sans.eot

      【讨论】:

      • 我只是再次(慢慢地)阅读了已接受的答案,现在觉得很愚蠢 xD
      【解决方案3】:

      我喜欢在文件夹中组织我的字体,如下所示:

      /fonts/OpenSans-Bold/OpenSans-Bold.eot
      /fonts/OpenSans-Bold/OpenSans-Bold.svg
      /fonts/OpenSans-Bold/OpenSans-Bold.ttf
      /fonts/OpenSans-Bold/OpenSans-Bold.woff
      

      我将这个 LESS MIXIN 用于字体:

      .font-face(@fontName: sans-serif; @fontWeight: normal; @fontStyle: normal) {
          @font-face {
              font-family: @fontName;
              src: url('@{pTheme}/fonts/@{fontName}/@{fontName}.eot');
              src: url('@{pTheme}/fonts/@{fontName}/@{fontName}.eot?#iefix') format('embedded-opentype'),
                   url('@{pTheme}/fonts/@{fontName}/@{fontName}.woff') format('woff'),
                   url('@{pTheme}/fonts/@{fontName}/@{fontName}.ttf') format('truetype'),
                   url('@{pTheme}/fonts/@{fontName}/@{fontName}.svg#@{fontName}') format('svg');
              font-weight: @fontWeight;
              font-style: @fontStyle;
          }
      }
      

      地点:

      @pTheme: "../../themes/[THEME NAME]";
      

      mixin 调用示例:

      .font-face('OpenSans-Bold');
      

      我已经创建了@pTheme 变量,因此我也可以在背景图像上使用它,如下所示:

      background: url("@{pTheme}/images/logo.png") no-repeat;
      

      【讨论】:

        【解决方案4】:

        LESS 确实支持带有参数的 mixin。见这里:http://lesscss.org/#-parametric-mixins

        【讨论】:

          【解决方案5】:

          你可以试试这个 mixin for font-face (font Proxima Nova as expl):

          .fontFace(@fontWidth) {
              @fontName: "Proxima Nova @{fontWidth}";
              @fileName: "../fonts/ProximaNova-@{fontWidth}";
              @font-face {        
                  font-family: '@{fontName}';
                  font-weight: 400;
                  font-style: normal;
                  src: url('@{fileName}.eot');
                  src: url('@{fileName}.eot?#iefix') format('embedded-opentype'),
                       url('@{fileName}.woff') format('woff'),
                       url('@{fileName}.ttf') format('truetype'),
                       url('@{fileName}.svg#@{fontName}') format('svg');
              }
          }
          
          .fontFace('Regular');
          .fontFace('RegularIt');
          .fontFace('Bold');
          
          @fontName: Proxima Nova;
          @font:          "@{fontName} Regular";
          @font_italic:   "@{fontName} RegularIt";
          @font_bold:     "@{fontName} Bold";
          
          h2 {
              font: 400 50px @font_bold;  
          }
          

          同样,SASS/SCSS 的这位员工:

          @mixin font ($weight) {
              @font-face {
                  $fontName: 'Proxima Nova ' + $weight;
                  $fileName: '../fonts/ProximaNova-' + $weight;
          
                  font-family: $fontName;
                  font-weight: 300;
                  font-style: normal;
                  src: url($fileName + '.eot ');
                  src: url($fileName + '.eot?#iefix') format('embedded-opentype'),
                     url($fileName + '.woff2') format('woff'),
                     url($fileName + '.ttf') format('truetype'),
                     url($fileName + '.svg##' + $fontName) format('svg');
              }
          }
          @include font(regular);
          @include font(bold);
          @include font(light);
          
          $fontName: 'Proxima Nova ';
          $font: $fontName + regular, "Helvetica Neue", sans-serif;
          $font_bold: $fontName + bold;
          $font_light: $fontName + light;
          
          h2 {
              font: 300 15px $font_bold;
          }
          

          【讨论】:

            【解决方案6】:

            我知道这个问题有点老了,但我回答 Mészáros Lajos' 。它需要考虑一些目前没有的事情:

            1. 您的字体系列和字体文件名不需要匹配;事实上,对于“粗体”和“斜体”,您可能需要使用样式链接。请参阅:http://www.smashingmagazine.com/2013/02/14/setting-weights-and-styles-at-font-face-declaration/。这可以防止当浏览器不理解字体之间的关系时发生“仿粗体”和“仿斜体”(例如,当您有一个强大的标签时,它不知道 MyFont-Bold 是正确的字体)使用 MyFont 的文本)。
            2. 字体系列名称和 SVG id 不需要匹配;例如,文件 montserrat-regular.svg 的 SVG ID 可能为 montserratregular。是的,您可以通过重命名文件来解决此问题,也许您应该这样做,但如果您是远程获取文件 (CDN),您可能就没有那么奢侈了。
            3. 个人喜好,我换了变体和重量的顺序。在 CSS 字体速记中,variant 是第一位的,所以为了保持相似,我把它放在第一位。
            4. 再次,个人喜好,我删除了默认值,因为我不想在没有明确说明的情况下调用此方法。当您必须指定所有内容时,您不太可能忘记您确实需要更改该字体的 SVG ID。
            5. 由于 Chrome-windows 目前不支持抗锯齿 WOFF,因此我在 EOT 之后立即切换了移动 SVG 的顺序(旧 IE 需要先进行 EOT 否则会中断)。 Credit

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

            【讨论】:

              【解决方案7】:

              这是我对 SASS 的演绎,经过测试和工作。

              @mixin fontface($family: sans-serif, $weight: normal, $style: normal, $path: '../fonts', $filename: 'font'){ @font-face{ font-family: $family; src:url('#{$path}/#{$filename}.eot'); src:url('#{$path}/#{$filename}.eot?#iefix') format('embedded-opentype'), url('#{$path}/#{$filename}.woff') format('woff'), url('#{$path}/#{$filename}.ttf') format('truetype'), url('#{$path}/#{$filename}.svg#icon') format('svg'); font-weight: $weight; font-style: $style; } }

              【讨论】:

              • 问题是关于 LESS。您应该考虑找到有关 SASS 的类似问题,在此处发布此答案,然后删除此答案。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-01-10
              • 2015-10-07
              • 1970-01-01
              • 2011-07-05
              • 2014-06-19
              • 2011-08-04
              相关资源
              最近更新 更多