【问题标题】:Why is 'font-family' not inherited in '<button>' tags automatically?为什么 'font-family' 不会自动在 '<button>' 标签中继承?
【发布时间】:2014-11-26 05:11:03
【问题描述】:

我注意到在&lt;button&gt; 标签的情况下,font-family 不会自动继承;要么我必须像这样明确指定它:

<button style="font-family: some_style;">Button</button> 

或像这样使用继承属性:

<button style="font-family: inherit;">Button</button> 

但是,font-family 在其他标签的情况下会自动继承,例如&lt;a&gt; 标签。

为什么&lt;button&gt; 标签会出现这个问题?

这是一个DEMO

【问题讨论】:

标签: html css fonts


【解决方案1】:

表单元素不继承字体设置,您必须手动设置这些属性。

如果您使用字体声明,例如。身体,

body {font-family: arial, sans-serif}

只使用

body, input, textarea, button {font-family: arial, sans-serif}

input, textarea, button {font-family: inherit}

【讨论】:

  • 哈哈,我认为这甚至不会针对他的&lt;button&gt; 元素。
【解决方案2】:

如果您在浏览器中使用其开发工具检查您的演示,您会看到button 元素的字体系列来自浏览器样式表。它们以不同的方式显示这一点,并且它们可能在那里使用不同的字体,但原理是相同的:some 样式表中元素的 font-family 属性声明,因此该属性不能被继承(当然,除非您在其上显式设置值 inherit)。

这在规范中没有定义,但它们也没有禁止这样的浏览器样式表设置,它们是常见的做法。

【讨论】:

    【解决方案3】:

    我知道这是一个老问题,我回答晚了。 @panther 和 @jukka-k-korpela 都正确回答了这个问题。但是,他们缺少有关修复精确问题的关键信息以及@nikunj-madhogaria 提供的演示。我尝试根据问题完成答案。

    由于问题是关于button 标签,将button 添加到@panther 提供的CSS 修复可能是明智的。所以,这里是按钮标签的正确修复:

    button { font-family: inherit }
    

    【讨论】:

      【解决方案4】:

      旧回复,但它可能对某些人有所帮助。我添加了以下内容:

      * {
           font-family: arial, sans-serif
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-12
        • 2017-10-22
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多