44

I'm trying to use the table-cell way to center a div vertically and horizontally.

It works when I use the following code:

div {
  display: table;
}
.logo {
  display: table-cell;
  position: absolute;
  vertical-align: middle;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
}

But I'd rather wrap .logo in another div called .center like here JSFiddle, but for some reason, although it works in JSFiddle, it isn't working for me on my site.

kissu
  • 6,476
  • 5
  • 15
  • 33
Steven_Harris_
  • 911
  • 1
  • 8
  • 19
  • What browser are you testing on? – Gurminder Singh Oct 04 '13 at 14:41
  • Are you constrained to using the table-cell method? Is there a reason why? – robabby Oct 04 '13 at 14:41
  • No reason, just wanted to figure out why it isn't working, testing on Google Chrome. – Steven_Harris_ Oct 04 '13 at 14:42
  • You are trying to center `.logo` vertically & horizontally in the parent `div`. What controls the width and height of `div`, are these specified? – Marc Audet Oct 04 '13 at 14:49
  • 3
    The parent element isn't relatively positioned, so .logo is being positioned relative to the next closest ancestor (if no ancestors are relatively positioned, then it is positioned relative to the document). Don't mix vertical centering techniques, pick one or the other. – cimmanon Oct 04 '13 at 15:01
  • Possible duplicate of [How to align text vertically center in div with CSS?](http://stackoverflow.com/questions/8865458/how-to-align-text-vertically-center-in-div-with-css) – Cole Johnson Apr 26 '16 at 17:53

2 Answers2

53

Here is a good starting point.

HTML:

<div class="containing-table">
    <div class="centre-align">
        <div class="content"></div>
    </div>
</div>

CSS:

.containing-table {
    display: table;
    width: 100%;
    height: 400px; /* for demo only */
    border: 1px dotted blue;
}
.centre-align {
    padding: 10px;
    border: 1px dashed gray;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.content {
    width: 50px;
    height: 50px;
    background-color: red;
    display: inline-block;
    vertical-align: top; /* Removes the extra white space below the baseline */
}

See demo at: http://jsfiddle.net/audetwebdesign/jSVyY/

.containing-table establishes the width and height context for .centre-align (the table-cell).

You can apply text-align and vertical-align to alter .centre-align as needed.

Note that .content needs to use display: inline-block if it is to be centered horizontally using the text-align property.

Chris Halcrow
  • 21,541
  • 11
  • 115
  • 145
Marc Audet
  • 42,628
  • 10
  • 57
  • 78
  • I would use list items instead of divs. Set the
  • to display: table, and then child elements will have display: table-cell. Then you can center text and do whatever you would do to a table cell.
  • – Donato Aug 18 '16 at 20:29
  • Awesome. I was having this issue in Samsung (Android) browser. Chrome(PC) was applying `vertical-align:bottom` on table-cell but Samsung browser wasn't.. your solution did the trick. – sohaiby Sep 25 '16 at 07:44