CSS For Hiding Multiple Columns In A Table - Html - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs CSS for hiding multiple columns in a table Ask Question Asked 11 years ago Modified 7 years, 4 months ago Viewed 85k times 24

I have a table similar to the one illustrated below in a SharePoint site. I cannot modify the table as it is generated dynamically but I can add external CSS to override its style. I am required to show only second column and hide first, third and fourth column.

The pseudo class to hide first column is

table#student tr td:first-child { display: none; }

Please help me with pseudo class or any other trick to hide third and forth column.

<table id="student"> <tr> <td>Role</td> <td>Merin</td> <td>Nakarmi</td> <td>30</td> <tr> <td>Role</td> <td>Tchelen</td> <td>Lilian</td> <td>22</td> </tr> <tr> <td>Role</td> <td>Suraj</td> <td>Shrestha</td> <td>31</td> </tr> </table> Share Improve this question Follow edited Jul 27, 2017 at 12:02 Brian Tompsett - 汤莱恩's user avatar Brian Tompsett - 汤莱恩 5,87572 gold badges60 silver badges133 bronze badges asked Nov 20, 2013 at 8:26 Merin Nakarmi's user avatar Merin NakarmiMerin Nakarmi 3,4183 gold badges37 silver badges43 bronze badges Add a comment |

5 Answers 5

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 33

CSS3:

table#student td { display: none; } table#student td:nth-child(2) { display: block; }

Use nth-child selector to un-hide the 2nd <td> of every row, effectively showing the 2nd column.

Share Improve this answer Follow edited Nov 15, 2015 at 13:51 Shimmy Weitzhandler's user avatar Shimmy Weitzhandler 104k126 gold badges434 silver badges639 bronze badges answered Nov 20, 2013 at 8:32 AlienHoboken's user avatar AlienHobokenAlienHoboken 2,79021 silver badges23 bronze badges 3
  • 4 These days, display: table-cell; is more appropriate. – Fabien Haddadi Commented Feb 22, 2018 at 4:57
  • 2 Unfortunately this breaks tables using colspan. – Dai Commented May 26, 2020 at 8:56
  • 1 @Dai I had exactly this problem and a solution which the space here is too narrow to contain. – theking2 Commented Oct 3, 2021 at 15:06
Add a comment | 25

You can use the CSS3 :nth-child() selector

td:nth-child(3), td:nth-child(4) { display: none }

jsfiddle here

Share Improve this answer Follow edited Nov 20, 2013 at 8:43 answered Nov 20, 2013 at 8:35 Aurélien Gasser's user avatar Aurélien GasserAurélien Gasser 3,0901 gold badge21 silver badges25 bronze badges Add a comment | 3

I'm surprised no one mentioned the general sibling selector. (More info here) If you only need to show second column, I'd apply a display: none; style to the first cell and all cells after the second one.

table#student td:first-child, table#student td:nth-child(2) ~ td { display: none; } <table id="student"> <tr> <td>Role</td> <td>Merin</td> <td>Nakarmi</td> <td>30</td> <tr> <td>Role</td> <td>Tchelen</td> <td>Lilian</td> <td>22</td> </tr> <tr> <td>Role</td> <td>Suraj</td> <td>Shrestha</td> <td>31</td> </tr> </table>

If you need to support IE7 and IE8 for some reason, you could replace the :nth-child() selector with the adjacent sibling selector:

table#student td:first-child, table#student td + td ~ td { display: none; } Share Improve this answer Follow answered Dec 4, 2015 at 22:09 stealththeninja's user avatar stealththeninjastealththeninja 3,7902 gold badges29 silver badges44 bronze badges Add a comment | 0

Here you go.

The CSS:

table#student tr td:first-child, table#student tr td:nth-child(3), table#student tr td:nth-child(4) { display: none; }

WORKING DEMO

Share Improve this answer Follow answered Nov 20, 2013 at 8:38 Nitesh's user avatar NiteshNitesh 15.7k4 gold badges50 silver badges60 bronze badges Add a comment | -3

.hideFullColumn tr > .hidecol { display:none; }

You can use .hideFullColumn in the table and use .hidecol in the tag which you want to hide. You don't need to worry about td as those will automatically be hidden.

Pseudo class is fine but if you have 50 columns and have to hide 20, then you'd have to repeat the "td:nth-child(1),td:nth-child(2),...." 20 times by keeping the index in mind. But in this case you can add the .hidecol class on creating th, so you don't need to revise index.

You can try this and please let me know if it works.

Share Improve this answer Follow edited May 8, 2015 at 9:18 SuperBiasedMan's user avatar SuperBiasedMan 9,94910 gold badges50 silver badges75 bronze badges answered May 8, 2015 at 8:40 souvik sett's user avatar souvik settsouvik sett 831 silver badge6 bronze badges 1
  • don't use it as it only hides th. – souvik sett Commented May 8, 2015 at 9:01
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

43 CSS for first td of each tr in certain table 0 JQuery hide table column 1 How to hide or show a html table column 0 How to hide one cell in the ASCIIDOC table? -2 Hide multiple span element (span_0_1, span_1_1, span_2_1) from a column html table 1 Is there any way to hide a column in a table? 40 show/hide html table columns using css 10 Is it possible to hide / show table columns by changing the CSS class on the col element only? 2 Hide columns in HTML table via CSS 0 How can i hide certain table column in a HTML table using CSS 1 Hiding a column of table with a class 1 Hide Table Column in CSS 2 Hide table columns by class 3 Hide HTML Table Columns 0 Hide columns in a table using CSS

Hot Network Questions

  • Can we use the simple present tense to express a habit that starts just about 24 hours or less ago?
  • Brushing pastries with jam
  • Scandinavian film, 1980s, possibly called Royal Toilet?
  • How to detect if the KERNAL load/save routine is currently active on a CBM system?
  • Density in the real line
  • Are periodic functions such as sine and cosine defined on surreal numbers?
  • Why「记」for shop names?
  • User temp folder is 103GB; is it safe to delete?
  • Superimposed triangles
  • Asymptotic Expansion of a Recurrence Relation Involving a Square Root
  • Conditionally making environment content disappear
  • Is it legal to take advantage of loopholes in GAAP to misrepresent profit?
  • Does Windows 11 PIN Behavior Break Password Security Conventions?
  • Biasing common-source NMOS with active load and fixed Vgs
  • endless looping in minimax
  • Accused of violating NDA on thesis
  • Nest "For Each" loops in Geometry Nodes
  • Did Superdana manufacture a 66 AC outlet power strip/surge protector?
  • Is there a theorem in metaphysics that basically says, "Biting the bullet will be inevitable in any metaphysical theory?"
  • What is the basis for the view of Oneness in Theravada?
  • block nvme0n1: no uuid available providing old nguid - after disk cloning
  • Commencement and repeal dates in UK Acts of Parliament - meaning of "From and after"
  • How can I draw and adjust the max and min values of the curves?
  • Can I protect my EV car charger's cable with aluminum tape and a stainless steel hose helix?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html Table Hide Column Css