使用字体的目的是什么? [英] What is the purpose of using font: inherit?

查看:115
本文介绍了使用字体的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道为什么在层叠样式表中使用font: inherit;.

I just wanted to know why font: inherit; is used in Cascading Style Sheets.

推荐答案

就像其他答案所说的那样,它是从父元素继承CSS属性.

Like the other answers have said, it’s to inherit a CSS property from the parent element.

其他答案未能说明的是您为什么需要这个.因为毕竟毕竟CSS属性是继承的,对吧?

What the other answers have failed to say is why you’d need this. Because, after all, CSS properties are inherited anyway, right?

嗯,不.默认情况下,大多数都是(例如,链接颜色不是从父元素继承的).但是考虑这种情况:

Well, no. Most are, by default (but link colour isn’t inherited from the parent element, for instance). But consider this case:

p { color: blue; }

div.important { color: red; }

<div class="important">
    <p>This is a text</p>
</div>

现在,文本将为蓝色,而不是红色.如果我们希望<p>具有其父级样式而不是其默认样式,则必须覆盖其CSS.我们当然可以重复属性值(red),但这违反了DRY(请勿重复自己).相反,我们继承它:

Now the text will be blue, not red. If we want the <p> to have its parent’s styling rather than its default styling, we have to override its CSS. We could of course repeat the property value (red) but that violates DRY (don’t repeat yourself). Instead, we inherit it:

div.important p { color: inherit; }

这篇关于使用字体的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆