Alternative to visibility:collapse not working on IE and Chrome
Use
display: none
instead of
visibility: collapse
It works for me to hide the dojo tree grid summary row in IE6 & Google Chrome
CSS col visibility:collapse does not work on Chrome
You are right, chrome doesn't properly support visibility:collapse for table rows and columns -- follow the bug for updates. We are planning on tackling it in the next few months but it probably won't show up in stable until the end of 2017. Sorry about the bad news.
Chrome Visibility: Collapse
Chrome and Safari treat visibility: collapse
as visibility: hidden
.
This will only work in Firefox/IE.
You can change it to display: none
to make sure it works the same in all browsers, however this way you will miss the general idea of the collapse
value, where all the width/height of the table's elements are calculated and take into account while affecting other elements in the table:
.collapse { display: none;}
<table> <caption>This is a Table</caption> <thead> <tr> <th>Column 1</th> <th>Column 2</th> </tr> </thead> <tbody class='collapse'> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> </tr> </tbody> <tfoot> <tr> <td>TOTAL 1</td> <td>TOTAL 2</td> </tr> </tfoot></table>
Visibility:collapse is rendered as visibility:hidden
It won't work as intended on all browsers, only Firefox and IE (I can't confirm IE right now). The MDN docs on visibility
say:
The support for
visibility:collapse
is missing or partially incorrect in some modern browsers. In many cases it may not be correctly treated likevisibility:hidden
on elements other than table rows and columns.
You can test it with this jsFiddle: http://jsfiddle.net/meT7k/. In Chrome, the first row is rendered as visibility: hidden
. In Firefox, collapse
is applied correctly.
In Chrome, you get the desired results when applying display: none
to the row instead. However, that will probably force a re-layout of the whole table (e.g., column widths may change after the display
property is changed).
What are some good work arounds to deal with the Visiblility: Collapse bug in Mozilla Firefox?
As @Richard Deeming has pointed out, this rendering issue is related to the border-collapse: collapse;
property.
If you can do without it and centering is also neglectable –
removing the border-collapse: collapse;
property from your table might be a valid compromise.
function hideColumn(columns) {
columns.forEach(function(num, i) {
columns[i] = "#col_" + columns[i];
});
let selectors = columns.join(", ");
// reset previously hidden
let hidden = document.querySelectorAll(".collapsed-col");
hidden.forEach(function(el) {
el.classList.remove("collapsed-col");
});
// hide cells by class
let cells = document.querySelectorAll(selectors);
cells.forEach(function(item, i) {
item.classList.add("collapsed-col");
});
}
// test input
let inputHideColumns = document.querySelector(".inputHideColumns");
inputHideColumns.addEventListener("change", function(e) {
let value = e.target.value.replaceAll(", ", ",");
let columns = value.split(",");
hideColumn(columns);
});
.collapsed-col {
visibility: collapse;
}
table {
border-spacing: 0;
/*
border-collapse: collapse;
margin: auto;
*/
border: 1px solid black;
table-layout: fixed;
}
th {
border-bottom: 1px solid grey;
}
td {
text-align: center;
}
<body>
<div style="text-align: center;">
<table>
<colgroup>
<col id="col_1">
<col id="col_2">
<col id="col_3">
<col id="col_4" class="collapsed-col">
<col id="col_5" class="collapsed-col">
<col id="col_6">
</colgroup>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
<th>Col 6</th>
</tr>
<tr>
<td>1.1</td>
<td>1.2</td>
<td>1.3</td>
<td>1.4</td>
<td>1.5</td>
<td>1.6</td>
</tr>
<tr>
<td>2.1</td>
<td>2.2</td>
<td>2.3</td>
<td>2.4</td>
<td>2.5</td>
<td>2.6</td>
</tr>
</table>
</div>
<p>
<label>hide columns (comma seperated)</label>
<input class="input inputHideColumns" type="text" placeholder="hide columns (comma seperated)" value="5,4">
</p>
</div>
Related Topics
How to Make a Div Full Screen and Scrollable
Bootstrap Collapse Accordion - Default Expand/Collapse
Mvc Bundling and Relative CSS Image When Website Is Deployed to an Application
Why Does Firefox Treat Helvetica Differently from Chrome
Is There Something "Like" CSS Built into Xsl-Fo
Overwriting Styles from Bootstrap in an Angular2 Component-Stylesheet
Bootstrap 4 Center Pagination in Column
Horizontal Scrollbar Only Appearing at Bottom of Page
How to Use CSS3: :Selection Without Changing The Default Color and Background Color
CSS Sprite Techniques, CSS Background or Img's Clip
CSS Terminology: What Are These Called
CSS- Target Text Links with Bottom Border on Hover, But Image Links with No Border
Shiny Dashboadpage Lock Dashboardheader on Top
Styling Links Inside a Div with a Specific Class
Right Aligning Cell Content in a Datatable Column
How to Select CSS Id's with Numbers in Them