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.
Select the text that you want to center, and then click Paragraph on the Format menu. On the Indents and Spacing tab, change the setting in the Alignment box to Centered, and then click OK.
To center this table, you would need to add ;margin-left:auto;margin-right:auto; to the end of the style attribute in the <table> tag.
Table data defaults to left alignment; table headers to center. In order to change the alignment in one cell, insert the appropriate "ALIGN=" attribute within the code for that cell. In order to change the alignment in all cells in a row, insert the appropriate alignment attribute within the code for that row.
Set height and width to 100% to make the element fill the available space within its parent element. Use display: table-cell on the child element to make it behave like a <td> elements. Use text-align: center and vertical-align: middle on the child element to center it horizontally and vertically.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With