css - 通过增加字体填充剩余空间
问题描述
我有一个问题,我想通过尽可能增加(和减少)字体大小来填充框的剩余(非常动态)空间。
http://jsfiddle.net/urqL1evn/2
现在,我只有:
.textbox {
margin: 0 5em;
margin-top: 0.5em;
width: 80%;
font-size: 30px;
white-space: pre-wrap;
}
该页面将在 1920x1080 50" 电视上以 100% 的窗口缩放比例观看。
我将添加和删除部分,直到屏幕上只有一个,最多 4 个。
非常感谢任何帮助!
解决方案
这是您的问题的解决方案:
$(document).ready(function() {
$('.textbox').each(function() {
let that = $(this),
textBoxHeight = parseInt(that.height()) + 20, // +20 its sum of margin-top + margin-bottom of .textbox element
parentHeight = parseInt(that.parent().height()),
fontSize = parseInt(that.css('font-size'))
while(textBoxHeight > parentHeight)
{
fontSize -= 1
that.css({'font-size':fontSize+'px'})
textBoxHeight = parseInt(that.height()) + 20 // +20 its sum of margin-top + margin-bottom of .textbox element
}
})
})
http://jsfiddle.net/urqL1evn/21/
CSS 已更改(em > px 中的边距)
.textbox {
margin: 10px 20px;
width: 80%;
font-size: 100px;
white-space: pre-wrap;
}
因此,您需要设置.textbox
font-size
为100px
,然后在 JS 中我们一直将其降低,直到.textbox
元素的高度不等于或低于它的父元素。这很简单,而且效果很好。
我希望这是您问题的答案。
不要忘记使用 jQuery 库。
推荐阅读
- angular - Right click inspect element on IE11 developer tools always to to top of HTML
- gitlab - Gitlab Runner 作业失败
- android - DatePicker 的 MinDate 有问题吗?
- javafx - JavaFX:是否可以删除 ColumnResizePolicy?
- javascript - 如何使用不是来自 jquery 的 javascript 给出唯一的 html 类名
- pentaho-cde - 在 Pentaho CDE 上更改表格样式?
- jquery - 如何搜索特定属性及其值并为其分配一些 css
- pytorch - Pytorch 将部分训练值设置为零
- angular - Primeng Dropdown 未绑定 Angular FormArray 中的选项
- kubernetes - GKE 节点绑定到网关