字体大小要尽可能大以适合窗口宽度

时间:2018-10-16 08:02:58

标签: css font-size

是否有任何技巧可以将font-size设置得尽可能大,以使文本始终适合窗口宽度?

我尝试使用vw,但我确信CCS的功能要丰富得多。

.font{
font-family:Arial;
font-size:7vw;
display:table;
height:100vh;
width:100vw;
}

.inner{
display:table-cell;
vertical-align:middle;
text-align:center;

}
<div class=font>
<div class=inner>abcdefghijklmnopqrstuvwxyz</div>
</div>

谢谢。

1 个答案:

答案 0 :(得分:1)

好吧,您的代码已经非常聪明了。我真的看不到我们可以建立任何改进。

您绝对应该检查这个问题,指向似乎非常方便的Jquery插件:Auto-size dynamic text to fill fixed size container

因此,让我们将其作为第一个解决方案:

body {
  margin: 0;
}

.font {
  font-family: Arial;
  font-size: 49vw;
  display: table;
  height: 100vh;
  width: 100vw;
  margin: 0;
}

.inner {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<div class=font>
  <div class=inner>glee</div>
</div>

我可以建议的(尽管这对于语义学,屏幕阅读器来说是非常糟糕的……)也许正在尝试使用背景图片和background-size: contain;。请务必使用SVG文字!

body {
  margin: 0;
}

.font {
  font-family: Arial;
  font-size: 6vw;
  display: table;
  height: 100vh;
  width: 100vw;
  margin: 0;
}

.inner {
  display: table-cell;
  background: center no-repeat;
  background-size: contain;
  background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmc6c3ZnCiAgIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIKICAgeG1sbnM6Y2M9Imh0dHA6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL25zIyIKICAgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIgogICB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxucz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ZGlzcGxheTogbm9uZSIgLz4KICA8c2NyaXB0CiAgICAgaWQ9Imdsb2JhbFZhcnNEZXRlY3Rpb24iCiAgICAgc3JjPSJtb3otZXh0ZW5zaW9uOi8vYTAyOWVkZjktOWZhMS00YjhjLTlhZjYtZmIwNWUxMTM5YTFiL2pzL3dyc19lbnYuanMiIC8+Cjwvc3ZnOnN2Zz4K)
}
<div class=font>
  <div class=inner></div>
</div>

以防万一,您可能需要添加word-break: break-all,以确保即使由于某种原因未正确加载字体,文本仍然可读。