Search code examples
javascriptcssfont-size

how to set font size based on container size?


I have a container that has a % width and height, so it scales depending on external factors. I would like the font inside the container to be a constant size relative to the size of containers. Is there any good way to do this using CSS? The font-size: x% would only scale the font according to the original font size (which would be 100%).


Solution

  • You may be able to do this with CSS3 using calculations, however it would most likely be safer to use JavaScript.

    Here is an example: http://jsfiddle.net/8TrTU/

    Using JS you can change the height of the text, then simply bind this same calculation to a resize event, during resize so it scales while the user is making adjustments, or however you are allowing resizing of your elements.