CSS: 100% font size - 100% of what?

前端 未结 9 681
梦毁少年i
梦毁少年i 2020-11-30 17:35

There are many articles and questions about percentage-sized vs other-sized fonts. However, I can not find out WHAT the reference of the percent-value is supposed to be. I u

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 18:11

    According to ALL THE SPECS DATING BACK TO 1996, percentage values on font-size refer to the parent element's (computed) font-size.

    
    
    this font size is 12px!

    It's that easy.

    What if the div declares a relative font-size, like ems, or even worse, another percentage?? See “computed” above. Whatever absolute unit the relative unit converts to.

    The only question that remains is what happens when you use a percentage value on the root element, which has no parent:

    html {
      font-size: 62.5%; /* 62.5% of what? */
    }
    

    In that case, see the “duplicate” of this question. TLDR: percentages on the root element refer to the browser default font size, which might be different per user.

    References:

    • CSS 1 spec (1996)
    • CSS 2.1 spec (2011)
    • CSS Fonts Level 3 spec (2013)
    • CSS Fonts Level 3 editor’s draft (2017)

提交回复
热议问题