Created
May 4, 2011 01:28
-
-
Save marcuswestin/954593 to your computer and use it in GitHub Desktop.
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
var textEl = document.createElement('div'), | |
container = document.createElement('div'), | |
lineHeight = 25 // pixels of a single line | |
container.appendChild(textEl) | |
function showText(text) { | |
// hide the text while we test its layout. visibility='hidden' still keeps the element in the DOM, in contrast to display='none' | |
textEl.style.visibility = 'hidden' | |
textEl.innerHTML = text | |
textEl.style.textAlign = (textEl.offsetHeight > lineHeight | |
? 'left' // overflowing onto two lines | |
: 'center') // fits on a single line | |
textEl.style.visibility = 'visible' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment