为什么我不能使用属性隐藏CSS中的表列?

时间:2023-02-03 21:07:49

I found how I can hide a table column in this thread.

我发现如何在这个线程中隐藏表列。

I first tried

我第一次尝试

<colgroup>
  ...
  <col style="visibility:hidden;">
  ...
</colgroup>

and also

<colgroup>
  ...
  <col style="display:none;">
  ...
</colgroup>

but neither has any effect. I noticed that other styling properties don't work either on <col>s either.
What can the <col> tag be used for, and what would be its typical use?

但都没有任何影响。我注意到其他样式属性也无法在上运行。 标签可以用于什么,它的典型用途是什么?

2 个解决方案

#1


that's because the col-element isn't "visible" in the first place, it's just an "alias" for the table columns. If you want to hide the second column for-example, apply it directly to the tds like:

这是因为col-element首先不是“可见的”,它只是表列的“别名”。如果要隐藏第二列 - 例如,请将其直接应用于tds,例如:

tr td:nth-child(2)
{
 display: none;
 /* or */
 visibility: hidden;
}

#2


What is colgroup for?

The colgroup can be used the set the width, and only the width, of the columns of a given table.

colgroup可以用于设置给定表的列的宽度和宽度。

Solution for your use case

See this answer.

看到这个答案。

That means you need to apply the styles to each cell individually. You could try something like:

这意味着您需要单独将样式应用于每个单元格。你可以尝试类似的东西:

<head>
    ...
    <style type="text/css">
        table.myTable tr td:nth-child(4) {
            visibility:hidden;
        }
    </style>
    ...

where 4 is the index of the column.

其中4是列的索引。

nth-child documentation on MDN

关于MDN的n-child文档

#1


that's because the col-element isn't "visible" in the first place, it's just an "alias" for the table columns. If you want to hide the second column for-example, apply it directly to the tds like:

这是因为col-element首先不是“可见的”,它只是表列的“别名”。如果要隐藏第二列 - 例如,请将其直接应用于tds,例如:

tr td:nth-child(2)
{
 display: none;
 /* or */
 visibility: hidden;
}

#2


What is colgroup for?

The colgroup can be used the set the width, and only the width, of the columns of a given table.

colgroup可以用于设置给定表的列的宽度和宽度。

Solution for your use case

See this answer.

看到这个答案。

That means you need to apply the styles to each cell individually. You could try something like:

这意味着您需要单独将样式应用于每个单元格。你可以尝试类似的东西:

<head>
    ...
    <style type="text/css">
        table.myTable tr td:nth-child(4) {
            visibility:hidden;
        }
    </style>
    ...

where 4 is the index of the column.

其中4是列的索引。

nth-child documentation on MDN

关于MDN的n-child文档