Font scaling based on width of container

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

    Here is the function:

    document.body.setScaledFont = function(f) {
      var s = this.offsetWidth, fs = s * f;
      this.style.fontSize = fs + '%';
      return this
    };
    

    Then convert all your documents child element font sizes to em's or %.

    Then add something like this to your code to set the base font size.

    document.body.setScaledFont(0.35);
    window.onresize = function() {
        document.body.setScaledFont(0.35);
    }
    

    http://jsfiddle.net/0tpvccjt/

提交回复
热议问题