How To Best Hide A Table Column For Mobile View - 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.

    Explore Teams Create a free Team
  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 How to best hide a table column for mobile view Ask Question Asked 10 years, 11 months ago Modified 6 years, 4 months ago Viewed 39k times 19

I have a HTML table, in a mobile browser I need to hide some of its columns.

I want to hide 2 or 3 of these cols using @media queries if possible. the below code didn't work for me:

<table> <tr> <td></td> <td class='collapsable'></td> <td class='collapsable'></td> <td></td> </tr> </table> //hide cols here td.collapsable {display:block; } @media only screen and (min-width: 450px) { //show cols here td.collapsable {display:none; } } Share Improve this question Follow edited Feb 12, 2018 at 11:45 Brian Tompsett - 汤莱恩's user avatar Brian Tompsett - 汤莱恩 5,83772 gold badges60 silver badges132 bronze badges asked Jul 24, 2013 at 15:03 IEnumerable's user avatar IEnumerableIEnumerable 3,74014 gold badges51 silver badges79 bronze badges 6
  • Plan B: put a DIV in each cell. Class the DIVs by column. Hide the DIVs. – Diodeus - James MacFarlane Commented Jul 24, 2013 at 15:11
  • ok, I see where you're at. Yes if no one post a direct solution that actually collapses that will have to do :) – IEnumerable Commented Jul 24, 2013 at 15:12
  • Should your media query be max-width if you are hiding the column for smaller displays? – user1171848 Commented Jul 24, 2013 at 15:17
  • 1 im coding css for mobile first, then using media queries for desktop. My question was worded the other way around. – IEnumerable Commented Jul 24, 2013 at 15:18
  • Mmm I've seen this a lot. Assign class to the part that may not be displayed. Gues what? You still request the data.... Just don't post data to the mobiles. Or you expect all users to have unlimited data limits?? – Ron van der Heijden Commented Jul 24, 2013 at 15:30
| Show 1 more comment

4 Answers 4

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

Something like this? (try resizing the Result window/pane)

The HTML:

<tr> <td class="one">corpse</td> <td>corpse</td> <td>corpse</td> </tr>

The CSS:

table{ width: 50%; } td, th{ border: 1px solid orange; } @media only screen and (max-width: 900px) { .one{ display: none; } } Share Improve this answer Follow answered Jul 24, 2013 at 15:23 Zubzob's user avatar ZubzobZubzob 2,7134 gold badges29 silver badges46 bronze badges 5
  • yes the jsfiddle looks good. love it thankyou; exactly what I was looking for. No more JS :) – IEnumerable Commented Jul 24, 2013 at 15:26
  • @IEnumerable isn't this exactly what you had written in your question? – punkrockbuddyholly Commented Jul 24, 2013 at 15:37
  • Oh I see, it's effectively the opposite of what you did. – punkrockbuddyholly Commented Jul 24, 2013 at 15:47
  • 5 thats one way to get rid of a corpse – Jason Portnoy Commented Jan 17, 2016 at 22:35
  • Thanks, I had the same problem but I also added CSS style display:block; for class .one outside of the @media to make it visible again on all other screen sizes, but apparently that is not necessary. – Al John Commented Jan 20, 2020 at 12:04
Add a comment | 14

Keep your HTML and table markup clean by using nth-child to hide the column. Should work with every modern browser that supports media queries.

Here's a sample fiddle showing it in action. Resize the result pane to see it work.

https://jsfiddle.net/tycahill/xm0nwr7x

The clean HTML:

<table> <tr> <td>Column 1a</td> <td>Column 2a</td> <td>Column 3a</td> </tr> <tr> <td>Column 1b</td> <td>Column 2b</td> <td>Column 3b</td> </tr> </table>

The CSS to hide the second column:

@media only screen and (max-width: 800px) { td:nth-child(2) { display:none; } } Share Improve this answer Follow answered Sep 1, 2016 at 16:47 Ty the Web Guy's user avatar Ty the Web GuyTy the Web Guy 1911 silver badge4 bronze badges 0 Add a comment | 2

Plan B: put a DIV in each cell. Class the DIVs by column. Hide the DIVs. You may need to adjust your table's cellpadding/cellspacing and put it on the .cell DIV to maintain the previous appearance.

<table> <tr> <td><div class='cell'></div></td> <td><div class='cell collapsable'></div></td> <td><div class='cell collapsable'></div></td> <td><div class='cell'></div></td> </tr> </table> Share Improve this answer Follow answered Jul 24, 2013 at 15:19 Diodeus - James MacFarlane's user avatar Diodeus - James MacFarlaneDiodeus - James MacFarlane 114k33 gold badges160 silver badges179 bronze badges 1
  • good work with the .cell - I just tried it out and worked fine. – IEnumerable Commented Jul 24, 2013 at 15:23
Add a comment | 2

I think your example has the display:block and display:none switched. Also, I would recommend using display: table-cell; instead of display: block;, since table-cell is the default display for <td> elements. This CSS worked for me, and also is a mobile-first approach:

td.collapsable {display:none; } @media only screen and (min-width: 450px) { td.collapsable {display:table-cell; } } Share Improve this answer Follow answered Jul 24, 2013 at 15:27 user1171848's user avatar user1171848user1171848 2961 gold badge4 silver badges10 bronze badges 2
  • 1 Im having trouble making this work, do I apply the class to just the th or each td as well ? – IEnumerable Commented Jul 24, 2013 at 15:31
  • I applied it to only the td. Maybe it's a browser-specific issue; have you tested in other browsers? – user1171848 Commented Jul 24, 2013 at 15:32
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.

  • Featured on Meta
  • We spent a sprint addressing your requests — here’s how it went
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • The [lib] tag is being burninated
  • What makes a homepage useful for logged-in users
1 Is there any way to hide a column in a table? 7 Hide Columns in responsive table using css 0 How can i hide certain table column in a HTML table using CSS 2 How to hide some of the columns in the table when in mobile view? 1 Hide Table Column in CSS 30 bootstrap-table hide column in mobile view 3 Hide HTML Table Columns 3 For mobile first approach,how to display all table column in desktop size after hiding some column in mobile size 0 Hide table columns for mobile view 0 How to Hide Mysql table Column in mobile version

Hot Network Questions

  • Any alternative to lockdown browser?
  • Why do Electric Aircraft Seem to Eschew Photovoltaics?
  • LilyPond: how do I visualize Bezier control points?
  • How to prepare stack pointer for bare metal Rust?
  • Plastic plugs used to fasten cover over radiator
  • How to photograph the lettering on a bronze plaque?
  • Why was this a draw? What move I supposed to play to win?
  • Would this telescope be capable to detect Middle Ages Civilization?
  • Does it make sense to use a skyhook to launch and deorbit mega-satellite constellations now?
  • What is gñânendriyas?
  • Why did Nigel Farage choose Clacton as the constituency to campaign in?
  • PCIe digest explanation
  • When is bribery legal?
  • Challenge the appointment of the prime minister
  • Why not build smaller Ringworlds?
  • What makes Python better suited to quant finance than Matlab / Octave, Julia, R and others?
  • In a sum of high-variance lognormals, what fraction comes from the first term?
  • Will the 3D effect change shockwave angle/strength?
  • 'adb help' for 'root' says "restart adbd with root permissions", but 'adb root' says "adbd cannot run as root in production builds"
  • How to POSIX-ly ignore "warning: command substitution: ignored null byte in input"?
  • Does closedness of the image of unit sphere imply the closed range of the operator
  • The reading of the Ethiopian eunuch conversion story without Acts 8:37
  • How can I export my Location History now that this data is only stored locally on the phone?
  • "Though Fancy's casket were unlock'd to choose" in John Keats's "Lamia"
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 When Small