65

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.

6
  • What browser are you testing on? Commented Oct 4, 2013 at 14:41
  • Are you constrained to using the table-cell method? Is there a reason why? Commented Oct 4, 2013 at 14:41
  • No reason, just wanted to figure out why it isn't working, testing on Google Chrome. Commented Oct 4, 2013 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? Commented Oct 4, 2013 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. Commented Oct 4, 2013 at 15:01

2 Answers 2

77

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I would use list items instead of divs. Set the <li> 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.
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.
30

This would be easier to do with flexbox. Using flexbox will let you not to specify the height of your content and can adjust automatically on the height it contains.

DEMO

here's the gist of the demo

.container { display: flex; height: 100%; justify-content: center; align-items: center; } 

html

<div class="container"> <div class='content'> //you can size this anyway you want put anything you want here, </div> </div> 

enter image description here

2 Comments

You "forgot" to demonstrate problems when content dimensions exceed parent dimensions.
The OP is looking for an answer with display: 'table-cell'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.