0

I need to place a div exactly in the middle of the page with an unknown width and height. I can get it centered with the width but is it possible to get it centered with the height?

HTML:

<div class='lightbox'>
    <div class='tc-content'>AAA</div>
</div>

CSS

.lightbox { 
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background: #000;
    text-align: center;
}

.tc-content {
    display: inline-block;
}
Leggy
  • 682
  • 7
  • 20
  • See also http://stackoverflow.com/questions/5481821/image-center-align-vertically-and-horizontally – Phrogz Dec 03 '14 at 03:24

2 Answers2

1

Use this style for tc-content

.tc-content {
    position: relative;
    top: calc(50% - 1em);
}

Here is a working Fiddle

VSri58
  • 3,691
  • 2
  • 14
  • 16
0

A lot of times I use this to center divs horizontally and vertically

.lightbox {
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    /* Other code here */   
}
michaelpri
  • 3,521
  • 4
  • 30
  • 46