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
.
相关问题
- Views base64 encoded blob in HTML with PHP
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
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:
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.
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:
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:
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.
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