-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
331 lines (292 loc) · 12.7 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!-- DEMONSTRATION CODE - skip ahead for the actual code if you're here for looking at the implementation. -->
<head>
<!-- We increase the scale on mobile devices for demonstration purposes, because many mobile devices pixel density
is so high that you don't even see the table's overflow behavior on mobile. -->
<meta name="viewport" content="initial-scale=1.7, maximum-scale=1.7">
<title>demo.html</title>
</head>
<!--
Here we have some exemplary css for tables, which corresponds to the css for the minified tables we defined later.
You can also use any other table css, but make sure to properly modify the small-table-css to work well with your
large-table-css:
-->
<link rel="stylesheet" href="large-table-styling.css">
<!-- Quick explanation: -->
<p>
The idea of this page is to demonstrate a css & js trick that stops tables from overflowing on mobile, and displays them in a special non-overflowing styling instead.<br>
You can read more about this <a href="https://github.com/phseiff/tiny-tables">on the project's GitHub page</a>.<br/><br/>
If you are on mobile, you can switch to desktop view (probably somewhere in the hamburger menu) to see how the demo looks on desktop.<br/>
If you are on desktop, you can use Ctrl+Shift+I to get a console window, in which you can then choose to see the page in a mobile-like size.
</p>
<!-- Here we have some html for demonstration purposes: -->
<p>Demonstration table 1 (lots of text):</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill Jill </td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<p>Demonstration table 2 (some images):</p>
<table>
<tr>
<th>image 1</th>
<th>comment</th>
<th>image 2</th>
</tr>
<tr>
<td>
<img width="300px" src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Image-Omnidirectional_image_computer_lab.jpg/800px-Image-Omnidirectional_image_computer_lab.jpg">
</td>
<td>
foo bar
</td>
<td>
<img width="200px" src="https://c.pxhere.com/images/38/b3/1131303fe23a144034881294fa34-1566387.jpg!d">
</td>
</tr>
</table>
<!-- THE ACTUAL IMPLEMENTATION: -->
<!-- The following style is for when js is disabled, and for the time until the DOM and all images have loaded,
since the js implementation is defunct/ erring on the side of not applying the special style, respectively,
in these situations.-->
<style id="table-small-style-remove-if-js-activated">
@media screen and (max-width: 599px) {
table,
table th,
table td,
table tr {
all: initial;
display: block;
word-wrap: normal;
font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif;
color: rgba(0, 0, 0, 0.87);
}
table tr:not(:last-child) {
border-bottom: 1px solid #d1d5da
}
table tr:nth-child(2n) {
background-color: #f6f8fa;
}
table {
border: 1px solid #d1d5da;
max-width: 100%;
border-radius: 5px;
margin-bottom: 4px;
width: fit-content;
}
table img {
max-width: 100% !important
}
table td,
table th,
table tr {
padding-bottom: 4px;
padding-top: 4px;
border-collapse: collapse;
}
table tr {
padding-left: 4px;
padding-right: 4px
}
table th:nth-child(1),
table td:nth-child(1) {
padding-left: 0;
padding-bottom: 3px;
}
table th:nth-child(2),
table td:nth-child(2) {
padding-left: 40px;
padding-bottom: 3px;
}
table th:nth-child(3),
table td:nth-child(3) {
padding-left: 80px;
padding-bottom: 3px;
}
table th:nth-child(4),
table td:nth-child(4) {
padding-left: 120px;
padding-bottom: 3px;
}
table th:nth-child(5),
table td:nth-child(5) {
padding-left: 160px;
padding-bottom: 3px;
}
table th:nth-child(6),
table td:nth-child(6) {
padding-left: 200px;
padding-bottom: 3px;
}
table th:nth-child(7),
table td:nth-child(7) {
padding-left: 220px;
padding-bottom: 3px;
}
table th {
font-weight: 600;
}
}
</style>
<!-- This is where we store our js-generated styling: -->
<style id="remove-max-width-from-images-in-tables"></style>
<!-- The actual JS implementation: -->
<script>
/* Get all tables */
let allTablesInArticles = document.querySelectorAll("table");
/* Check whether an element is overflown */
function isOverflown(el) {
return el.clientWidth < el.scrollWidth;
}
/* The basis CSS for every table's min css: */
// css for one indention level:
let tableColumnWidthDescriptionCSS = "\n" +
" table.{stcn} th:nth-child({n}),\n" +
" table.{stcn} td:nth-child({n}) {padding-left: {px}px; padding-bottom: 3px;}\n";
// pre-rendered indention css for tables with less than 7 columns:
let basisTableColumnWidthDescriptionCSS = "\n" +
" table.{stcn} th:nth-child(1),\n" +
" table.{stcn} td:nth-child(1) {padding-left: 0; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(2),\n" +
" table.{stcn} td:nth-child(2) {padding-left: 40px; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(3),\n" +
" table.{stcn} td:nth-child(3) {padding-left: 80px; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(4),\n" +
" table.{stcn} td:nth-child(4) {padding-left: 120px; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(5),\n" +
" table.{stcn} td:nth-child(5) {padding-left: 160px; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(6),\n" +
" table.{stcn} td:nth-child(6) {padding-left: 200px; padding-bottom: 3px;}\n" +
" table.{stcn} th:nth-child(7),\n" +
" table.{stcn} td:nth-child(7) {padding-left: 220px; padding-bottom: 3px;}\n";
// complete css for a table:
let tableSmallCSS = "" +
" table.{stcn},\n" +
" table.{stcn} th,\n" +
" table.{stcn} td,\n" +
" table.{stcn} tr {\n" +
" all: initial;\n" +
" display: block;\n" +
" word-wrap: normal;\n" +
" border-collapse: collapse;\n" +
" font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif;\n" +
" color: rgba(0, 0, 0, 0.87);\n" +
" }\n" +
"\n" +
" table.{stcn} tr:not(:last-child) {border-bottom: 1px solid #d1d5da}\n" +
"\n" +
" table tr:nth-child(2n) {\n" +
" background-color: #f6f8fa;\n" +
" }" +
" table.{stcn} {border: 1px solid #d1d5da; border-radius: 5px; margin-bottom: 4px}\n" +
"\n" +
" table.{stcn} img {max-width: 100% !important}\n" +
" table.{stcn} td,\n" +
" table.{stcn} th,\n" +
" table.{stcn} tr {padding-bottom: 4px}\n\n" +
" table.{stcn} tr {padding-top: 4px; padding-left: 4px; padding-right: 4px}\n" +
"\n" +
" table.{stcn} {max-width: fit-content;}\n" +
"\n" +
"{tableColumnsWidthInformation}" +
" table.{stcn} th {font-weight: 600;}"
/* Create initial css for every table. */
function createInitialCSSForEveryTable() {
let css = "";
for (let i=0; i<allTablesInArticles.length; i++) {
let table = allTablesInArticles[i];
let table_small_class_name = "table-" + i + "-small"
let thisTablesCSS = tableSmallCSS;
let firstRowOfTheTable = table.getElementsByTagName("tr")[0];
let columnsOfFirstRowOfTable = firstRowOfTheTable.getElementsByTagName("th");
if (!columnsOfFirstRowOfTable)
columnsOfFirstRowOfTable = firstRowOfTheTable.getElementsByTagName("td");
let amountOfColumns = columnsOfFirstRowOfTable.length;
let tableColumnsWidthInformation;
if (amountOfColumns <= 4)
tableColumnsWidthInformation = basisTableColumnWidthDescriptionCSS;
else {
tableColumnsWidthInformation = "";
let maxIndention = 90;
for (let j=0; j<amountOfColumns; j++) {
tableColumnsWidthInformation += tableColumnWidthDescriptionCSS
.replaceAll("{n}", (j + 1).toString())
.replaceAll("{px}", (j * maxIndention / amountOfColumns).floor().toString())
}
}
thisTablesCSS = thisTablesCSS
.replaceAll("{tableColumnsWidthInformation}", tableColumnsWidthInformation)
.replaceAll("{stcn}", table_small_class_name);
css += thisTablesCSS;
}
/* Add the generated css to a style tag - source: https://stackoverflow.com/a/524721/9816158 */
head = document.head || document.getElementsByTagName('head')[0];
style = document.createElement('style');
style.type = 'text/css';
head.appendChild(style);
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
/* Iterate over all tables and give each of them their small-mode-css if they need scrolling. */
function resizeAllTablesIfWindowBecameToSmall() {
/* Make sure tables with one `max-width: 100%`-image in every row don't get squashed until the image is 2 pixels wide. */
/* (You might need to remove this or find another solution for this if you use max-width with different values
than 100% often): */
document.getElementById("remove-max-width-from-images-in-tables").innerText = "table img[max-width=100%] {" +
" max-width: none !important;" +
"}" +
"table img[width=100%] {" +
" width: none !important;" +
"}";
/* Actually change style for tables where needed: */
for (let i=0; i<allTablesInArticles.length; i++) {
let table = allTablesInArticles[i];
let table_small_class_name = "table-" + i + "-small"
if (isOverflown(table)) {
if (!table.classList.contains(table_small_class_name)) {
console.log("Added small style to table-" + i);
table.classList.add(table_small_class_name);
}
} else {
if (table.classList.contains(table_small_class_name)) {
table.classList.remove(table_small_class_name);
if (isOverflown(table)) {
/* We have to re-add the element because it overflows again. */
table.classList.add(table_small_class_name);
} else {
console.log("Removed small style from table-" + i);
}
}
}
}
/* Make sure tables with nothing but images don't get their max width again: */
document.getElementById("remove-max-width-from-images-in-tables").innerText = "";
}
/* Add event listeners to be executed once DOM has loaded: */
document.addEventListener('DOMContentLoaded', function() {
createInitialCSSForEveryTable();
resizeAllTablesIfWindowBecameToSmall();
});
window.addEventListener("load", function(){
/* Remove the offline style - but only after all images are loaded and we can use the more sophisticated method */
document.getElementById("table-small-style-remove-if-js-activated").remove();
/* For when a table contains images that make it overflow after our first check after DOMContentLoaded said it fits. */
resizeAllTablesIfWindowBecameToSmall();
});
window.addEventListener('resize', resizeAllTablesIfWindowBecameToSmall);
</script>
<p>See <a href="https://phseiff.com/tiny-tables/anti-demo">here</a> to see how this would behave without our css & js trick!</p>