如何自动更改div内的文本大小?
我有一个带有 div
的背景图像。我想写一些文本,但是这个文本应该通过div
改变字体大小。
I have a background image with a div
. I want to write some text, but this text should change the font size through the div
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我这样理解你的问题,你想将一些文本放入具有固定尺寸的给定 div 中,如果 div 太小而无法显示所有文本,则应缩小字体大小,直到文本适合 div 。如果这就是重点,这就是我的解决方案。
下面是一个 jQuery 实现的示例: http://jsfiddle.net/MYSVL/2/
这里 div
是一个具有固定大小的
这个 JavaScript 可以完成这项工作。
I understand your question this way, you would like to fit some text into a given div with a fixed dimension, and if the div is too small to show all the text, the font size should be shrinked until the text fits into the div. If that's the point, here is my solution.
Here is an example with a jQuery implementaion: http://jsfiddle.net/MYSVL/2/
Here is a div
With a fixed size
This JavaScript will do the job.
FlowType.js 将会做到这一点:调整字体大小以匹配边界元素,无论是 div 还是其他元素类型。
实现 FlowType 的示例:
设置您的样式
<前><代码>正文{
字体大小:18px;
行高:26px;
}
从 GitHub 下载 FlowType 并将其引用包含在你的头部元素
Call流类型
(可选)更新默认设置
查看他们的主页以获取交互式演示
FlowType.js will do just that: resize the font to match the bounding element, be it a div or another element type.
An example of implementing FlowType:
Set up your style
Download FlowType from GitHub and include a reference to it in your head element
Call FlowType
(optional) Update default settings
Check out their homepage for an interactive demo
我不完全确定我理解你的问题,但我认为你要问的是如何将文本放入预定义大小的特定容器中。我将尝试回答这个问题。
客户端
当您想在客户端按需执行此操作时,您必须运行 Javascript。这个想法是:
生成一个不可见的
div
并将其样式设置为:将文本复制到其中
检查DIV的宽度是否超出原始 DIV 的大小。
调整
font-size
值不是简单地增加/减少,而是通过计算不可见和原始DIV之间的宽度差异。这将更快地偏离正确的大小。转到步骤3。
服务器端
没有可靠的方法在服务器上设置字体大小,以使其适合客户端渲染的容器。不幸的是,您只能根据预先测试的经验数据来估算大小。
I'm not completely sure I understand your question but I think what you're asking is how to fit text in a particular container of predefined size. I'll try to answer this question.
Client side
When you want to do this on demand on the client you will have to run Javascript. The idea is to:
generate an invisible
div
and set its style to:copy your text to it
check whether DIV's width exceeded the size of your original DIV.
adjust
font-size
value by not simply incrementing/decrementing but rather by calculating width difference between your invisible and original DIV. This will deviate to correct size much faster.go to step 3.
Server side
There is no reliable way of setting font size on the server so it will fit your client rendered container. Unfortunately you can only approximate sizes on pretested empirical data.
这个问题已经回答很久了,但我想添加一些注释。
从那时起,一个额外的 jQuery 插件被添加到宇宙中: FlowType.js
https://github.com/simplefocus /FlowType.JS
我尝试了给定的解决方案和插件(包括 FlowType.JS),最后我编写了自己的解决方案和插件。我只是想将其作为“灵感”包含在这里。我使用不同的方法:我计算文本大小大于其父级的比率。我不知道这是否有意义,但对我来说效果很好。
它假设块级元素中有一个内联元素,它通过重新计算字体大小来缩小内联元素。
HTML:
JavaScript:
The question has been answered since long, but I'd like to add some notes.
Since, an extra jQuery plugin has been added to the universe: FlowType.js
https://github.com/simplefocus/FlowType.JS
I tried my hand at the given solutions and plugins (including FlowType.JS), in the end I wrote my own. I just like to include it here as 'inspiration'. I use a different approach: I calculate the ratio that the size of the text is larger then it's parent. I don't know if it makes sense, but for me it works nicely.
It assumes a inline-element within a block-level element, it shrinks the inline element by recalculating the font-size.
HTML:
JavaScript:
您必须预加载图像才能获取图像的宽度和高度。
一旦获得尺寸,您就可以“尝试”不同的字体大小:
You have to preload the image to get the width and height of the image.
As soon as you got the dimensions you can "try" different fontsizes:
这是一个带有 JQuery 和 HTML5 数据注释的全局函数,用于定义块大小,请看一下:
不要问我为什么需要表格:-),宽度函数在 Jquery 1.7 上最适合表格... Divs 无宽度
用法:
添加功能:
Here is a global function with JQuery and HTML5 Data annotations to define the block size take a look:
Don't ask me why I need the table :-) , the width function works best with tables... on Jquery 1.7 .. Getting no width for Divs
Usage :
Function to add:
当你在 div 中有一个复杂的 HTML 并且你想保持不同元素之间的比例时,我扩展了 DanielB 解决方案:
I extend DanielB solution in the case when you have a complex HTML inside the div and you want to maintains ratio between different elements:
我已经制作了更好版本的 @DanielB 代码,也许它可以节省某人的时间:)
(只需将 RESIZABLE 类添加到您想要更改的 div 中)
I have made better version of @DanielB code, maybe it save someone time :)
(Just add class RESIZABLE to div you want to be changed)
在这里查看这个示例 http://codepen.io/LukeXF/pen/yOQWNb,您可以在页面加载和页面调整大小上使用一个简单的函数来实现奇迹。
Check out this example here http://codepen.io/LukeXF/pen/yOQWNb, you can use a simple function on page load and page resize to make the magic happen.