Nothing Insightful

  • Archive
  • RSS

Vertically Centering DIVs

The ability to vertically center a DIV in HTML is one of the holy grails of web development, along with sticky footers and just generally making shit work in IE. I had to vertically centre something recently, and I was sick of using the dirty hacks involving absolute positioning, negative margins and extra HTML elements.

The solution below works in all modern browsers, and is supported in IE8 and above.

<div id=”centered”>
   I’m centered vertically and horizontally
</div>

#centered {
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
   margin: auto;
   width: 200px;
   height: 200px; 
} 

It is listed as Method 4 on this page, but frankly should be Method 1.

The reason I love this solution so much is that we have all been using margin: 0 auto; to horizontally center things for yonks. This answers the age old question of, well, why doesn’t it work vertically either? Another big plus is no extra HTML elements. Generally you’re forced to create one to horizontally center an item. Another to offset it 50% from the top and, depending on your cross browser confidence, another to negatively offset this with a margin. ■

    • #html
    • #css
    • #web dev
  • 1 year ago
  • 24
  • Comments
  • Permalink
  • Share

24 Notes/ Hide

  1. xn----slbefavdc9aecr2ax8cfbiip9g reblogged this from taitems
  2. trampoline1k reblogged this from taitems
  3. baddeo likes this
  4. reach1to1 likes this
  5. jurosik likes this
  6. jotarun likes this
  7. frederickpolk likes this
  8. digdog likes this
  9. kylewritescode likes this
  10. luminae likes this
  11. pixelblotch likes this
  12. rainerbird reblogged this from taitems and added:
    #yes
  13. taitems posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

My name is Tait Brown, and I'm a Melbourne-based UI designer and a front end developer. I like to make stuff.

taitbrown.com

taitbrown@gmail.com

Me, Elsewhere

  • taitems on Dribbble
  • taitems on Forrst
  • @taitems on Twitter
  • taita_cakes on Last.fm
  • Linkedin Profile
  • taitems on github

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr