使用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, ...;
但首先我需要注册custom-font:
@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,这样我就可以传递字体名称和路径并注册字体而不重复所有这些代码?
我不确定如何将其与我的字体混合...
或者,如果有更好的方法可以做到这一点......
谢谢你, 米格尔
答案 0 :(得分:16)
你可以定义自定义mixin以包含自定义字体,但由于LESS没有实现任何控制指令,只有mixins的守卫(在当前问题的方面是无用的),你不能缩短mixin&#39 ; s代码为font-face定义。
.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;
}
}
答案 1 :(得分:4)
我知道这是一个老帖子但是我这样解决了这个问题,希望能帮助其他人。
首先,我创建了一个参数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');
}
然后加载我的所有网络字体(在本例中为open 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
}
作为旁注。我的所有*.eot
和*.woff
文件都位于LESS
文件旁边,并命名为@Local
参数(Open-Sans.woff
|| Open-Sans.eot
)< / p>
答案 2 :(得分: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:
.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;
答案 3 :(得分:1)
LESS确实支持mixins和参数。见这里:http://lesscss.org/#-parametric-mixins
答案 4 :(得分:0)
我知道这个问题有点陈旧,但我跟Mészáros Lajos&#39;回答。它需要考虑一些目前尚无法解决的问题:
.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;
}
答案 5 :(得分:0)
你可以试试这个mixin for font-face(字体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 :(得分:-4)
这是我对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;
}
}