jquery - 根据 flexbox 容器的高度设置字体大小

标签 jquery html css flexbox font-size

我想知道是否可以使用 flexbox 元素的高度来设置字体大小。我有一个使用视口(viewport)单位设置的 flexbox 容器,元素的高度由 flex-grow 属性决定。我正在寻找的是将字体大小设置为这些元素的高度,并在视口(viewport)更改时保留这些关系。

我有一个基本的想法,有点管用,但我不确定如何只隔离字母(基线到上限高度)并将其缩放到元素容器。

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);
});

感谢任何帮助!

最佳答案

如果您可以控制 flex-grow,您可以根据容器的高度进行一些计算以获得 font-size。所以如果你有一个 1 + 2 作为 flex-grow,这意味着第二个将是第一个的两倍所以我们可以定义一个 H 高度作为 H+2*H = 容器高度 = 80vh 所以 H = calc(80vh/3)

因此第一项将具有 font-size:H,第二项将具有 font-size:2*H

您也可以考虑使用 CSS 变量来更好地处理这个问题。

body {
   margin:0;
  padding:0;
  font-family: sans-serif;
}

header {
  display:flex;
  height:5vh;
}
section {
  display:flex;
  flex-direction:row;
  height:95vh;
  width:100%;
  border:1px solid red;
}

.container {
  display:flex;
  flex-direction:column;
  --h:80vh;
  height:var(--h);
  width:var(--h);
  border:1px solid blue;
}

.gauge {
  display:flex;
  flex-direction:column;
  height:80vh;
  width:10vh;
  border:1px solid blue;
}

.center {
  justify-content:center;
  align-items:center;
}

.item1 {
  flex-grow:1;
  font-size:calc((var(--h) / 3));
  flex-shrink:0;
  flex-basis:auto;
  border:1px solid green;
  line-height:1;
}

.item2 {
  font-size:calc((var(--h) / 3) * 2);
  flex-grow:2;
  flex-shrink:0;
  flex-basis:auto;
  border:1px solid green;
  line-height:1;
}
<header class="center">resize window vertically</header>
<section class="center">
  <div class="gauge">
    <div class="item1"></div>
    <div class="item2"></div>
  </div>
  <div class="container">
    <div class="item1">Haj</div>
    <div class="item2">Hlp</div>
  </div>
</section>

关于jquery - 根据 flexbox 容器的高度设置字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50419351/

相关文章:

javascript - 根据事件cookie将css类添加到div

html - 从类中提取跨度值,VBA

mouseover() 和 slideDown 需要 jQuery 帮助

css - 仅在第一个和最后一个输入 block 上的边界半径

jQuery 并始终执行一次事件

javascript - 如何在 select2 中使用选项组或按索引选择选项

php - 从外部页面抓取 DIV 内的特定元素

html - CSS - 区分下拉选择和列表框选择

正在应用 Html CSS 重置,但未兑现

javascript - 使用 gmap3 自定义信息窗口和标记