简体   繁体   English

是否可以调整文本大小以适合固定大小的div?

[英]Is it possible to resize text to fit a fixed size div?

This seems like a pretty natural use case to me, though I haven't been able to find anything on it: 对我来说,这似乎是一个很自然的用例,尽管我无法在上面找到任何东西:

Say I have a fixed-width div that is dynamically populated with some number. 假设我有一个固定宽度的div,它会动态填充一些数字。 What's the best way to ensure that numbers with more digits take smaller font sizes such that they fit nicely into that fixed width? 确保具有更多数字的数字采用较小的字体大小以使其恰好适合该固定宽度的最佳方法是什么? Is there some CSS property for this, or do I have to resort to Javascript hackage? 是否有一些CSS属性,或者我必须诉诸Javascript hackage?

CSS不会执行此操作,但是您可以尝试使用以下脚本: http : //www.zachleat.com/web/fittext-and-bigtext/

There is no CSS property which automatically adjusts font-sizes based on a fixed container. 没有CSS属性可根据固定容器自动调整字体大小。 You will have to resort to javascript. 您将不得不使用javascript。

You could put each number in a span, and loop over each span checking its width. 您可以将每个数字放在一个跨度中,然后遍历每个跨度以检查其宽度。 If the width is greater than the fixed width, bump the font-size down and then check the width again. 如果宽度大于固定宽度,请减小字体大小,然后再次检查宽度。 Keep on lowering the font-size until the span's width is less than the fixed width. 继续降低字体大小,直到跨度的宽度小于固定宽度。

To prevent flickering, you should perform this loop checking while the fixed div is visible, but placed off page (such as "position: absolute; left: -5000px;") 为防止闪烁,应在固定div可见但不在页面上的情况下执行此循环检查(例如“ position:absolute; left:-5000px;”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM