Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center align with table-cell

Tags:

html

css

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.

like image 566
Steven_Harris_ Avatar asked Oct 04 '13 14:10

Steven_Harris_


People also ask

How do you center align text in a table cell?

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.

How do I center a cell in a HTML table?

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.

How do you align a cell in a table?

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.

How do I center a div in a table cell?

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.


2 Answers

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.

like image 101
Marc Audet Avatar answered Oct 01 '22 15:10

Marc Audet


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

like image 40
Louie Almeda Avatar answered Oct 01 '22 15:10

Louie Almeda