Font scaling based on width of container

后端 未结 30 2956
面向向阳花
面向向阳花 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.

    0 讨论(0)
  • 2020-11-21 05:28

    HTML

    <div style="height:100px; width:200px;">
      <div id='qwe'>
        test
      </div>
    </div>
    

    JavaScript code for maximizing font-size:

    var fontSize, maxHeight, maxWidth, textElement, parentElement;
    textElement = document.getElementById('qwe');
    parentElement = textElement.parentElement;    
    maxHeight = parentElement.clientHeight;
    maxWidth = parentElement.clientWidth;
    fontSize = maxHeight;
    var minFS = 3, maxFS = fontSize;
    while (fontSize != minFS) {
      textElement.style.fontSize = `${fontSize}px`;
      if (textElement.offsetHeight < maxHeight && textElement.offsetWidth <= maxWidth) {
        minFS = fontSize;
      } else{
        maxFS = fontSize;
      }
      fontSize = Math.floor((minFS + maxFS)/2);
    }
    textElement.style.fontSize = `${minFS}px`;
    
    0 讨论(0)
  • 2020-11-21 05:29

    Inside your CSS, try adding this at the bottom changing the 320 pixels width for wherever your design starts breaking:

        @media only screen and (max-width: 320px) {
    
            body { font-size: 1em; }
    
        }
    

    Then give the font-size in "px" or "em" as you wish.

    0 讨论(0)
  • 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:

    <script> document.documentElement.style.setProperty('--column-width', Math.min(window.innerWidth, 800)+'px'); </script>
    

    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.

    0 讨论(0)
  • 2020-11-21 05:30

    As a JavaScript fallback (or your sole solution), you can use my jQuery Scalem plugin, which lets you scale relative to the parent element (container) by passing the reference option.

    0 讨论(0)
  • 2020-11-21 05:32

    Try to use the fitText plugin, because Viewport sizes isn't the solution of this problem.

    Just add the library:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    

    And change font-size for correct by settings the coefficient of text:

    $("#text_div").fitText(0.8);
    

    You can set maximum and minimum values of text:

    $("#text_div").fitText(0.8, { minFontSize: '12px', maxFontSize: '36px' });
    
    0 讨论(0)
提交回复
热议问题