简体   繁体   English

减:将字体粗细添加到字体家族变量的最简单方法

[英]Less: Easiest way to add font-weight to font-family variables

I'm working on a system that uses LESS for styling. 我正在使用LESS进行样式设计的系统上工作。 The variables for the font families look like this: 字体系列的变量如下所示:

@font-family-regular: "robotoregular", sans-serif;
@font-family-bold: "robotobold", sans-serif;
@font-family-condensedregular: "roboto_condensedregular", sans-serif;
@font-family-condensedbold: "roboto_condensedbold", sans-serif;

Now I have to change the font family to Arial. 现在,我必须将字体系列更改为Arial。 I'm going to use Arial Narrow for the condensed fonts: 我将使用Arial Narrow压缩字体:

@font-family-regular: Arial, sans-serif;
@font-family-bold: Arial, sans-serif;
@font-family-condensedregular: "Arial Narrow", sans-serif;
@font-family-condensedbold: "Arial Narrow", sans-serif;

The problem is, that I can't set the font weight of the two bold font styles that way. 问题是,我无法以这种方式设置两种粗体字体样式的字体粗细。 I could add "font-weight: bold;" 我可以添加“ font-weight:粗体;” manually to all styles that use one of the bold families, but I would like to avoid that. 手动更改为使用大胆家族之一的所有样式,但我想避免这种情况。

Is there a way to tell LESS something like "for all elements: if font-family is set to @font-family-bold or @font-family-condensedbold add font-weight: bold;" 有没有一种方法可以告诉LESS诸如“对于所有元素:如果将font-family设置为@ font-family-bold或@ font-family-condensedbold,则添加font-weight:粗体;” or something like that? 或类似的东西? What would be the most elegant solution? 什么是最优雅的解决方案?

Thank you in advance. 先感谢您。

I fear you'll have to change the markup a little bit after all, but this is a solution i can offer, that might work for you: 我担心您毕竟必须稍微更改标记,但这是我可以提供的解决方案,可能对您有用:

@font-family-regular: "robotoregular", sans-serif;
@font-family-bold: "robotobold", sans-serif;
@font-family-condensedregular: "roboto_condensedregular", sans-serif;
@font-family-condensedbold: "roboto_condensedbold", sans-serif;

.font-face-bold (@font-select) when 
    (@font-select = @font-family-bold),
    (@font-select = @font-family-condensedbold) {
  font-weight: bold;
}

.font-face (@size, @font-select) {
    font: @size @font-select;
    .font-face-bold(@font-select);
}


// USAGE
.my-class {
    .font-face(14px, @font-family-regular);
}

.my-bold-class {
    .font-face(14px, @font-family-bold);
}

.my-condensed-class {
  .font-face(14px, @font-family-condensedregular);
}

.my-condensed-bold-class {
  .font-face(14px, @font-family-condensedbold);
}

What i do here is that i create two mixins that are nested in one another. 我在这里所做的是,我创建了两个嵌套在一起的mixin。 The inner one is conditional and only "does" something if the passed variable is either @font-family-bold or @font-family-condensedbold . 内部的是有条件的,并且仅在传递的变量是@font-family-bold@font-family-condensedbold时才“执行”操作。

Here is a Live Example of the code above. 这是上面代码的实时示例

Hopefully this works for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM