html - Less:将字体粗细添加到字体系列变量的最简单方法

标签 html css fonts less

我正在开发一个使用 LESS 进行样式设置的系统。字体系列的变量如下所示:

@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;

现在我必须将字体系列更改为 Arial。我将使用 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;

问题是,我无法通过这种方式设置两种粗体样式的字体粗细。我可以添加“字体粗细:粗体;”手动添加到所有使用粗体系列之一的样式,但我想避免这种情况。

有没有办法告诉 LESS 类似“对于所有元素:如果 font-family 设置为 @font-family-bold 或 @font-family-condensedbold add font-weight: bold;”或类似的东西?什么是最优雅的解决方案?

提前谢谢你。

最佳答案

我担心您毕竟必须稍微更改标记,但这是我可以提供的解决方案,可能对您有用:

@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);
}

我在这里所做的是创建两个相互嵌套的混入。内部变量是有条件的,只有当传递的变量是 @font-family-bold@font-family-condensedbold 时才会“做”某事。

这是一个Live Example上面的代码。

希望这对你有用。

关于html - Less:将字体粗细添加到字体系列变量的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47773283/

相关文章:

javascript - 在推送的 Canvas 对象上使用 drawimage() 时出现错误消息,而 preloading() 没有帮助

JavaScript if 语句不应该返回 true

html - 服务器获取旧版本的图像?

javascript - 在添加类的情况下,CSS 3 Transition 是否不能与模板元素 (HTML 5) 一起使用...?

python-3.x - 有没有办法在 Pygame 中使用更多字体?

javascript - 每当 div 在 mootools 中增加或减少时如何调用函数

html - 使用 float 和 margin-left 创建 html 布局的问题

ipad - 数学符号字体

java Graphics2D 的drawString质量

javascript - 使用 html5 Canvas 作为背景,如何使用 css 将 youtube 嵌入到它上面以独立于分辨率?