Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive table using CSS display: table-row

I'm having trouble converting my table to a more responsive design, I've tried many resources on Google but they are all limited and do not work like intended. The underlying problem I'm having is that my tables are dynamically generated and I do not want to edit resources I find on Google so I can update them easily and don't mess with the code too much.

Anyway, my table has a really simple design. There are 5 columns with X rows (depending on what's being generated from the database). Whenever an user resizes their browser (or when they are using a mobile phone) I want to change my table so it is fitting on smaller screens as well. An example of this would be:

Desktop

Header 1 | Header 2 | Header 3
------------------------------
Content  | Content  | Content

Phone

Header 1 | Content
------------------
Header 2 | Content
------------------
Header 3 | Content

I'm trying to accomplish this by using display: table-row using CSS whenever the screen hits a certain width, I created this jsfiddle to display my current code. However currently the th and td are displayed vertically above/under each other, and not next to each other like I intended to do. I tried using float: left and float: right but that does not work (see the jsfiddle). Can anybody help me accomplish what I want using CSS? And if that is not possible, how would I go over using Javascript for this, keeping in mind that my tables will not all be generated when the page is loaded (some are dynamically added to the page)?

like image 855
Kevin Voorn Avatar asked Jan 07 '15 10:01

Kevin Voorn


People also ask

What is a responsive table in HTML?

Responsive Tables. A responsive table will display a horizontal scroll bar if the screen is too small to display the full content.

What are the characteristics of responsive CSS Design?

The responsive table is a good combination of icons, font styles and appealing buttons. 10. Fixed Table Header This example of css table layout has fixed header which means you can only go up to certain limit for compressing it. Its not always wise to break each row into a separate table for achieving responsive css design.

Is there a way to make a table responsive?

There are many types of tables on websites where content can vary as wildly as the approaches used to make them responsive. The tables I find most frustrating are comparison tables or normal content layout tables, there are really no comprehensive CSS based solutions for making these types of tables responsive.

What are some examples of table layout in CSS?

3. Simple Responsive Table in CSS This is one of the examples on table layout that focuses on responsive feature more than attractive css layout. You can choose the layout as per your organization’s theme because for users its just the matter of how efficiently they can view it. If you ask my opinion I like the mobile layout more than the web one.


3 Answers

Using thead and tbody elements, you could try like so:

http://jsfiddle.net/uzsw7s4o/

CSS

@media screen and (max-width: 750px) {
    tbody, thead { float: left; }
    thead { min-width: 120px }
    td,th { display: block }
}
like image 104
Fabrizio Calderan Avatar answered Oct 11 '22 16:10

Fabrizio Calderan


How about adjusting the table definitions for the different screen widths. This also prevents any unanticipated side effects of floating your elements.

Resizable Fiddle

.table {
  display: table;
  width: 100%;
}
.row {
  display: table-row;
}
.cell {
  display: table-cell;
}
@media screen and (max-width: 750px) {
  .cell {
    display: block;
  }
  .row {
    display: table-cell;
    width: 50%;
  }
}
<div class="table">
  <div class="row">
    <div class="cell">Head #1</div>
    <div class="cell">Head #2</div>
    <div class="cell">Head #3</div>
    <div class="cell">Head #4</div>
  </div>
  <div class="row">
    <div class="cell">Content #1</div>
    <div class="cell">Content #2</div>
    <div class="cell">Content #3</div>
    <div class="cell">Content #4</div>
  </div>
</div>
like image 20
SW4 Avatar answered Oct 11 '22 14:10

SW4


I have forked you fiddle. That is the way I would solve it:

HTML

<div class="row">
    <span>Head #1</span>
    <span>Head #2</span>
    <span>Head #3</span>
    <span>Head #4</span>
</div>
<div class="row">
    <span>Content #1</span>
    <span>Content #2</span>
    <span>Content #3</span>
    <span>Content #4</span>
</div>

CSS

.row
{
    display: table-row;
}
.row > *
{
    display: table-cell;
}

@media screen and (max-width: 750px) {
    .row
    {
        display: block;
        float: left;
    }
    .row > *
    {
        display: block;
    }
}
like image 1
Joshua Behrens Avatar answered Oct 11 '22 16:10

Joshua Behrens