DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Vertical Centering In CSS

07.04.2005
| 6535 views |
  • submit to reddit
        
#center{
   width:100px;
   height:100px;
   position:absolute;
   top:50%;
   left:50%;
   margin-top:-50px;
   margin-left:-50px;
}

usage:

<img id="center" src="path/image.gif":    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

Is this cross-browser compatible? I've noticed some wierdness using this technique in IE on Windows.

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

It should be noted that this centers a single element on a page, not something within a box