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%).

- 14,714
- 27
- 76
- 97
-
1http://stackoverflow.com/questions/16056591/font-scaling-based-on-width-of-container – Ming Slogar Nov 10 '13 at 23:20
10 Answers
If you want to set the font-size as a percentage of the viewport width, use the vw
unit:
#mydiv { font-size: 5vw; }
The other alternative is to use SVG embedded in the HTML. It will just be a few lines. The font-size attribute to the text element will be interpreted as "user units", for instance those the viewport is defined in terms of. So if you define viewport as 0 0 100 100, then a font-size of 1 will be one one-hundredth of the size of the svg element.
And no, there is no way to do this in CSS using calculations. The problem is that percentages used for font-size, including percentages inside a calculation, are interpreted in terms of the inherited font size, not the size of the container. CSS could use a unit called bw
(box-width) for this purpose, so you could say div { font-size: 5bw; }
, but I've never heard this proposed.
-
1
-
1@lorefnon, this page http://caniuse.com/#feat=viewport-units says it will work in Firefox 19, meanwhile try Chrome. – Dec 16 '12 at 03:36
-
@torazburo, Hmm, tried it in FF 17.0. Great find btw, had no idea something like this existed. – lorefnon Dec 16 '12 at 06:09
-
I've hacked together a piece of less code that will use vw where available and fallback predefined values for various media queries: https://gist.github.com/tnajdek/5143504 – tnajdek Mar 12 '13 at 14:53
-
1I came here because `vw` is the opposite of what I need, and found this answer...which has nothing to do with the question.. :( – vsync Feb 18 '14 at 18:13
-
for anyone interested in the cross browser support of this: http://caniuse.com/viewport-units – jampez77 Apr 28 '14 at 09:40
-
16Note that `vw` is relative to the viewport, not to the container size. Probably useful in many cases, but svg worked better for me. – zelanix Oct 25 '14 at 14:17
-
If the parent container's height or width is a percentage like 40% or 70%, what would we put down for vh or vw? That is what the question of this post asked about originally. – Jonathan J. Pecany Apr 27 '22 at 22:01
Another js alternative:
fontsize = function () {
var fontSize = $("#container").width() * 0.10; // 10% of container width
$("#container h1").css('font-size', fontSize);
};
$(window).resize(fontsize);
$(document).ready(fontsize);
Or as stated in torazaburo's answer you could use svg. I put together a simple example as a proof of concept:
<div id="container">
<svg width="100%" height="100%" viewBox="0 0 13 15">
<text x="0" y="13">X</text>
</svg>
</div>
-
Just a comment, take a look how 'firefox' opens and display a .pdf file. It's javascriptly resizing the content. Quite heavy, but fully working. – Milche Patern Jul 07 '14 at 18:04
-
This totally worked for me , now I just need to center the text vertically – alejandro Dec 21 '17 at 07:47
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.

- 2,298
- 18
- 26
-
Try using: ```window.onresize = function(event) { yourFunctionHere(); };``` as well, this way if the user decides to resize their page after it has loaded your font will resize appropriately – Henry Howeson Nov 01 '17 at 23:08
I used Fittext on some of my projects and it looks like a good solution to a problem like this.
FitText makes font-sizes flexible. Use this plugin on your fluid or responsive layout to achieve scalable headlines that fill the width of a parent element.
Here is the function:
document.body.setScaledFont = function(f) {
var s = this.offsetWidth, fs = s * f;
this.style.fontSize = fs + '%';
return this
};
Then convert all your documents child element font sizes to em's or %.
Then add something like this to your code to set the base font size.
document.body.setScaledFont(0.35);
window.onresize = function() {
document.body.setScaledFont(0.35);
}

- 5,400
- 2
- 38
- 52
I had a similar issue but I had to consider other issues that @apaul34208 example did not tackle. In my case;
- I have a container that changed size depending on the viewport using media queries
- Text inside is dynamically generated
- I want to scale up as well as down
Not the most elegant of examples but it does the trick for me. Consider using throttling the window resize (https://lodash.com/)
var TextFit = function(){
var container = $('.container');
container.each(function(){
var container_width = $(this).width(),
width_offset = parseInt($(this).data('width-offset')),
font_container = $(this).find('.font-container');
if ( width_offset > 0 ) {
container_width -= width_offset;
}
font_container.each(function(){
var font_container_width = $(this).width(),
font_size = parseFloat( $(this).css('font-size') );
var diff = Math.max(container_width, font_container_width) - Math.min(container_width, font_container_width);
var diff_percentage = Math.round( ( diff / Math.max(container_width, font_container_width) ) * 100 );
if (diff_percentage !== 0){
if ( container_width > font_container_width ) {
new_font_size = font_size + Math.round( ( font_size / 100 ) * diff_percentage );
} else if ( container_width < font_container_width ) {
new_font_size = font_size - Math.round( ( font_size / 100 ) * diff_percentage );
}
}
$(this).css('font-size', new_font_size + 'px');
});
});
}
$(function(){
TextFit();
$(window).resize(function(){
TextFit();
});
});
.container {
width:341px;
height:341px;
background-color:#000;
padding:20px;
}
.font-container {
font-size:131px;
text-align:center;
color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container" data-width-offset="10">
<span class="font-container">£5000</span>
</div>

- 983
- 12
- 10
I've given a more detailed answer of using vw
with respect to specific container sizing in this answer, so I won't just repeat my answer here.
In summary, however, it is essentially a matter of factoring (or controlling) what the container size is going to be with respect to viewport, and then working out the proper vw
sizing based on that for the container, taking mind of what needs to happen if something is dynamically resized.
So if you wanted a 5vw
size at a container at 100% of the viewport width, then one at 75%
of the viewport width you would probably want to be (5vw * .75) = 3.75vw
.
If you want to scale it depending on the element width, you can use this web component:
https://github.com/pomber/full-width-text
Check the demo here:
https://pomber.github.io/full-width-text/
The usage is like this:
<full-width-text>Lorem Ipsum</full-width-text>

- 23,132
- 10
- 81
- 94
You can also try this pure CSS method:
font-size: calc(100% - 0.3em);
-
1that's not going to work at all. all this does is set the font size relatively to the containers font size. (minus 0.3 em) – Martijn Scheffer Apr 19 '17 at 23:50