How to hide columns in HTML table?

后端 未结 9 1106
执念已碎
执念已碎 2020-11-27 14:07

I have created a table in ASPX. I want to hide one of the columns based on the requirement but there is no attribute like visible in the HTML table building. Ho

9条回答
  •  一生所求
    2020-11-27 14:36

    Kos's answer is almost right, but can have damaging side effects. This is more correct:

    #myTable tr td:nth-child(1), #myTable th:nth-child(1) {
        display: none;
    }
    

    CSS (Cascading Style Sheets) will cascade attributes to all of its children. This means that *:nth-child(1) will hide the first td of each tr AND hide the first element of all td children. If any of your td have things like buttons, icons, inputs, or selects, the first one will be hidden (woops!).

    Even if you don't currently have things that will be hidden, image your frustration down the road if you need to add one. Don't punish your future self like that, that's going to be a pain to debug!

    My answer will only hide the first td and th on all tr in #myTable keeping your other elements safe.

提交回复
热议问题