Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created May 4, 2011 01:28
Show Gist options
  • Save marcuswestin/954593 to your computer and use it in GitHub Desktop.
Save marcuswestin/954593 to your computer and use it in GitHub Desktop.
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