5

I want to make a button fill a table cell to make the whole cell clickable. By default, the button has an auto-width, so I have to make it explicitly 100% width (display: block doesn’t work). But doing that will make the cell no longer recognize its own width. In Firefox, this is a problem when nesting the table in a container with overflow: scroll:

Bug in Firefox

As you can see in the example, the text from the cells flows out of the cells, and the cells are far too small. The width of the visible width is equally distributed across all cells. In the second example, I replaced the button by a div, which does not show the problem: The cells correctly get the width they require. The third example shows a more real use case with a second row making the columns wider (which is the reason the buttons need to have width: 100%).

.wrapper {
  width: 250px;
  overflow-x: scroll;
  background: indigo;
  margin-bottom: 1em;
}
.table {
  white-space: nowrap;
}
.table td {
  background: lavender;
}
.table button {
  font: inherit;
  border: 0;
  background: none;
  padding: 0;
  width: 100%;
}
<div class="wrapper">
  <table class="table">
    <tr>
      <td><button>Some long table header A</button></td>
      <td><button>Some long table header B</button></td>
      <td><button>Some long table header C</button></td>
     </tr>
  </table>
</div>

<div class="wrapper">
  <table class="table">
    <tr>
      <td><div>Some long table header A</div></td>
      <td><div>Some long table header B</div></td>
      <td><div>Some long table header C</div></td>
     </tr>
  </table>
</div>

<div class="wrapper">
  <table class="table">
    <tr>
      <td><button>Table header A</button></td>
      <td><button>Table header B</button></td>
      <td><button>Table header C</button></td>
     </tr>
    <tr>
      <td>Some long table content</td>
      <td>Some long table content</td>
      <td>Some long table content</td>
    </tr>
  </table>
</div>

Do you have any idea how to make the cell take its required width with buttons? My alternative right now is to use divs and add ARIA roles to them, so they behave like buttons (but that feels kind of dirty).

The problem does not appear in Chrome or Internet Explorer, so there might be some Firefox-related style that is added to the button. Maybe it’s possible to disable that with a browser-specific CSS property?

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
  • I'm assuming you're in Firefox, right? Because I don't see an issue in Chrome or IE. – Josh Crozier Jan 22 '16 at 14:10
  • @JoshCrozier Yes, you’re right. As I posted this, I remembered to check it agian in the rest of the browsers. It seems to be Firefox-specific (which only make this issue worse imo xD). I’ve updated the question to reflect that. – poke Jan 22 '16 at 14:12
  • I removed width: 100% from your .table button in your css and it works fine for me! – Chris G Jan 22 '16 at 14:19
  • @ChrisG But then the button will not fill the cell. So if there is another cell in the column that makes the column wider, then the button will still only fill the required space, not the available space. – poke Jan 22 '16 at 14:23
  • I'll create a fiddle and see if I can't make it work :) – Chris G Jan 22 '16 at 14:26
  • Does it have to be a `button` element? Can’t you use a normal link perhaps? (And format it with CSS to look “button-like”, if desired.) With a normal link you should be able to use `display:block` without problem. – CBroe Jan 22 '16 at 14:30
  • @CBroe It doesn’t need to be a button element (I don’t want it to appear like a button at all; it should appear as a “clickable cell”). So yeah, using another element is the alternative but will require a proper button ARIA role. – poke Jan 22 '16 at 14:33

1 Answers1

9

This appears to be a bug in Firefox. A bug report describing your issue has been filed here https://bugzilla.mozilla.org/show_bug.cgi?id=332171.

Looks like it could be fixed soon though given the following event in that bug report:

I think my patches to bug 823483 will fix this (since they expand the quirk in one way (max-width too) and restrict it in another (fewer frame types); the latter is what matters here); just need to finish them (and figure out why the tests I wrote weren't all passing)...

Response by David Baron at 2016-01-06 21:12:31 PST

For the time being to get the desired behaviour the simplest change seems to be:

  • Change width: 100%; to min-width: 100%; on .table button

.wrapper {
  width: 250px;
  overflow-x: scroll;
  background: indigo;
  margin-bottom: 1em;
}
.table {
  white-space: nowrap;
}
.table td {
  background: lavender;
}
.table button {
  font: inherit;
  border: 0;
  background: none;
  padding: 0;
  min-width: 100%;
}
<div class="wrapper">
  <table class="table">
    <tr>
      <td><button>Some long table header A</button></td>
      <td><button>Some long table header B</button></td>
      <td><button>Some long table header C</button></td>
     </tr>
  </table>
</div>

<div class="wrapper">
  <table class="table">
    <tr>
      <td><div>Some long table header A</div></td>
      <td><div>Some long table header B</div></td>
      <td><div>Some long table header C</div></td>
     </tr>
  </table>
</div>

<div class="wrapper">
  <table class="table">
    <tr>
      <td><button>Table header A</button></td>
      <td><button>Table header B</button></td>
      <td><button>Table header C</button></td>
     </tr>
    <tr>
      <td>Some long table content</td>
      <td>Some long table content</td>
      <td>Some long table content</td>
    </tr>
  </table>
</div>
Hidden Hobbes
  • 13,893
  • 3
  • 38
  • 64
  • about to post that! ;) – dippas Jan 22 '16 at 14:34
  • Ohh, nice, I didn’t try `min-width` yet! – poke Jan 22 '16 at 14:34
  • 2
    I’m accepting this since the `min-width` seems to be the cause of the problem. I’ve found [this bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1060131) which describes the issue very well and says that buttons with `width: 100%` apparently get an intrinsic `min-width: 0` which causes this issue. So setting the `min-width` explicitely will fix this. – poke Jan 22 '16 at 14:38
  • 1
    Very nicely found @HiddenHobbes! Plus one for you on that one! – Chris G Jan 22 '16 at 14:40
  • 1
    @poke Looks like there are a couple of bug reports regarding the problem, hopefully it is something that will be sorted soon. – Hidden Hobbes Jan 22 '16 at 14:41
  • 1
    The fix landed yesterday in mozilla-central, and in today’s Firefox Nightly build the problem is gone! – poke Feb 10 '16 at 14:28
  • @poke Hurrah, that was quick! – Hidden Hobbes Feb 10 '16 at 14:41
  • `min-width: 100%;` and `width: 0;` solved aotu width column problem. Thanks! – bozydarlelutko Mar 21 '18 at 14:29