CSS to make an empty cell's border appear?

Css

Css Problem Overview


What CSS should I use to make a cell's border appear even if the cell is empty?

IE 7 specifically.

Css Solutions


Solution 1 - Css

If I recall, the cell dosn't exist in some IE's unless it's filled with something...

If you can put a   (non-breaking space) to fill the void, that will usually work. Or do you require a pure CSS solution?

Apparently, IE8 shows the cells by default, and you have to hide it with empty-cells:hide But it doesn't work at all in IE7 (which hides by default).

Solution 2 - Css

Another way of making sure there is data in all cells:

   $(document).ready(function() {
      $("td:empty").html(" ");
    });

Solution 3 - Css

If you set the border-collapse property to collapse, IE7 will show empty cells. It also collapses the borders though so this might not be 100% what you want

td {
  border: 1px solid red;
}
table {
  border-collapse: collapse;
}
<html> <head> <title>Border-collapse Test</title> <style type="text/css"> td {
  border: 1px solid red;
}
table {
  border-collapse: collapse;
}

<table>
  <tr>
    <td></td>
    <td>test</td>
    <td>test</td>
  </tr>
  <tr>
    <td>test</td>
    <td></td>
    <td>test</td>
  </tr>
  <tr>
    <td>test</td>
    <td></td>
    <td>test</td>
  </tr>
  <tr>
    <td>test</td>
    <td></td>
    <td />
  </tr>
</table>

Solution 4 - Css

The question asked for a CSS solution, but on the off-chance an HTML solution will do, here is one:

Try adding these two attributes to the table element: frame="box" rules="all" like this:

<table border="1" cellspacing="0" frame="box" rules="all">

Solution 5 - Css

I just found the following. It's standards compliant but it doesn't work in IE. sigh.

empty-cells: show 

Solution 6 - Css

I happened across this question and haven't seen any answers that really addressed the issue.

The problem results because IE7 does not see any internal content for the cell; in programming terms the cell is resulting as a null and like most things, you cannot border a null or perform any action on it. The browser needs an element/object that has a layout, in order to apply a border/layout.

Even empty <div></div> or <span></span> do not contain content, thus there is nothing to render, resulting in that null case again.

However, you can trick the browser into thinking the cell has content, by giving the empty div/span layout properties. The easiest way is to apply the CSS style zoom:1.

<table>
   <tr><td>Foo</td>
       <td><span style="zoom:1;"></span></td></tr>
</table>

This workaround is better than using a &nbsp;, since it doesn't unnecessarily mess up screen readers, and isn't misrepresenting the value of the cell. In newer browser you can use the empty-cell:<show|hide> alternative.


Note: in lieu of Tomalak's comment, it should be understood that hasLayout has nothing to do with null, it was merely a comparison of how the browser interacts and renders hasLayout similarly to how a database or programming language interacts with nulls. It is a strech, but I thought it might be easier to understand for those programmers turned web designers.

Solution 7 - Css

Ideally, you shouldn't have any empty cells in a table. Either you have a table of data, and there's no data in that specific cell (which you should indicate with "-", or "n/a/", or something equally appropriate, or - if you must - &nbsp;, as suggested), or you have a cell that needs to span a column or row, or you're trying to achieve some layout with a table that you should be using CSS for.

Can we have a bit more detail?

Solution 8 - Css

This question's old, but still a top result in Google, so I'll add what I've found:

Simply adding border-collapse: collapse to the table style fixed this problem for me in IE7 (and didn't affect the way they're displayed in FF, Chrome, etc).

Best to avoid the extraneous code of adding an &nbsp; or other spacing element when you can fix with CSS.

Solution 9 - Css

I guess this can't be done with CSS; You need to put a &nbsp; in every empty cell for the border to show in IE...

Solution 10 - Css

empty-cell only fixed Firefox (YES I really did have this issue in Firefox) IE 7 & 8 were still problematic..

This worked for me in both Firefox 3.6.x, IE 7 & 8, Chrome, and Safari:

==============================

table {
*border-collapse: collapse;}

.sampleTD {
empty-cells: show;}

==============================

Had to use the * to make sure the table style was only applied to the IE browser.

Solution 11 - Css

Try this if you can't use non-breakable space:

var tn = document.createTextNode('\ ');
yourContainer.appendChild(ta);

Solution 12 - Css

I create a div style that has the same font color as the background of your cell and write anything (usually a "-" "n/a" or "empty") to give the cell content. It shows up if you highlight the page, but when viewed normally looks how you want.

Solution 13 - Css

I use a mix of html and css to create cross browser table grids:

html

<table cellspacing="1" style="background-color:#000;" border="0">

css

td{background-color:#fff;}

I haven't seen any issues with any browsers so far.

Solution 14 - Css

"IE" isn't a useful term in this context anymore now that IE8 is out.

IE7 always does "empty-cells:show" (or so I'm told ... Vista). IE8 in any of its "Quirks" or "IE7 Standards" modes always does "empty-cells:hide". IE8 in "Standards" mode defaults to "empty-cells:show" and supports the attribute via CSS.

As far as I know, every other browser has correctly supported this for several years (I know it was added in Firefox 2).

Solution 15 - Css

I'm taking this from another website but:

.sampletable {
border-collapse: collapse;}

.sampleTD {
empty-cells: show;}

Use for the CSS for the table and TD element respectively.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllain LalondeView Question on Stackoverflow
Solution 1 - CssGrantView Answer on Stackoverflow
Solution 2 - CssRasmusView Answer on Stackoverflow
Solution 3 - CssrpetrichView Answer on Stackoverflow
Solution 4 - CssrenozuView Answer on Stackoverflow
Solution 5 - CssAllain LalondeView Answer on Stackoverflow
Solution 6 - Cssvol7ronView Answer on Stackoverflow
Solution 7 - CssBobby JackView Answer on Stackoverflow
Solution 8 - CssVoodooView Answer on Stackoverflow
Solution 9 - CssfijterView Answer on Stackoverflow
Solution 10 - CssCrystal JonesView Answer on Stackoverflow
Solution 11 - CssruruskyiView Answer on Stackoverflow
Solution 12 - CssBrett HaynesView Answer on Stackoverflow
Solution 13 - CssJT...View Answer on Stackoverflow
Solution 14 - CssGabeView Answer on Stackoverflow
Solution 15 - CssSofoxView Answer on Stackoverflow