字体大小自动调整以适合
我正在尝试做标题所说的事情。我已经看到 font-size
可以是百分比。所以我的猜测是 font-size: 100%;
可以做到这一点,但不行。
这是一个示例: http://jsfiddle.net/xVB3t/
我可以获得一些帮助吗?
(如果需要用js编程的话没有问题)
I'm trying to do what the title says. I've seen that font-size
can be a percentage. So my guess was that font-size: 100%;
would do it, but no.
Here is an example: http://jsfiddle.net/xVB3t/
Can I get some help please?
(If is necesary to do it programatically with js there is no problem)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个问题可能会帮助你,但我警告你,尽管这可以通过 jQuery 解决它:
自动调整动态文本大小以填充固定大小的容器
祝你好运。
该问题的OP制作了一个插件,这里是它的链接(& 下载)
顺便说一句,我建议使用 jQuery,因为 Gaby指出这不能仅通过CSS完成,你说你愿意使用js...
This question might help you out but I warn you though this solves it through jQuery:
Auto-size dynamic text to fill fixed size container
Good luck.
The OP of that question made a plugin, here is the link to it (& download)
BTW I'm suggesting jQuery because as Gaby pointed out this can't be done though CSS only and you said you were willing to use js...
不能用 CSS 来完成。
100% 与父元素的计算
font-size
相关。参考:http://www.w3.org/TR/ CSS2/fonts.html#font-size-props
对于 jQuery 解决方案,请查看 自动调整动态文本大小以填充固定大小容器
Can't be done with CSS.
100% is in relation to the computed
font-size
of the parent element.reference: http://www.w3.org/TR/CSS2/fonts.html#font-size-props
For a jQuery solution look at Auto-size dynamic text to fill fixed size container
我正在研究这个工作,我喜欢 tnt-rox 的答案,但我忍不住注意到它有一些可以削减的额外开销。
如果将其添加到 onresize 事件,则减少开销会使它运行得更快一些。
如果您只想将特定元素内的字体设置为调整大小以适合,您也可以执行类似以下操作
我刚刚注意到尼古拉斯的回答也有一些额外的开销。我已经清理了一点。从性能角度来看,我不太喜欢使用 while 循环并慢慢缩小尺寸,直到找到合适的。
这是我之前使用 jquery 的代码的示例。
I was looking into this for work and I liked tnt-rox's answer, but I couldn't help but notice that it had some extra overhead that could be cut out.
Cutting out the overhead makes it run a little bit quicker if you add it to an onresize event.
If you are only looking to have the font inside a specific element set to resize to fit, you could also do something like the following
I just noticed nicolaas' answer also had some extra overhead. I've cleaned it up a bit. From a performance perspective, I'm not really a fan of using a while loop and slowly moving down the size until you find one that fits.
And here is an example of my earlier code using jquery.
有点晚了,但这就是我解决这个问题的方法:
现在设置了基本文档字体。
对于 dom 中的其余元素,将字体大小设置为 % 或 em,它们将按比例缩放。
A bit late but this is how I approach this problem:
The base document font is now set.
For the rest of your elements in the dom set font sizes as % or em and they will scale proportionately.
这里我有一个 mootools 解决方案:
here I have a mootools solution:
这是另一个 jQuery 解决方案...
Here is another jQuery solution ...
这是一种查找您正在使用的文本高度的方法。
它很简单,只使用 JavaScript。您可以使用它来调整文本相对于您想要的高度。
Here's a way to find the height of the text that you are using.
It's simple and only uses javascript. You can use this to adjust your text relative to the height you want.