首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为每个字体定义单独的字体-style- CSS

如何为每个字体定义单独的字体-style- CSS
EN

Stack Overflow用户
提问于 2013-03-28 13:33:52
回答 2查看 1.4K关注 0票数 2

我有一个超文本标记语言页面,包含各种font-style,如normalbolditalicsoblique等,使用CSS定义。我想为每个font-style使用不同的字体,例如,myboldfont用于boldmyitalicsfont用于italics等。我使用@font-face导入字体,例如,

代码语言:javascript
复制
@font-face {
    font-family: "MyNormal";
    font-style: normal;
    font-weight: normal;
    src: url("mynormalfont.woff") format("woff");
}
@font-face {
    font-family: "MyBold";
    font-style: normal;
    font-weight: normal;
    src: url("myboldfont.woff") format("woff");
}
@font-face {
    font-family: "MyItalics";
    font-style: normal;
    font-weight: normal;
    src: url("myitalicsfont.woff") format("woff");
}
@font-face {
    font-family: "MyOblique";
    font-style: normal;
    font-weight: normal;
    src: url("myobliquefont.woff") format("woff");
}

并导入普通、斜体、粗体和斜体字体。我将body样式定义为;

代码语言:javascript
复制
body{
  font-family: MyNormal, MyBold, MyItalics, MyOblique;
}

这是否足以定义主体中所有font-style的样式?我的意思是,如果我将font-style: italicfont-weight: bold赋值给一个元素,会使用斜体字体还是粗体字体?或者我应该怎么做才能做到这一点,这样如果我对任何元素使用font-weight: bold,就应该使用myboldfont.woff

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-28 13:36:08

这应该是可行的:

代码语言:javascript
复制
@font-face {
    font-family: "MyFont";
    font-style: normal;
    font-weight: normal;
    src: url("mynormalfont.woff") format("woff");
}
@font-face {
    font-family: "MyFont";
    font-style: normal;
    font-weight: bold;
    src: url("myboldfont.woff") format("woff");
}

还有这个:

代码语言:javascript
复制
...{
   font-family:"MyFont";
   font-weight:bold;
}...

然后,您将始终对font-family使用相同的名称,但您需要更改不同的属性。

票数 4
EN

Stack Overflow用户

发布于 2013-03-28 15:37:38

问题中列出的CSS是最安全的方法。当超过1个粗细或链接到字体系列名称的粗细或样式超过4个时,IE8会出现显示问题。对每个字体变体使用唯一的字体系列名称可以避免此问题。

当对每个字体变体使用唯一的字体系列名称时,没有必要在@ font -face声明或使用该字体的HTML元素的CSS规则中标识粗细或样式。

此外,要支持尽可能多的浏览器,请组合使用.woff、.ttf和.eot作为嵌入式字体。这是TypeKit使用的方法:

代码语言:javascript
复制
@font-face {
    font-family: 'MyNormal';
    src: url('mynormalfont.eot');
    src: url('mynormalfont.eot?#iefix')
            format('embedded-opentype'),
         url('mynormalfont.woff') format('woff'),
         url('mynormalfont.ttf') format('truetype');
}
@font-face {
    font-family: 'MyBold';
    src: url('myboldfont.eot');
    src: url('myboldfont.eot?#iefix')
            format('embedded-opentype'),
         url('myboldfont.woff') format('woff'),
         url('myboldfont.ttf') format('truetype');
}
@font-face {
    font-family: 'MyItalics';
    src: url('myitalicsfont.eot');
    src: url('myitalicsfont.eot?#iefix')
            format('embedded-opentype'),
         url('myitalicsfont.woff') format('woff'),
         url('myitalicsfont.ttf') format('truetype');
}
@font-face {
    font-family: 'MyOblique';
    src: url('myobliquefont.eot');
    src: url('myobliquefont.eot?#iefix')
            format('embedded-opentype'),
         url('myobliquefont.woff') format('woff'),
         url('myobliquefont.ttf') format('truetype');
}

字体变体的用法如下:

代码语言:javascript
复制
p {
    font-family: MyNormal, sans-serif;   /* Still useful to add fallback fonts */
    font-size: 12px;
}
h2 {
    font-family: MyBold, sans-serif;
    font-size: 20px;
}

缺点是,必须在使用其中一种字体变体的每个CSS规则中指定字体系列名称。但这可以被认为是目前广泛的跨浏览器支持所必须付出的代价。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15674906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档