Skip to content

Instantly share code, notes, and snippets.

@devilskitchen
Created August 5, 2012 11:24
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 devilskitchen/3264012 to your computer and use it in GitHub Desktop.
Save devilskitchen/3264012 to your computer and use it in GitHub Desktop.
A technique for altering HTML tables using CSS.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- IF PEN IS PRIVATE -->
<!-- <meta name="robots" content="noindex"> -->
<!-- END -->
<title>Table sizing and checkbox toggling &middot; CodePen</title>
<!--
Copyright (c) 2012 Chris Mounsey, http://codepen.io/devilskitchen
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.
-->
<style>
body {
font-family: sans-serif;
padding: 0 1em;
margin: 0;
font-size: 12px;
background: #eee;
}
.container {
margin: 0 auto;
max-width: 980px;
padding: 2em 1em 0.2em 1em;
background: white;
}
input {
position: absolute;
top: -9999px;
left: -9999px;
}
label {
cursor: pointer;
font-weight: normal;
}
label:after {
font-weight: bold;
}
.text-ellipsis {
white-space: nowrap;
overflow: hidden;
-o-text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
/* or "clip" */
}
.text-wrap {
word-wrap: break-word;
overflow-wrap: break-word;
}
.table-container {
margin: 1em auto;
padding: 0;
}
.table-container table {
margin: 1em auto;
table-layout: auto;
width: 100%;
border-collapse: collapse;
border-left: 1px solid #ddd;
}
.table-container tr {
background: white;
}
.table-container tr:nth-child(odd) {
background: #e3effa;
}
.table-container th {
background: #444;
color: #eee;
padding: 0.5em 1em;
text-shadow: 1px 1px 2px #000000;
border: 1px solid #111;
border-left: none;
}
.table-container th:first-child {
border-left: 1px solid #111;
}
.table-container td {
padding: 0.5em 1em;
border-bottom: 1px solid #ccc;
border-right: 1px solid #ccc;
}
.table-container td:first-child {
border-left: 1px solid #ccc;
}
/* Now apply certain controls using checkboxes */
input + label {
display: inline-block;
border: 1px solid #ddd;
padding: 0.2em 1em;
border-radius: 2px;
position: relative;
}
label[for="container-width"]:after {
content: " 100%";
}
label[for="table-layout"]:after {
content: " auto";
}
input[type=checkbox]#container-width:checked ~ .table-container {
width: 50%;
}
input[type=checkbox]#container-width:checked + label:after {
content: " 50%";
}
input[type=checkbox]#table-layout:checked ~ .table-container table {
table-layout: fixed;
}
input[type=checkbox]#table-layout:checked + label:after {
content: " fixed";
}
input[type=checkbox].remove-content + label {
padding: 0.2em 0.5em;
font-weight: bold;
}
input[type=checkbox].remove-content:checked + label {
font-weight: normal;
}
input[type=checkbox].remove-content#remove-col1 + label {
margin-left: 9em;
}
input[type=checkbox].remove-content#remove-col1 + label:before {
content: "Remove column: ";
position: absolute;
left: -8.1em;
font-weight: normal;
}
input[type=checkbox].remove-content#remove-col1:checked ~ .table-container table th:nth-child(1),
input[type=checkbox].remove-content#remove-col1:checked ~ .table-container table td:nth-child(1) {
display: none;
}
input[type=checkbox].remove-content#remove-col2:checked ~ .table-container table th:nth-child(2),
input[type=checkbox].remove-content#remove-col2:checked ~ .table-container table td:nth-child(2) {
display: none;
}
input[type=checkbox].remove-content#remove-col3:checked ~ .table-container table th:nth-child(3),
input[type=checkbox].remove-content#remove-col3:checked ~ .table-container table td:nth-child(3) {
display: none;
}
input[type=checkbox].remove-content#remove-col4:checked ~ .table-container table th:nth-child(4),
input[type=checkbox].remove-content#remove-col4:checked ~ .table-container table td:nth-child(4) {
display: none;
}
input[type=checkbox].remove-content#remove-col5:checked ~ .table-container table th:nth-child(5),
input[type=checkbox].remove-content#remove-col5:checked ~ .table-container table td:nth-child(5) {
display: none;
}
input[type=radio]:checked + label {
background: #eee;
border-color: #999;
}
input[type=radio]#text-break:checked ~ table td {
word-wrap: break-word;
overflow-wrap: break-word;
}
input[type=radio]#text-ellipsis:checked ~ table td {
white-space: nowrap;
overflow: hidden;
-o-text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
/* or "clip" */
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
border-image: none !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: normal !important;
word-spacing: 0 !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
-o-transform: none !important;
transform: none !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
text-shadow: none !important;
border: 0 !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script>
// Kill alerts, confirmations and prompts
// window.alert = function(){}; we're going to allow alerts for now
window.confirm = function(){};
window.prompt = function(){};
window.open = function(){};
window.print = function(){};
</script>
</head>
<body>
<div class="container">
<!-- Change table-container width -->
<input type="checkbox" id="container-width">
<label for="container-width">Container width:</label>
<!-- Change table layout -->
<input type="checkbox" id="table-layout">
<label for="table-layout">Table layout:</label>
<!-- Remove optional columns -->
<input type="checkbox" id="remove-col1" class="remove-content">
<label for="remove-col1">1</label>
<input type="checkbox" id="remove-col2" class="remove-content">
<label for="remove-col2">2</label>
<input type="checkbox" id="remove-col3" class="remove-content">
<label for="remove-col3">3</label>
<input type="checkbox" id="remove-col4" class="remove-content">
<label for="remove-col4">4</label>
<input type="checkbox" id="remove-col5" class="remove-content">
<label for="remove-col5">5</label>
<!-- Begin table container -->
<div class="table-container">
<!-- Normal text (default) -->
<input type="radio" name="text-option" id="text-normal" checked>
<label for="text-normal">Normal</label>
<!-- Break words -->
<input type="radio" name="text-option" id="text-break">
<label for="text-break">Break word</label>
<!-- Force ellipsis -->
<input type="radio" name="text-option" id="text-ellipsis">
<label for="text-ellipsis">Ellipsis</label>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
</tr>
<tr>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>forename.surname@reallylongdomain.com</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
</tr>
<tr>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>forename.surname@prettylongdomain.com</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
</tr>
</table>
</div>
</div>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" target="_blank">Report Abuse</a>
&nbsp;
<a href="/devilskitchen/pen/xEaBp" target="_blank">Edit this Pen</a>
</div>
</body>
</html>
<div class="container">
<!-- Change table-container width -->
<input type="checkbox" id="container-width">
<label for="container-width">Container width:</label>
<!-- Change table layout -->
<input type="checkbox" id="table-layout">
<label for="table-layout">Table layout:</label>
<!-- Remove optional columns -->
<input type="checkbox" id="remove-col1" class="remove-content">
<label for="remove-col1">1</label>
<input type="checkbox" id="remove-col2" class="remove-content">
<label for="remove-col2">2</label>
<input type="checkbox" id="remove-col3" class="remove-content">
<label for="remove-col3">3</label>
<input type="checkbox" id="remove-col4" class="remove-content">
<label for="remove-col4">4</label>
<input type="checkbox" id="remove-col5" class="remove-content">
<label for="remove-col5">5</label>
<!-- Begin table container -->
<div class="table-container">
<!-- Normal text (default) -->
<input type="radio" name="text-option" id="text-normal" checked>
<label for="text-normal">Normal</label>
<!-- Break words -->
<input type="radio" name="text-option" id="text-break">
<label for="text-break">Break word</label>
<!-- Force ellipsis -->
<input type="radio" name="text-option" id="text-ellipsis">
<label for="text-ellipsis">Ellipsis</label>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
</tr>
<tr>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>forename.surname@reallylongdomain.com</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
</tr>
<tr>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>forename.surname@prettylongdomain.com</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
<td>Here's a whole load of content, so that I can decently attempt to judge how the table reflows when certain properties are applied.</td>
</tr>
</table>
</div>
</div>
body {
font-family:sans-serif;
padding:0 1em;
margin:0;
font-size:12px;
background:#eee;
}
.container {
margin:0 auto;
max-width:980px;
padding:2em 1em 0.2em 1em;
background:white;
}
input {
position: absolute;
top: -9999px;
left: -9999px;
}
label {
cursor:pointer;
font-weight:normal;
&:after { font-weight:bold; }
}
.text-ellipsis {
white-space: nowrap;
overflow: hidden;
-o-text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis; /* or "clip" */
}
.text-wrap {
word-wrap:break-word;
overflow-wrap:break-word;
}
.table-container {
margin:1em auto;
padding:0;
table {
margin:1em auto;
table-layout:auto;
width:100%;
border-collapse:collapse;
border-left:1px solid #ddd;
}
tr {
background:white;
&:nth-child(odd) {
background:#e3effa;
}
}
th {
background:#444;
color:#eee;
padding:0.5em 1em;
text-shadow:1px 1px 2px rgba(0,0,0,1);
border:1px solid #111;
border-left:none;
&:first-child {
border-left:1px solid #111;
}
}
td {
padding:0.5em 1em;
border-bottom:1px solid #ccc;
border-right:1px solid #ccc;
&:first-child {
border-left:1px solid #ccc;
}
}
}
/* Now apply certain controls using checkboxes */
input {
+ label {
display:inline-block;
border:1px solid #ddd;
padding:0.2em 1em;
border-radius:2px;
position:relative;
}
}
label[for="container-width"]:after {
content:" 100%";
}
label[for="table-layout"]:after {
content:" auto";
}
input[type=checkbox]#container-width:checked {
~ .table-container { width:50%; }
+ label:after {
content:" 50%";
}
}
input[type=checkbox]#table-layout:checked {
~ .table-container table {
table-layout:fixed;
}
+ label:after {
content:" fixed";
}
}
input[type=checkbox].remove-content {
+ label {
padding:0.2em 0.5em;
font-weight:bold;
}
&:checked + label { font-weight:normal; }
&#remove-col1 {
+ label {
margin-left:9em;
&:before {
content:"Remove column: ";
position:absolute;
left:-8.1em;
font-weight:normal;
}
}
&:checked ~ .table-container table {
th,td {
&:nth-child(1) {
display:none;
}
}
}
}
&#remove-col2:checked ~ .table-container table {
th,td {
&:nth-child(2) {
display:none;
}
}
}
&#remove-col3:checked ~ .table-container table {
th,td {
&:nth-child(3) {
display:none;
}
}
}
&#remove-col4:checked ~ .table-container table {
th,td {
&:nth-child(4) {
display:none;
}
}
}
&#remove-col5:checked ~ .table-container table {
th,td {
&:nth-child(5) {
display:none;
}
}
}
}
input[type=radio] {
+ label {}
}
input[type=radio]:checked {
+ label {
background:#eee;
border-color:#999;
}
}
input[type=radio]#text-break:checked {
~ table td { .text-wrap; }
}
input[type=radio]#text-ellipsis:checked {
~ table td { .text-ellipsis; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment