jquery - 文本的最大可用宽度

标签 jquery html css flexbox

所以我有一堆 flex div,每个都在 span 中包含一个数字。数字可以是2、3、4,最多7位。 我想将每个 divfont-size 设置得尽可能大,这样里面的数字就不会溢出。我计算了 div 宽度并将其除以跨度长度。但在某些较小的显示尺寸中,文本会变小。有什么想法吗?

$('.paramItem').each(function() {
  $(this).css({
    'font-size': $(this).width() / $(this).html().length / 2
  })
});
.paramItem {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  justify-content: center;
  line-height: 1;
  position: relative;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="paramItem ECG">
  <small>ECG</small>
  <span>85</span>
</div>
<div class="paramItem NIBP">
  <small>ECG</small>
  <span>120/50</span>
</div>

最佳答案

您可以将结果乘以确定的系数。然后看看什么系数最符合您对渲染的期望。

'font-size': $(this).width() / $(this).html().length / 2

现在

'font-size': ($(this).width() / $(this).html().length / 2) * 3 // 3 is the coefficient

$('.paramItem').each(function() {
  $(this).css({
    'font-size': ($(this).width() / $(this).html().length / 2) * 3
  })
});
.paramItem {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  justify-content: center;
  line-height: 1;
  position: relative;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="paramItem ECG">
  <small>ECG</small>
  <span>85</span>
</div>
<div class="paramItem NIBP">
  <small>ECG</small>
  <span>120/50</span>
</div>

关于jquery - 文本的最大可用宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43973285/

相关文章:

html - 我的内容没有在 div 内调整

javascript - html中的多级列表

php - 如何检查任何文本区域, radio 被检查jquery

javascript - JQuery 过滤函数忽略 html 元素并只搜索文本

对必填字段进行 Javascript/Jquery 验证

javascript - 如何自动增长具有可变字体大小的文本框

php - 在 PHP 中循环插入查询

javascript - 如何在 Javascript HTML 中显示隐藏元素

css - iPhone/mobile safari = .css 文件阻止并发服务器请求?

html - 如何更改 CSS 媒体查询中的 <span> 文本