Skip to content

Instantly share code, notes, and snippets.

@embarq
Last active June 22, 2016 01:36
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 embarq/0f88587f87796be48b034cb758ed4593 to your computer and use it in GitHub Desktop.
Save embarq/0f88587f87796be48b034cb758ed4593 to your computer and use it in GitHub Desktop.
Responsify existing tables on the page. Common-case: you're can't add class to table-element e.g. (in my case) when you're using SSG(Static-Site Generator) that produce *.html from *.md templates
"use strict";
let tables = document.querySelectorAll("table");
let copy = collection => Array.prototype.slice.call(collection);
tables = copy(tables);
tables.forEach(item => {
let thead = copy(item.tHead.rows[0].cells).map(item => item.textContent);
let tbody = copy(item.tBodies).forEach(
tbody => copy(tbody.rows).forEach(
row => copy(row.cells).forEach(
(cell, index) => cell.setAttribute('data-title', thead[index]))));
console.log(thead);
});
body {
font-size: 1.2em
}
@media only screen and (max-width: 480px) {
thead {
display: none;
}
table,
tbody,
table td,
table tr,
table th {
display: block;
}
table td,
table tr,
table th {
padding: 0;
text-align: left !important;
white-space: normal;
}
table tr {
border-bottom: 1px solid #eee;
padding-bottom: 11px;
margin-bottom: 11px;
}
table td {
border: none;
margin-bottom: 6px;
color: #444;
}
table td:empty {
display: none;
}
table td:first-child {
margin-bottom: 6px;
color: #333;
}
table td:first-child:before {
content: '';
}
table td:first-child {
margin-bottom: 6px;
color: #333;
}
table th[data-title]:before,
table td[data-title]:before {
content: attr(data-title) ":\00A0";
font-weight: bold;
}
table th:nth-child(1),
table td:nth-child(1) {
text-align: left;
padding-left: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Атрибут</th>
<th>Тип</th>
<th>Пояснення</th>
</tr>
</thead>
<tbody>
<tr>
<td>x1</td>
<td><code>&lt;coordinate&gt;</code></td>
<td>x-координата початкової точки.</td>
</tr>
<tr>
<td>y1</td>
<td><code>&lt;coordinate&gt;</code></td>
<td>y-координата початкової точки.</td>
</tr>
<tr>
<td>x2</td>
<td><code>&lt;coordinate&gt;</code></td>
<td>x-координата кiнцевої точки.</td>
</tr>
<tr>
<td>y2</td>
<td><code>&lt;coordinate&gt;</code></td>
<td>y-координата кiнцевої точки.</td>
</tr>
</tbody>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment