Skip to content

Instantly share code, notes, and snippets.

@miloskroulik
Last active August 29, 2015 14:01
Show Gist options
  • Save miloskroulik/e1e70c8fb541864f7c60 to your computer and use it in GitHub Desktop.
Save miloskroulik/e1e70c8fb541864f7c60 to your computer and use it in GitHub Desktop.
Shows how to create multiple HTML tables, where first column (th cell) is of the same width.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<meta name=description content="">
<meta name=viewport content="width=device-width, initial-scale=1">
<title>Test zarovnaných tabulek</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<style type="text/css">
table {
table-layout:fixed;
border-collapse: collapse;
border-spacing: 0;
width: 100%;
margin-bottom: 20px;
}
table th, table td {
border-top: 1px solid #DDDDDD;
line-height: 1.42857;
padding: 8px;
vertical-align: top;
}
table td {
}
th {
text-align: left;
background-color: #DDDDDD;
}
th span {
white-space: nowrap;
display: inline-block;
}
</style>
</head>
<body>
<table>
<tr>
<th><span>Lorem ipsum dolor sit amet. Lorem ipsum dolor.</span></th>
<td><span>Lorem ipsum dolor sit amet.</span></td>
</tr>
<tr>
<th><span>Laudantium illo facilis itaque accusantium.</span></th>
<td><span>Totam, atque asperiores nihil incidunt!</span></td>
</tr>
<tr>
<th><span>Sapiente, vitae illo nam amet.</span></th>
<td><span>Distinctio, quos eum quasi voluptates!</span></td>
</tr>
</table>
<table>
<tr>
<th><span>Lorem ipsum dolor sit amet.</span></th>
<td><span>Lorem ipsum dolor sit amet.</span></td>
</tr>
<tr>
<th><span>Quibusdam, nemo dolor illo minima.</span></th>
<td><span>Sequi, commodi nulla cum ab.</span></td>
</tr>
<tr>
<th><span>Soluta ex voluptate error voluptatibus.</span></th>
<td><span>Temporibus sed hic dolorum ratione.</span></td>
</tr>
</table>
<table>
<tr>
<th><span>Lorem ipsum dolor sit amet.</span></th>
<td><span>Lorem ipsum dolor sit amet.</span></td>
</tr>
<tr>
<th><span>Quas, magnam consequatur quae praesentium.</span></th>
<td><span>Corporis, doloribus esse distinctio similique.</span></td>
</tr>
<tr>
<th><span>Facilis, id porro dolorem consequatur.</span></th>
<td><span>Dignissimos, mollitia itaque minus eveniet?</span></td>
</tr>
</table>
<script type="text/javascript">
var ThSpan = $("table tr > th > span");
function GetWidthOfWidestElement (element) {
var maxwidth=0;
$(element).each(function(){
if($(this).width() > maxwidth)
{
maxwidth = $(this).width();
}
});
return maxwidth;
}
$("table").find("tr > th").width(GetWidthOfWidestElement(ThSpan) + "px");
$( window ).resize(function() {
$("table").find("tr > th").width(GetWidthOfWidestElement(ThSpan) + "px");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment