Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant HTML5 Responsive Table Design How-to

You're reading from   Instant HTML5 Responsive Table Design How-to Present your data everywhere on any device using responsive design techniques with this book and ebook

Arrow left icon
Product type Paperback
Published in Apr 2013
Publisher Packt
ISBN-13 9781849697262
Length 58 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Fernando Monteiro Fernando Monteiro
Author Profile Icon Fernando Monteiro
Fernando Monteiro
Arrow right icon
View More author details
Toc

Merging data – numbers and text (Advanced)


We introduce an alternative based on CSS3 for dealing with tables containing text and numbers.

Getting ready

Tables are used for different purposes, we will see an example where our data is not a data table. (Code Example: Chapter07_Codes_1)

Browser screenshot at 1024px

Although our table did not have many columns, showing it on a small screen is not easy. Hence we will progressively show the change in the table by subtracting the width of the screen.

How to do it...

Note that we have removed the .table class so this time apply the style directly in the table tags, see the following steps:

  1. Let's use a simple table structure as we saw before.

  2. Add some CSS3 to make some magic with our selectors.

  3. Set our breakpoints to two sizes.

    <table>
          <thead>
            <tr>   
            <th>CONTACT</th>
            <th scope="col">Manager</th>
            <th scope="col">Label</th>
            <th scope="col">Phone</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">Black Sabbath</th>
              <td>Richard Both</td>
              <td>Atlantic</td>
              <td>+1 (408) 257-1500 </td>    
            </tr>
            <tr>
              <th scope="row">Ac/DC</th>
              <td>Paolla Matazo</td>
              <td>Sony</td>
              <td>+1 (302) 236-0800</td>   
            </tr>
            <tr>
              <th scope="row">Queen</th>
              <td>Suzane Yeld</td>
              <td>Warner</td>
              <td>+1 (103) 222-6754</td>   
            </tr>
            <tr>
              <th scope="row">Whitesnake</th>
              <td>Paul S. Senne</td>
              <td>Vertigo</td>
              <td>+1 (456) 233-1243</td>   
            </tr>
            <tr>
              <th scope="row">Deep Purple</th>
              <td>Ian Friedman</td>
              <td>CosaNostra</td>
              <td>+1 (200) 255-0066</td>   
            </tr>
        </tbody>
      </table>
  4. Applying the style as follows:

      table {
            width: 100%;
            background-color: transparent;
            border-collapse: collapse;
            border-spacing: 0;
            background-color: #fff
        }
        th {
          text-align: left;
        }
        td:last-child, th:last-child {
          text-align:right;
        }
        td, th {
          padding: 6px 12px;
        }
        tr:nth-child(odd), tr:nth-child(odd) {
          background: #f3f3f3;
        }
        tr:nth-child(even) {
          background: #ebebeb;
        }
        thead tr:first-child, thead tr:first-child {
          background: #000;
          color:white;
        }
        table td:empty {
          background:white;
        }
  5. We use CSS3 pseudo-selectors here again to help in the transformation of the table. And the most important part, the Media Queries breakpoints:

    @media (max-width: 768px) {
          tr :nth-child(3) {display: none;}
        }
        
        @media (max-width: 480px) {
          thead tr:first-child {display: none;}
          th {display: block}
          td {display: inline!important;}
        }
  6. When the resolution is set to 768px, we note that the penultimate column is removed.

    This way we keep the most relevant information on the screen. We have hidden the information less relevant to the subject. And when we decrease further, we have the data distributed as a block.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image