How to resize font-size proportionally to div size

孤人 提交于 2019-12-19 19:59:08

问题


I'm trying to create a website where the pages resize to the windows size on the fly. While we can get images and such to resize automatically as a % of the div size. We do not seem to be able to resize the text within a div to a relative % of the container.

This feature would need to work as the browser window itself is resized, without having to refresh the page.

The site is basic and is using js,css, html. The simplest method possible would be appreciated, but any solution is fantastic.

Thanks


回答1:


Whipped up some example to adjust font-size on div size change. Using percentage in the parent div and adjusting the font-size there should do the trick.

http://jsfiddle.net/744A2/




回答2:


So you can use screen.availHeight to calculate a constant value which you can multiply with the font size.

window.onload = function () {
    document.body.style.fontSize = (Math.ceil(screen.availHeight * parseInt(document.body.style.fontSize) / 600) + 'px';
}



回答3:


How about using media queries?




回答4:


even use % for text. http://w3schools.com/cssref/css_units.asp



来源:https://stackoverflow.com/questions/8639249/how-to-resize-font-size-proportionally-to-div-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!