CSS: Display Table on Small Screen

By Xah Lee. Date: . Last updated: .

Here's how to display table on mobile device that has small screen. (The technique is often called “responsive table”)

Here's how a table looks on laptop screen.

html table wide screen 2018-11-09 f9ff9
html table wide screen

On a small screen with narrow width, it looks like this:

html table small screen 2018-11-09 f0c4a
html table small screen

With CSS, you can make it to look like this:

html table responsive design 2018-11-09 143d9
html table responsive design

Here's a sample HTML table to play with.

DecOctHexCharComment
6510141ASomething something long text here. Too long and is a problem when displaying on a small screen. And it is really long, so long that on small narrow width screen one column becomes long strip while other columns are empty space, and page becomes very long, requiring lots scroll.
6610242BSomething something long text here. Too long and is a problem when displaying on a small screen. And it is really long, so long that on small narrow width screen one column becomes long strip while other columns are empty space, and page becomes very long, requiring lots scroll.

Here's html code for the table.

<table class="nrm">
<tr><th>Dec</th><th>Oct</th><th>Hex</th><th>Char</th><th>Comment</th></tr>
<tr><td>65</td><td>101</td><td>41</td><td>A</td><td>Something something long text here. Too long and is a problem when displaying on a small screen. And it is really long, so long that on small narrow width screen one column becomes long strip while other columns are empty space, and page becomes very long, requiring lots scroll.</td></tr>
<tr><td>66</td><td>102</td><td>42</td><td>B</td><td>Something something long text here. Too long and is a problem when displaying on a small screen. And it is really long, so long that on small narrow width screen one column becomes long strip while other columns are empty space, and page becomes very long, requiring lots scroll.</td></tr>
</table>

Here's the CSS:

table {
border-collapse:collapse;
margin:1rem;
}
table th, table td {
padding:.25rem;
border:solid thin gray;
}
table th {
background-color:#d7e4f2;
}

Responsive Table

Here's the CSS to change the look of table on small screen.

@media all and (max-width: 760px)
{

table {
display: block;
}

table caption {
display: inline-block;
}

table tr {
display: list-item;
border: none;
border-top: solid thin red;
}

table th {
display: inline-block;
border: solid thin gray;
margin-right: 0.2rem;
}

table td {
display: inline-block;
border: solid thin gray;
margin-right: 0.2rem;
}

}
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

CSS Basics

  1. Basics
  2. Tag Matching Tutorial
  3. Selector Syntax
  4. Units

Color

  1. Color Syntax
  2. Color Names
  3. Opacity
  4. Linear Gradient
  5. Radial Gradient

Font

  1. font-family
  2. Standard Web Fonts
  3. font-size
  4. Meaning of Font Size
  5. Font Weight
  6. Chinese Font Names

Box Model

  1. Border
  2. Outline
  3. Margin vs Padding
  4. Box Sizing

Layout

  1. Layout Tutorial
  2. Display Property
  3. Position: static, relative, fixed, absolute
  4. Pure CSS Table
  5. Tabbed Menu
  6. 3 Columns Page Layout
  7. Multi-Column Layout for Text Flow
  8. Fix Element to Window
  9. Centering
  10. Flowing List
  11. z-index
  12. Text Over Image
  13. Text Flow Around Image
  14. OverFlow, Scrollbar

Transform/Animation

  1. 2D Transform
  2. Transition
  3. Animation

Line Wrap, Justification

  1. Line Wrap
  2. Text Alignment, Justification
  3. Match first-letter and first-line
  4. “:before”, “:after”
  5. Text Decoration: Underline, Overline, Line-Through
  6. Letter Spacing, Word Spacing

Box Decoration

  1. Box Shadow
  2. Text Shadow
  3. Round Corners
  4. Background Image
  5. Image Rollover

Misc

  1. Computed Style
  2. Pseudo Class vs Pseudo Element
  3. Media Query
  4. Variable
  5. calc
  6. Reset
  7. user-select
  8. Data URI Scheme
  9. protocol-relative URL
  10. Default Unit
  11. Declaring Character Set in CSS File
  12. Jargons Explained: Tag, Element, Node, Object, Attribute, Property, Method
  13. Display Table on Small Screen
  14. Fixed Aspect Ratio
  15. Add Icon to Links
  16. Progress Bar Example

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials