【问题标题】:CSS 'visibility:collapse' bug between Safari and Chrome?Safari 和 Chrome 之间的 CSS 'visibility:collapse' 错误?
【发布时间】:2021-12-15 03:46:30
【问题描述】:

编写一个简单的html 表时,我在浏览器之间遇到了一个令人惊讶的渲染错误。
因为它太基础了,也许我做错了什么......

当您加载这个MDN page 时,您可以看到一个非常简单的表格(带有htmlcss)。

我的目标是隐藏第一列。
乍一看好像很简单,加style="visibility:collapse"
下面所有html代码:

<table>
    <caption>Superheros and sidekicks</caption>
    <colgroup>
        <col style="visibility:collapse">
        <col span="2" class="batman">
        <col span="2" class="flash">
    </colgroup>
    <tr>
        <td> </td>
        <th scope="col">Batman</th>
        <th scope="col">Robin</th>
        <th scope="col">The Flash</th>
        <th scope="col">Kid Flash</th>
    </tr>
    <tr>
        <th scope="row">Skill</th>
        <td>Smarts</td>
        <td>Dex, acrobat</td>
        <td>Super speed</td>
        <td>Super speed</td>
    </tr>
</table>

它在Chrome 中完美运行,但在Safari 中却不行。其他浏览器不知道。
这是为什么?有破解吗?
谢谢。

【问题讨论】:

  • 至于为什么会这样,只是因为苹果从来不关心实现它。见bugs.webkit.org/show_bug.cgi?id=8735。如下所述,将display:none 应用于 td:nth-child(3) 或其他任何可行的解决方法。
  • 谢谢,我就是这么做的。但是,如果没有这个错误,我的代码会更干净......

标签: css google-chrome browser html-table safari


【解决方案1】:

实际上这不是一个错误。问题出在表结构上。您有 5 列,但 &lt;col span="2"&gt; 不起作用。该属性不是span,而是colspan。如果您正确编写所有 5 列,则示例有效。请参阅此处,Cell 1 的第一列已隐藏。

<table>
    <caption>Superheros and sidekicks</caption>
    <colgroup>
        <col style="visibility:collapse">
        <col class="batman">
        <col class="flash">
        <col>
        <col>
    </colgroup>
    <tr>
        <th>Cell 1</th>
        <th scope="col">Batman</th>
        <th scope="col">Robin</th>
        <th scope="col">The Flash</th>
        <th scope="col">Kid Flash</th>
    </tr>
    <tr>
        <th scope="row">Skill</th>
        <td>Smarts</td>
        <td>Dex, acrobat</td>
        <td>Super speed</td>
        <td>Super speed</td>
    </tr>
</table>

【讨论】:

  • 您的示例(第一列被隐藏/折叠)在Chrome 中有效,而在Safari 中无效。因此,错误... :-)。有什么想法吗?
  • 真的吗?我没有要测试的 Safari。 Caniuse.com 说它是 Safari 支持的......然后你可以使用 'nth-child' 来隐藏表格单元格并以此方式隐藏列。
  • " 不起作用。属性不是 span 而是 colspan。"这是错误的。属性为span
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2012-03-24
  • 2010-09-19
  • 2014-08-18
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
相关资源
最近更新 更多