Nested inside a

tag giving different font size

后端 未结 2 421
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 17:59

Applying an enlarged font-size using em directly to a

tag and to the nested tag are giving different results.

相关标签:
2条回答
  • 2021-01-20 18:04

    You can use rem (root em) instead of em

    em is relative to the parent element, while rem takes reference from the root (html) value

    The css code becomes

    body {
     font-size: 14px;
    }
    
    p {
     font-size: 1.4em;
    }
    
    .enlarge {
      font-size: 1.7rem;
    }
    

    The code can be tested here: https://jsfiddle.net/zrzahoro/

    Update:

    The rem takes reference from root, which it html tag. If you want to control the value of font being referenced in your css instead of letting it use the browser default value, give a fixed font-size to html tag too.

    0 讨论(0)
  • 2021-01-20 18:10

    "The em unit is a relative unit based on the computed value of the font size of the parent element. This means that child elements are always dependent on their parent to set their font-size."

    Reference: CSS-Tricks

    Your <p> tag has already 1,4 of normal size and <span class="enlarge"> is 1,7 of its parent (which is already enlarged).

    0 讨论(0)
提交回复
热议问题