简体   繁体   English

如何覆盖全局样式表

[英]How to override global stylesheet

In a nutshell, there's a global stylesheet: 简而言之,有一个全局样式表:

a { font-family: Arial; }

I want to use a different font family for a particular link: 我想为特定链接使用不同的字体系列:

<a href="..." style="font-family: Helvetica;">...</a>

or 要么

<span style="font-family: Helvetica;"><a href="...">...</a></span>

but nothing works. 但没有任何作用。 Is there an easy way to do this? 是否有捷径可寻?

PS I'm dynamically (via PHP) assign different fonts to different links, so creating a special class is not an option. PS我是动态的(通过PHP)为不同的链接分配不同的字体,因此创建一个特殊的类不是一个选项。

Unless you have a specific font named Helvetica, you should realise that on some platforms (such as Windows, via FontSubstitutes ), Helvetica is aliased to Arial. 除非你有一个名为Helvetica的特定字体,否则你应该意识到在某些平台上(例如Windows,通过FontSubstitutes ),Helvetica是别名的Arial。 That might be the source of the problem. 这可能是问题的根源。 Try another font and see. 尝试另一种字体,看看。

Your first attempt 你的第一次尝试

  <a href="..." style="font-family: Helvetica;">...</a>

should have worked. 应该有用。 Agree that you're probably missing a font. 同意你可能错过了一种字体。 Inline styles have precedence over any other styles beside a user-defined style sheet. 内联样式优先于用户定义样式表旁边的任何其他样式。 Here's the order of priorities for style definitions: 以下是样式定义的优先顺序:

  1. User defined style 用户定义的样式
  2. Embedded or inline style sheet 嵌入式或内联样式表
  3. Internal style sheet 内部样式表
  4. External style sheet 外部样式表
  5. Browser default style 浏览器默认样式

Within a style sheet the priorities are as follows: 在样式表中,优先级如下:

  1. Anything marked !important 任何标记的东西!重要的
  2. id ID

  3. .class 。类
  4. element 元件

In addition, you have the rule of greater specificity: div a overrides a . 此外,你有更大的特殊性规则: div a覆盖a

Here's a good article with more detail on the subject . 这是一篇很好的文章,详细介绍了这个主题

@Kip's suggestion is your best bet. @Kip的建议是你最好的选择。

What you've written should work, unless the problem is what Chris pointed out, 除非问题是克里斯指出的,否则你所写的内容应该有效。

When you get a pair of fonts for which this works correctly, you might consider that a better way of doing this would be to declare a class for the special links that somehow reminds yourself of why they need a separate font (maybe because you want them to be especially noticed?) 当你得到一对正常工作的字体时,你可能会认为更好的方法是为特殊链接声明一个类,以某种方式提醒自己为什么需要一个单独的字体(也许是因为你想要它们)要特别注意?)

a { font-family: Arial; }
a .noticed { font-family: Helvetica; }

Then in HTML: 然后在HTML中:

<a class="noticed" href="...">...</a>

Changing the font by creating a span tag around the link, or adding inline style to the link just smacks of the old days of <font> tags. 通过在链接周围创建一个span标签来更改字体,或者为链接添加内联样式只是过去的<font>标签。

element styles override global styles, so Chris Jester-Young is probably right and you don't actually have a Helvetica font; 元素样式覆盖全局样式,所以Chris Jester-Young可能是正确的,你实际上并没有Helvetica字体; try a different font eg Courier or Times New Roman that you're certain exists 尝试不同的字体,例如Courier或Times New Roman,你肯定存在

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

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