Font scaling based on width of container

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

    I've prepared a simple scale function using CSS transform instead of font-size. You can use it inside of any container, you don't have to set media queries, etc. :)

    Blog post: Full width CSS & JS scalable header

    The code:

    function scaleHeader() {
      var scalable = document.querySelectorAll('.scale--js');
      var margin = 10;
      for (var i = 0; i < scalable.length; i++) {
        var scalableContainer = scalable[i].parentNode;
        scalable[i].style.transform = 'scale(1)';
        var scalableContainerWidth = scalableContainer.offsetWidth - margin;
        var scalableWidth = scalable[i].offsetWidth;
        scalable[i].style.transform = 'scale(' + scalableContainerWidth / scalableWidth + ')';
        scalableContainer.style.height = scalable[i].getBoundingClientRect().height + 'px';
      }
    }
    

    Working demo: https://codepen.io/maciejkorsan/pen/BWLryj

提交回复
热议问题