If you don't need text wrap in the scaled text, you can use SVG for it (IE9+, so totally acceptable):
<div class="element" style="width: 30%">
<svg class="scale" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<text x="50" y="50" text-anchor="middle">Test</text>
</svg>
</div>
<div class="element" style="width: 60%">
<svg class="scaled" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<text x="50" y="50" text-anchor="middle">Test</text>
</svg>
</div>
With this CSS:
.element {
position: relative;
display: inline-block;
border: 1px solid red;
}
.scaled {
width: 100%;
}
https://jsfiddle.net/93bLu80k/1/
You may encounter problems with vertical scaling of the SVG in some versions of Safari and Internet Explorer (I don't remember which versions exactly). I have this code in some of my projects with scaled SVGs for Safari:
svg {
// hack to make safari use the correct height of svg
// https://bugs.webkit.org/show_bug.cgi?id=82489
max-height: 100%;
}
And this SASS mixin for Internet Explorer, for which you must know the aspect ratio of your SVG in advance, which is usually not a problem:
@mixin aspect-ratio($ratio) {
.ie & {
padding-bottom: 100% * $ratio - 0.02;
height: 1px;
overflow: visible;
box-sizing: content-box;
}
}
(You can translate this mixin to another preprocessor or simply use a hard coded $ratio
in a normal CSS class.)