Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center horizontal table-cell

Tags:

html

css

I want the Content A, Content B, and Content C columns to be horizontally centered. I have this code been trying to add

http://jsfiddle.net/hsX5q/24/

HTML:margin: 0 auto to .columns-container and it doesn't work. Could anyone help?

/*************************
 * Sticky footer hack
 * Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
 ************************/

/* Stretching all container's parents to full height */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
/* Setting the container to be a table with maximum width and height */

#container {
  display: table;
  height: 100%;
  width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */

.section {
  display: table-row;
  height: 1px;
}
/* The last-but-one section should be stretched to automatic height */

.section.expand {
  height: auto;
}
/*************************
 * Full height columns
 ************************/

/* We need one extra container, setting it to full width */

.columns-container {
  display: table-cell;
  height: 100%;
  width: 300px;
  margin: 0 auto;
}
/* Creating columns */

.column {
  /* The float:left won't work for Chrome for some reason, so inline-block */
  display: inline-block;
  /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
  vertical-align: top;
  height: 100%;
  width: 100px;
}
/****************************************************************
 * Just some coloring so that we're able to see height of columns
 ****************************************************************/

header {
  background-color: yellow;
}
#a {
  background-color: pink;
}
#b {
  background-color: lightgreen;
}
#c {
  background-color: lightblue;
}
footer {
  background-color: purple;
}
<div id="container">
  <header class="section">
    foo
  </header>

  <div class="section expand">
    <div class="columns-container">
      <div class="column" id="a">
        <p>Contents A</p>
      </div>
      <div class="column" id="b">
        <p>Contents B</p>
      </div>
      <div class="column" id="c">
        <p>Contents C</p>
      </div>
    </div>
  </div>

  <footer class="section">
    bar
  </footer>
</div>
like image 456
Daniel Avatar asked Apr 25 '13 23:04

Daniel


People also ask

How do I center align a cell in a table?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.

How do you center elements horizontally?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


3 Answers

If you add text-align: center to the declarations for .columns-container then they align centrally:

.columns-container {
    display: table-cell;
    height: 100%;
    width:600px;
    text-align: center;
}

/*************************
 * Sticky footer hack
 * Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
 ************************/

/* Stretching all container's parents to full height */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
/* Setting the container to be a table with maximum width and height */

#container {
  display: table;
  height: 100%;
  width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */

.section {
  display: table-row;
  height: 1px;
}
/* The last-but-one section should be stretched to automatic height */

.section.expand {
  height: auto;
}
/*************************
 * Full height columns
 ************************/

/* We need one extra container, setting it to full width */

.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/* Creating columns */

.column {
  /* The float:left won't work for Chrome for some reason, so inline-block */
  display: inline-block;
  /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
  vertical-align: top;
  height: 100%;
  width: 100px;
}
/****************************************************************
 * Just some coloring so that we're able to see height of columns
 ****************************************************************/

header {
  background-color: yellow;
}
#a {
  background-color: pink;
}
#b {
  background-color: lightgreen;
}
#c {
  background-color: lightblue;
}
footer {
  background-color: purple;
}
<div id="container">
  <header class="section">
    foo
  </header>

  <div class="section expand">
    <div class="columns-container">
      <div class="column" id="a">
        <p>Contents A</p>
      </div>
      <div class="column" id="b">
        <p>Contents B</p>
      </div>
      <div class="column" id="c">
        <p>Contents C</p>
      </div>
    </div>
  </div>

  <footer class="section">
    bar
  </footer>
</div>

This does, though, require that you reset the .column elements to text-align: left (assuming you want them left-aligned, obviously (JS Fiddle demo).

like image 79
David Thomas Avatar answered Oct 16 '22 23:10

David Thomas


Sometimes you have things other than text inside a table cell that you'd like to be horizontally centered. In order to do this, first set up some css...

<style>
    div.centered {
        margin: auto;
        width: 100%;
        display: flex;
        justify-content: center;
    }
</style>

Then declare a div with class="centered" inside each table cell you want centered.

<td>
    <div class="centered">
        Anything: text, controls, etc... will be horizontally centered.
    </div>
</td>
like image 41
David Avatar answered Oct 16 '22 23:10

David


Short snippet for future visitors - how to center horizontal table-cell (+ vertically)

html, body {
  width: 100%;
  height: 100%;
}

.tab {
  display: table;
  width: 100%;
  height: 100%;
}

.cell {
  display: table-cell;
  vertical-align: middle;
  text-align: center; /* the key */
  background-color: #EEEEEE;
}

.content {
  display: inline-block; /* important !! */
  width: 100px;
  background-color: #00FF00;
}
<div class="tab">
  <div class="cell">
    <div class="content" id="a">
      <p>Content</p>
    </div>
  </div>
</div>
like image 2
B-GangsteR Avatar answered Oct 17 '22 00:10

B-GangsteR