【发布时间】:2018-10-29 08:51:38
【问题描述】:
我想知道是否可以使用弹性框项目的高度来设置字体大小。我有一个使用视口单元设置的 flexbox 容器,项目的高度由 flex-grow 属性确定。我正在寻找的是将字体大小设置为这些项目的高度,并在视口更改时保留这些关系。
我有一个基本的想法,但我不完全确定如何仅隔离字母(基线到大写高度)并将其缩放到项目容器。
https://codepen.io/NewbCake/pen/JvwNJq (垂直调整窗口大小以设置字体大小)
我愿意接受有关如何解决此问题或可能遇到的任何陷阱的任何建议。
HTML
<section class="center">
<div class="container">
<div class="item1">H</div>
<div class="item2">H</div>
</div>
</section>
CSS
section {
display:flex;
flex-direction:row;
height:95vh;
width:100%;
border:1px solid red;
}
.container {
display:flex;
flex-direction:column;
height:80vh;
width:80vh;
border:1px solid blue;
}
.container_wide {
display:flex;
flex-direction:column;
height:80vh;
width:80vh;
border:1px solid blue;
}
.center {
justify-content:center;
align-items:center;
}
.item1 {
flex-grow:1;
flex-shrink:0;
flex-basis:auto;
border:1px solid green;
line-height:.75;
}
.item2 {
flex-grow:3;
flex-shrink:0;
flex-basis:auto;
border:1px solid green;
line-height:.75;
}
JS
var resizeTimer;
$(window).on('resize', function(e) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
// Run code here, resizing has "stopped"
$(".item1").css("font-size", $(".item1").css("height"));
$(".item2").css("font-size", $(".item2").css("height"));
}, 250);
});
感谢任何帮助!
【问题讨论】:
标签: jquery html css flexbox font-size