Font scaling based on width of container

后端 未结 30 2969
面向向阳花
面向向阳花 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:27

    Pure-CSS solution with calc(), CSS units and math

    This is precisely not what OP asks, but may make someone's day. This answer is not spoon-feedingly easy and needs some researching on the developer end.

    I came finally to get a pure-CSS solution for this using calc() with different units. You will need some basic mathematical understanding of formulas to work out your expression for calc().

    When I worked this out, I had to get a full-page-width responsive header with some padding few parents up in DOM. I'll use my values here, replace them with your own.

    To mathematics

    You will need:

    • Nicely adjusted ratio in some viewport. I used 320 pixels, thus I got 24 pixels high and 224 pixels wide, so the ratio is 9.333... or 28 / 3
    • The container width, I had padding: 3em and full width so this got to 100wv - 2 * 3em

    X is the width of container, so replace it with your own expression or adjust the value to get full-page text. R is the ratio you will have. You can get it by adjusting the values in some viewport, inspecting element width and height and replacing them with your own values. Also, it is width / heigth ;)

    x = 100vw - 2 * 3em = 100vw - 6em
    r = 224px/24px = 9.333... = 28 / 3
    
    y = x / r
      = (100vw - 6em) / (28 / 3)
      = (100vw - 6em) * 3 / 28
      = (300vw - 18em) / 28
      = (75vw - 4.5rem) / 7
    

    And bang! It worked! I wrote

    font-size: calc((75vw - 4.5rem) / 7)
    

    to my header and it adjusted nicely in every viewport.

    But how does it work?

    We need some constants up here. 100vw means the full width of viewport, and my goal was to establish full-width header with some padding.

    The ratio. Getting a width and height in one viewport got me a ratio to play with, and with ratio I know what the height should be in other viewport width. Calculating them with hand would take plenty of time and at least take lots of bandwidth, so it's not a good answer.

    Conclusion

    I wonder why no-one has figured this out and some people are even telling that this would be impossible to tinker with CSS. I don't like to use JavaScript in adjusting elements, so I don't accept JavaScript (and forget about jQuery) answers without digging more. All in all, it's good that this got figured out and this is one step to pure-CSS implementations in website design.

    I apologize of any unusual convention in my text, I'm not native speaker in English and am also quite new to writing Stack Overflow answers.

    It should also be noted that we have evil scrollbars in some browsers. For example, when using Firefox I noticed that 100vw means the full width of viewport, extending under scrollbar (where content cannot expand!), so the fullwidth text has to be margined carefully and preferably get tested with many browsers and devices.

提交回复
热议问题