Skip to content

Instantly share code, notes, and snippets.

@chayn1k
Created January 14, 2014 13:23
Show Gist options
  • Save chayn1k/8418238 to your computer and use it in GitHub Desktop.
Save chayn1k/8418238 to your computer and use it in GitHub Desktop.
SVG alternative to fittext.js
/* SVG alternative to fittext.js */
div {
width: 300px;
height: 150px;
float: left;
margin-right : 10px;
background: #f06;
font: bold 150% sans-serif;
text-shadow: 0 1px 2px rgba(0,0,0,.5);
overflow: hidden; resize: both; /* just for this demo */
margin-bottom : 26px;
}
div:nth-child(3){ background: #004eff; }
svg {
width: 100%; height: 100%;
//pointer-events: none; /* so that you can resize the element */
}
text {
fill: white;
text-anchor: middle;
pointer-events: auto; /* Cancel the svg’s pointer-events */
}
p {
font: italic 100% Georgia, serif;
}
.sources{
font-family: Palatino Linotype, Book Antiqua, Palatino, serif;
position:relative;
display:block;
clear : both;
width : 100%;
color : #2051c0;
line-height : 1.5em;
text-decoration : none;
text-shadow: 0px 4px 2px rgba(0, 0, 0, 0.16);
}
span.sources{
color : #444;
padding-top : 2px;
padding-bottom : 8px;
border-top : 1px #ddd solid;
}
span.sources:before{ content: ": "; }
span.sources:after{ content: " "; }
.sources:before{
content:"{ ";
font-size : 1.2em;
line-height : 1.5em;
color : #ccc;
text-shadow: none;
}
.sources:after{
content:" }";
font-size : 1.2em;
line-height : 1.5em;
color : #ccc;
text-shadow: none;
}
<!-- SVG alternative to fittext.js -->
<p>
Resize the div below from the handle at the bottom right corner (if your browser supports the <code>resize</code> CSS property)
or change the CSS code to observe what happens at different dimensions.
</p>
<div>
<svg><text x="50%" y="50%" dy=".3em">Look, I’m centered!</text></svg>
</div><div>
<svg viewBox="-50 -50 100 100" preserveAspectRatio="xMidYMid meet"><text font-size="16" dy=".3em" >I’m also resizeable!</text></svg>
</div>
<span class="sources">
Original Gist: <a href="http://lea.verou.me/2013/03/easily-center-text-vertically-with-svg/">lea verou's "Easily center text vertically, with SVG!"</a><br/>
I've adapted the code to create a pure-SVG alternative to <a href="http://fittextjs.com/">fittext.js</a><br/>
I assume that the resizing itself will always be proportional, otherwise you'll have to change the preserveAspectRatio attribute to fit your needs.<br/>
</span>
<span class="sources">Sources</span>
<a href="http://www.justinmccandless.com/blog/Making+Sense+of+SVG+viewBox's+Madness" class="sources">Making Sense of SVG viewBox's Madness</a>
<a href="http://www.sitepoint.com/svg-styling-with-css/" class="sources">How to Style Scalable Vector Graphics Using CSS</a>
<a href="http://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute" class="sources">The ‘preserveAspectRatio’ attribute</a>
// alert('Hello world!');
{"view":"split-vertical","fontsize":"110","seethrough":"","prefixfree":"1","page":"all"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment