根据文本的长度固定DIV如何自动调整文本? [英] How to auto resize text in fixed DIV according to the text's length?

查看:140
本文介绍了根据文本的长度固定DIV如何自动调整文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定的宽度和高度DIV,我需要把文本中。
问题是,这段文字可以在不同的长度(字母明智的),所以我不介意以减小其大小一旦其四溢。

I have a fixed width and height DIV, and I need to put text inside. Problem is, this text can be in different lengths (letter-wise), so I dont mind to reduce its size once its overflowing.

但我怎么能这样做呢?

感谢

推荐答案

您可以使用 window.getComputedStyle 如果您定位的现代浏览器。

它返回应用于元素的所有真正的样式属性的集合。

You can use window.getComputedStyle if you target modern browsers.
It returns a collection of all real style properties applied to an element.

当您指定你的文字,你可以得到它的大小和它与div的大小进行比较。而减少或增加字体大小和重新测量。
在几个循环,你应该得到的DIV中的文本。

When you assign your text, you can get its size and compare it with the size of the div. And reduce or increase the font size and measure again.
In a few loops you should get the text in the DIV.

下面是一个说明: https://developer.mozilla.org/en/DOM :window.getComputedStyle

这篇关于根据文本的长度固定DIV如何自动调整文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆