Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active July 27, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/d1ab2d9adfad940ed0723a11bdeb6a5e to your computer and use it in GitHub Desktop.
Save code-boxx/d1ab2d9adfad940ed0723a11bdeb6a5e to your computer and use it in GitHub Desktop.
HTML CSS Responsive Tables

HTML CSS RESPONSIVE TABLE

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Full Width Table</title>
<style>
/* (A) THIS IS ALL WE NEED */
.full { width: 100%; }
/* (B) NOT IMPORTANT - FOR DEMO */
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
.full th, .full td { text-align: center; padding: 10px; }
.full th { background: #383bc7; color: #fff; }
.full td { background: #e3e3e3; }
</style>
</head>
<body>
<table class="full">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Container And Scrollbar</title>
<style>
/* (A) WRAPPER WITH OVERFLOW */
.twrap {
width: 100%;
overflow-x: auto;
}
/* (B) NOT IMPORTANT - FOR DEMO */
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
.twrap table { width: 200%; }
.twrap th, .twrap td { text-align: center; padding: 10px; }
.twrap th { background: #383bc7; color: #fff; }
.twrap td { background: #e3e3e3; }
</style>
</head>
<body>
<div class="twrap">
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</table>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Hiding Columns</title>
<style>
/* (A) HIDE SECOND COLUMN ON SMALL SCREENS */
.hidder { width: 100%; }
@media screen and (max-width:768px) {
.hidder th:nth-child(2),
.hidder td:nth-child(2)
{ display: none; }
}
/* (B) NOT IMPORTANT - FOR DEMO */
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
.hidder th, .hidder td { text-align: center; padding: 10px; }
.hidder th { background: #383bc7; color: #fff; }
.hidder td { background: #e3e3e3; }
</style>
</head>
<body>
<table class="hidder">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
</tr>
</table>
</body>
</dtml>
<!DOCTYPE html>
<html>
<head>
<title>Wrapping Columns Demo</title>
<style>
/* (A) REARRANGE TABLE ON SMALL SCREENS */
.gridtable { width: 100%; }
@media screen and (max-width:768px) {
.gridtable tr {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-gap: 3px;
margin-bottom: 3px;
}
}
/* (B) NOT IMPORTANT - FOR DEMO */
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
.gridtable th, .gridtable td { text-align: center; padding: 10px; }
.gridtable th { background: #383bc7; color: #fff; }
.gridtable td { background: #e3e3e3; }
</style>
</head>
<body>
<table class="gridtable">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Grid as Table</title>
<style>
/* (A) ON BIG SCREEN */
.thegrid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-gap: 3px;
}
/* (B) ON SMOL SCREEN */
@media screen and (max-width:768px) {
.thegrid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
/* (C) NOT IMPORTANT - FOR DEMO */
* { font-family: Arial, Helvetica, sans-serif; box-sizing: border-box; }
.thegrid .head, .thegrid .cell { text-align: center; padding: 10px; }
.thegrid .head { font-weight: 700; background: #383bc7; color: #fff; }
.thegrid .cell { background: #e3e3e3; }
</style>
</head>
<body>
<div class="thegrid">
<!-- HEAD -->
<div class="head">Column 1</div>
<div class="head">Column 2</div>
<div class="head">Column 3</div>
<div class="head">Column 4</div>
<!-- ROWS -->
<div class="cell">Column 1</div>
<div class="cell">Column 2</div>
<div class="cell">Column 3</div>
<div class="cell">Column 4</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment