How can I make a CSS table fit the screen width?

David B picture David B · Nov 21, 2010 · Viewed 102.9k times · Source

Currently the table is too wide and causes the browser to add a horizontal scroll bar.

Answer

Kai Noack picture Kai Noack · Oct 4, 2013

CSS:

table { 
    table-layout:fixed;
}

Update with CSS from the comments:

td { 
    overflow: hidden; 
    text-overflow: ellipsis; 
    word-wrap: break-word;
}

For mobile phones I leave the table width but assign an additional CSS class to the table to enable horizontal scrolling (table will not go over the mobile screen anymore):

@media only screen and (max-width: 480px) {
    /* horizontal scrollbar for tables if mobile screen */
    .tablemobile {
        overflow-x: auto;
        display: block;
    }
}

Sufficient enough.