Center text in SVG rect -- horz and vert
-
textelement in a sibling torect. - left side of
textelement positioned at horz center of rectangle and vertical center -
text-anchorfor centering text horizontally -
alignment-baselinefor centering vertically
<svg height="50" width="200">
<rect fill="green" x="0" y="0" width="200" height="50"></rect>
<text x="100" y="25" fill="red" text-anchor="middle" alignment-baseline="central">Christmas Time!</text>
</svg>
Written by Jake Trent
Related protips
1 Response
This works on Chrome, fails on Firefox and IE.
Using dominant-baseline instead of alignment-baseline makes it also work on Firefox, still fails on IE.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Svg
Authors
Related Tags
#svg
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#