Html 为什么我的 <select> “font-family” 属性不是从 <body> 继承的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/413017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't my <select> "font-family" property inheriting from <body>?
提问by Sabya
My code:
我的代码:
body {
font-family:"Verdana",Arial,Helvetica,sans-serif;
}
.myfont {
font-family:"Verdana",Arial,Helvetica,sans-serif;
}
<body>
Hello
<select>
<option>
Hello
</option>
</select>
<select class="myfont">
<option>
Hello
</option>
</select>
</body>
Why is the first <select>
element not inheriting the font-family
property from the specification for <body>
?
为什么第一个<select>
元素没有font-family
从 的规范继承属性<body>
?
If I have to change the font for a <select>
why do I have to copy the style?
如果我必须更改字体,<select>
为什么我必须复制样式?
回答by James B
If you use:
如果您使用:
select {
font-family: inherit;
}
It'll work fine. CSS is a little quirky when it comes to form controls.
它会工作得很好。CSS 在表单控件方面有点古怪。
回答by GONeale
Yes font-family: inherit seems the best.
But otherwise:
是的 font-family: 继承似乎是最好的。
然而在其他方面:
body, .myfont
{
font-family:Verdana,Arial,Helvetica,sans-serif;
}
You can also replace '.myfont' for 'select' if you want all select elements to use this.
如果您希望所有选择元素都使用它,您还可以将 '.myfont' 替换为 'select'。
Hot tip. Do not use quotes around your font family names, it is not understood by all browsers.
热点提示。不要在您的字体系列名称周围使用引号,并非所有浏览器都能理解。
回答by nickf
James's answer seems the most elegant and correct to me, but I thought I'd just add another way to do it:
詹姆斯的回答对我来说似乎是最优雅和正确的,但我想我只是添加另一种方法来做到这一点:
.myfont option {
font-family: "Verdana"; /* etc */
}
回答by AmbroseChapel
If I have to change the font for a "select" why do I have to copy the style?
如果我必须更改“选择”的字体,为什么我必须复制样式?
Just so you know, even if you did have to "copy the style", you don't have to copythe style like you've done above.
请注意,即使您确实必须“复制样式”,也不必像上面那样复制样式。
You would just apply the samy style to body
and .myfont
by doing this:
您只需将 samy 样式应用于body
并.myfont
执行以下操作:
.myfont, body
{
font-family:"Verdana",Arial,Helvetica,sans-serif;
}