【问题标题】:how to set font size 100% of the browser window如何将字体大小设置为浏览器窗口的 100%
【发布时间】:2016-03-12 01:03:40
【问题描述】:

我正在尝试弄清楚如何将字体的大小设置为浏览器窗口的高度/宽度 = 100% 并使其具有响应性。

我知道它会被破坏 - 但我希望字体为 100% 宽度和 100% 高度。

基本上

<div (set to responsive 100% of browser)>
  <p class="100by100">
    sample
  </p>
</div>

其中“Sample”始终是 Bowser 宽度和高度的 100%。

谢谢

【问题讨论】:

  • 我的问题有点不同。我想强制字体大小为 h 的 100% 和 w 的 100。视口设置只是放大或缩小字体。
  • 所以您想拉伸文本以填充 both 维度的视口吗?使用font-size 是不可能的。你可能想看看transform:scale()
  • 字体大小按比例变化。视口的 100% 的 h 和 w 会导致字体失真,AFAIK,这是不可能的......也许你可以使用图像代替?

标签: javascript html css


【解决方案1】:

我得到了这个工作,但我必须使用 javascript 在内部元素上设置 transformhttps://jsfiddle.net/ynheeut3/

html:

<div id="container">
  <div id="fill">sample</div>
</div>

css:

#container {
  position: absolute;
  margin: 0;
  padding: 0;
  top: 0; bottom: 0; left: 0; right: 0;
  overflow: hidden;
}
#fill {
  margin: 0;
  display: inline-block;
  transform-origin: 0 0;
}

javascript:

function fillWithText () {
  var fill = document.getElementById('fill');
  var container = document.getElementById('container');

  var totalHeight = container.clientHeight;
  var totalWidth = container.clientWidth;

  var currentHeight = fill.clientHeight;
  var currentWidth = fill.clientWidth;

  var scaleX = totalWidth / currentWidth;
  var scaleY = totalHeight / currentHeight;

  fill.style.transform = 'scale(' + scaleX + ',' + scaleY + ')';
};
window.addEventListener('resize', fillWithText, false);
fillWithText();

【讨论】:

  • 真棒 - 这正是 - 我需要的 - 但是当我将所有代码放在 html 中时 - 如果我调整 Bowser 的大小,它只会占用全屏。是否需要加载?
  • 是的。代码必须在 DOM 初始化后运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 2014-09-08
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
相关资源
最近更新 更多