Created
March 23, 2013 23:44
-
-
Save LeaVerou/5229803 to your computer and use it in GitHub Desktop.
Vertically centered text with SVG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Vertically centered text with SVG */ | |
div { | |
width: 300px; | |
height: 150px; | |
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 */ | |
color: white; | |
} | |
svg { | |
width: 100%; height: 100%; | |
pointer-events: none; /* so that you can resize the element */ | |
} | |
text { | |
text-anchor: middle; | |
pointer-events: auto; /* Cancel the svg’s pointer-events */ | |
fill: currentColor; | |
} | |
p { | |
font: italic 100% Georgia, serif; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// alert('Hello world!'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"css"} |
Make something that is not a table cell behaves like a table cell just for the sake of vertical alignment is anything but obvious.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it better than obvious
?