[html] css display table cell requires percentage width

I've been put in a position where I need to use the display:table-cell command for div elements.

However I've discovered that the "cells" will only work correctly if a percentage is added to the width.

In this fiddle http://jsfiddle.net/NvTdw/ when I remove the percentage width the cells do not have equal widths, however when a percentage value is added to the width all is well, but only when that percentage is equal to the proportion of max no of divs, so for four columns 25%, five 20% and in this case five at 16.666%.

I thought maybe adding a percentage of less - say 1% would be a good fix all, but the cells fall out of line again.

Why is this?

_x000D_
_x000D_
    .table {_x000D_
      display: table;_x000D_
      height: 200px;_x000D_
      width: 100%;_x000D_
    }_x000D_
_x000D_
    .cell {_x000D_
      display: table-cell;_x000D_
      height: 100%;_x000D_
      padding: 10px;_x000D_
      width: 16.666%;_x000D_
    }_x000D_
_x000D_
    .grey {_x000D_
      background-color: #666;_x000D_
      height: 100%;_x000D_
      text-align: center;_x000D_
      font-size: 48px;_x000D_
      color: #fff;_x000D_
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;_x000D_
      font-weight: 300;_x000D_
    }
_x000D_
<div class="table">_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block one</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block two</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block three</div>_x000D_
  </div>_x000D_
</div>_x000D_
<div class="table">_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block two</div>_x000D_
  </div>_x000D_
</div>_x000D_
<div class="table">_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block one</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block two</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block three</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block four</div>_x000D_
  </div>_x000D_
</div>_x000D_
<div class="table">_x000D_
  <div class="cell">_x000D_
    <div class="grey">x</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">xx</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">xxx</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">xxxx</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">xxxxxx</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block five test</div>_x000D_
  </div>_x000D_
</div>_x000D_
<div class="table">_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block two</div>_x000D_
  </div>_x000D_
  <div class="cell">_x000D_
    <div class="grey">Block three</div>_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

This question is related to html css css-tables

The answer is


You just need to add 'table-layout: fixed;'

.table {
   display: table;
   height: 100px;
   width: 100%;
   table-layout: fixed;
}

http://www.w3schools.com/cssref/pr_tab_table-layout.asp


Note also that vertical-align:top; is often necessary for correct table cell appearance.

css table-cell, contents have unnecessary top margin


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to css-tables

How (and why) to use display: table-cell (CSS) height: 100% for <div> inside <div> with display: table-cell css display table cell requires percentage width space between divs - display table-cell Why is width: 100% not working on div {display: table-cell}? Controlling Spacing Between Table Cells Setting a max height on a table How to apply CSS page-break to print a table with lots of rows? How is a CSS "display: table-column" supposed to work? What replaces cellpadding, cellspacing, valign, and align in HTML5 tables?