Skip to content

Instantly share code, notes, and snippets.

@sappho192
Created January 23, 2016 15:25
Show Gist options
  • Save sappho192/07004040c0db77a28c57 to your computer and use it in GitHub Desktop.
Save sappho192/07004040c0db77a28c57 to your computer and use it in GitHub Desktop.
How to center image both hor-ver

from http://stackoverflow.com/a/34914335

Basically, you just need a two containers and make sure your elements meet the following criteria.

The outher container :

  • should have display: table;

The inner container :

  • should have display: table-cell;
  • should have vertical-align: middle;
  • should have text-align: center;

The content box :

  • should have display: inline-block;

If you use this technique, just add your image (along with any other content you want to go with it) to the content box.

Demo :

body {
    margin : 0;
}

.outer-container {
    position : absolute;
    display: table;
    width: 100%;
    height: 100%;
    background: #ccc;
}

.inner-container {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

.centered-content {
    display: inline-block;
    background: #fff;
    padding : 12px;
    border : 1px solid #000;
}

img {
   max-width : 120px;
}
<div class="outer-container">
   <div class="inner-container">
     <div class="centered-content">
        <img src="https://i.stack.imgur.com/mRsBv.png" />
     </div>
   </div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment