Font scaling based on width of container

后端 未结 30 2976
面向向阳花
面向向阳花 2020-11-21 04:35

I\'m having a hard time getting my head around font scaling.

I currently have a website with a body font-size of 100%. 100% of what though? This seems t

30条回答
  •  借酒劲吻你
    2020-11-21 05:29

    Use CSS Variables

    No one has mentioned CSS variables yet, and this approach worked best for me, so:

    Let's say you've got a column on your page that is 100% of the width of a mobile user's screen, but has a max-width of 800px, so on desktop there's some space on either side of the column. Put this at the top of your page:

    
    

    And now you can use that variable (instead of the built-in vw unit) to set the size of your font. E.g.

    p {
      font-size: calc( var(--column-width) / 100 );
    }
    

    It's not a pure CSS approach, but it's pretty close.

提交回复
热议问题