3

How to make a reactive table in CSS

 2 years ago
source link: https://www.codesd.com/item/how-to-make-a-reactive-table-in-css.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to make a reactive table in CSS

advertisements

I have tried the piece of code mentioned below for a table, but it doesn't make it responsive. Can anyone make modifications to this code to make it responsive?

.content table {
    border-collapse:collapse;
    margin:0 0 1.625em;
    width:100%;
    font-size:90%
}
.content table h3 {
    font-size:1em;
    font-weight:300
}
.content tbody {
    border-bottom:1px solid #444;
    border-left:1px solid #444
}
.content tr:nth-child(odd) {
    background-color:#eee
}
.content table th {
    background-color:#0078d7;
    color:#fff;
    font-size:.85em
}
.content td,.content th {
    vertical-align:top
}
.content th,.content td {
    padding:3px 10px;
    text-align:left;
    border-top:1px solid #444;
    border-right:1px solid #444
}

$menu .= '<table class="content"><thead><tr><th>Name</th><th>Description</th>‌​<th>Price</th></tr><‌​/thead>'; 

while ( have_rows('sections_items') ) : the_row();
    // Your loop code
    $menu .= '<tr><td>'.get_sub_field('dish_names').'</td><td>'.get_sub_f‌ield('dish_descripti‌​on').'</td><td>$ '.get_sub_field('dish_price').'</td></tr>';
endwhile;

$menu .= '</table> ';


What I did for showing table on mobile was either using bootstrap grid feature or modify table display style. I use them both depending on the content I want to show.

The key is to add @media query so that it changes based on screen size:

@media (max-width: 767px){
    table{
        //styles you want to change
    }
}

If you have to use table elements, you can change the style of table and td elements to display: block on mobile and modify them from then.

Another way to do this which is not recommended is to create a whole new view for mobile only and hide the original table using media query. This creates duplicate data but gives you the freedom to custom mobile view at will.

example:

.mobile-table{ // or whatever class you want
    display: none;
}

@media (max-width: 767px){
    table{
        display: none;
    }
    .mobile-table{
        display: block;
    }
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK