Scaling table cell height - html

Firstly, this is a duplicate question: Equal-height scaling cells in an html table
The user that asked the question above didn't get a solution for the same issue I'm having.
Within the JSFIDDLE you will notice a cell with a red background. This cell is the highest and I need all other cells to pick up the highest cell height and span to the corresponding cell height.
The solution cannot contain fixed heights as this must be dynamic
Here is a mock up of what I'm trying to achieve: http://jsfiddle.net/pAz6G/
Here is HTML:
<table class="left" cellspacing="0" borderspacing="0" >
<tr>
<td>
<h2>Very Servere</h2>
<p>50m from high water on East Coast, 100m from high water on West Coast. Characterised by:</p>
<ul>
<li>Heavy salt deposits</li>
<li>The almost constant smell of saly spray in the air.</li>
<li>Close to breaking stuff (typically starts 50-100 metres) such as is found on exposed coasts.</li>
</ul>
<p>This environment may be extended inland by revailing winds and local coniditions</p>
</td>
</tr>
</table>
<table cellspacing="0" borderspacing="0" class="right">
<tr>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
<td class="section">
<span class="section-heading">Applications</span>
<table class="inner">
<tr>
<td>Roofing</td>
</tr>
<tr>
<td class="red">Rain washing plus manual washing every 3 months</td>
</tr>
<tr>
<td>Roofing</td>
</tr>
</table>
</td>
</tr>
</table>
Here is CSS:
/* Column Styling */
.left {
float: left;
width: 350px;
}
.left td {
padding: 10px;
}
.right {
float: left;
width: 400px;
}
/*********************************************/
/* General Styling */
.no-padding {
padding: 0;
}
td {
background: grey;
color: white;
vertical-align: top;
}
p {
margin: 0;
}
.red {
background: red;
}
/*********************************************/
/* Section Styling */
.section {
border-left: 1px solid #fff;
}
.section-heading {
display: block;
padding: 10px;
}
/*********************************************/
/* Nested Tables */
.inner {
width: 100%;
}
.inner td {
border-top: 1px solid #fff;
padding: 10px;
}
/*********************************************/

Instead of using multiple tables, try using one table.
Keeping it simple :)

Related

CSS: Sizing of elements in cells of a table with partial borders

What I Want
With pure HTML and CSS, to have a table where each cell either:
has a border
contains an element that is the same size as the full cell
such that there are no visible gaps between cells.
Visually, I have the left but need the right
What I've Tried
I've tried playing around with setting various combinations of box-sizing and padding to no avail.
You can see one of my attempts at this JSFiddle.
You have added padding:0 to just the coloured cells. As the first cell still have padding, it is increasing all the row height.
If you add:
.rows-index {
border: 1px solid black;
padding: 0;
box-sizing: border-box;
}
You will notice there's still a white 1px line which is not white, It is the space the border of the first cell is taking (as before, increasing the row hight). It does not matter if use border-box in this table. All cells will have same height but the coloured divs won't fill that gap.
I would suggest to use outline insteed of border as a solution:
table {
border-collapse: collapse;
border-spacing: 0;
}
.cols-index, .rows-index {
outline: 1px solid black;
padding: 0;
box-sizing: border-box;
}
.table-cell {
padding: 0;
box-sizing: border-box;
}
.table-cell div {
width: 100%;
height: 100%;
display: inline-block;
box-sizing: inherit;
}
.purple { background-color: purple; }
.red { background-color: red; }
.lightblue { background-color: lightblue; }
<table xmlns="http://www.w3.org/1999/html">
<thead>
<tr>
<th class="top-left-cell"></th>
<th class="cols-index">Bish</th>
<th class="cols-index">Bash</th>
<th class="cols-index">Bosh</th>
</tr>
</thead>
<tbody>
<tr>
<td class="rows-index">First</td>
<td class="table-cell"><div class="purple">A</div></td>
<td class="table-cell"><div class="purple">B</div></td>
<td class="table-cell"><div class="purple">C</div></td>
<tr>
<tr>
<td class="rows-index">Second</td>
<td class="table-cell"><div class="red">D</div></td>
<td class="table-cell"><div class="red">E</div></td>
<td class="table-cell"><div class="red">F</div></td>
<tr>
<tr>
<td class="rows-index">Third</td>
<td class="table-cell"><div class="lightblue">G</div></td>
<td class="table-cell"><div class="lightblue">H</div></td>
<td class="table-cell"><div class="lightblue">I</div></td>
<tr>
</tbody>
</table>
Edited: your height:100%; in the coloured divs is not doing anything as the parent does not have a fixed height. If you don't want to use outline you could set the height of this divs to 19px which is the height of the first cell... the cell that set the height of your rows. However this "solution" is ugly as hell and won't work if anytime any cell has 2 lines of text.
Edited... again: or third option, using border would be to set a line-height to your coloured divs. This is much cleaner than the previous edited paragraph and it will work as a kind of padding: https://jsfiddle.net/654gu2sf/
You can 'extend' the cells' coloring a little with padding and border to fill up the blank spaces (which can be seen to correspond to the borders in the first column).
An adjusment has to be made in the first row in the table body which is not given a top 'extension'.
table * {
padding: 0;
margin: 0;
box-sizing: border-box;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
.cols-index,
.rows-index {
border: 1px solid black;
}
.table-cell {
padding: 0;
box-sizing: border-box;
}
.table-cell div {
width: 100%;
height: 100%;
display: inline-block;
box-sizing: inherit;
}
.table-cell div {
padding: 1px;
background-color: var(--col);
border-top: var(--col) 1px solid;
border-bottom: var(--col) 1px solid;
}
.purple {
--col: purple;
}
.red {
--col: red
}
.lightblue {
--col: lightblue;
}
tbody tr:first-child td>div {
border-top-width: 0px;
}
td {
text-align: center;
}
.rows-index {
text-align: right;
}
<table xmlns="http://www.w3.org/1999/html">
<thead>
<tr>
<th class="top-left-cell"></th>
<th class="cols-index">Bish</th>
<th class="cols-index">Bash</th>
<th class="cols-index">Bosh</th>
</tr>
</thead>
<tbody>
<tr>
<td class="rows-index">First</td>
<td class="table-cell">
<div class="purple">A</div>
</td>
<td class="table-cell">
<div class="purple">B</div>
</td>
<td class="table-cell">
<div class="purple">C</div>
</td>
</tr>
<tr>
<td class="rows-index">Second</td>
<td class="table-cell">
<div class="red">D</div>
</td>
<td class="table-cell">
<div class="red">E</div>
</td>
<td class="table-cell">
<div class="red">F</div>
</td>
</tr>
<tr>
<td class="rows-index">Third</td>
<td class="table-cell">
<div class="lightblue">G</div>
</td>
<td class="table-cell">
<div class="lightblue">H</div>
</td>
<td class="table-cell">
<div class="lightblue">I</div>
</td>
</tr>
</tbody>
</table>
You can add the cellspacing attribute to to table tag to adjust spacing between cells.
To remove the spacing, set cellspacing=0.
td, th {
border: 1px solid black;
}
<table cellspacing=0>
<tr>
<th></th>
<th>bish</th>
<th>bash</th>
<th>bosh</th>
</tr>
<tr>
<td>first</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td>second</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
<tr>
<td>third</td>
<td>A</td>
<td>A</td>
<td>A</td>
</tr>
</table>

Slanted Column Headers

I want to build a table with slanted column headers as shown in the image below.
But I am not able to align the slanted header div with actual column and the text is overflowing the column header. here is the link to my code.
My question is how do we align the slanted column header with the column below it and contain the text within it ?
Here is my code below
<table>
<tr>
<td>
<div class="outerDiv">
<div class="innerDiv">This is first column header </div>
</div>
</td>
<td>
<div class="outerDiv">
<div class="innerDiv">This is second column header</div>
</div>
</td>
<td>
<div class="outerDiv">
<div class="innerDiv">This is third column header</div>
</div>
</td>
</tr>
<tr> <td> 1 </td> <td> 2 </td> <td> 3 </td> </tr>
<tr> <td> 4 </td> <td> 5 </td> <td> 6 </td> </tr>
<tr> <td> 7 </td> <td> 8 </td> <td> 9 </td> </tr>
<tr> <td> 10 </td> <td> 11 </td> <td> 12 </td> </tr>
</table>
CSS:
.outerDiv {
background: grey;
height: 200px;
width: 100px;
border: 1px solid black;
transform: skew(-30deg);
}
body, html {
height: 100%;
}
.innerDiv{
transform:skew(45deg);
writing-mode: vertical-lr;
}
body {
display: flex;
justify-content: center;
}
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
}
Here's another attempt. I changed the header cells to th. I added a translateX to get it to line up better. For the inside dev I reversed skewed it so the text didn't look funny and then rotated the container so it was still at a slant following the container. Also added some positioning to get it to look right.
https://jsfiddle.net/b1mrksou/3/
The CSS I added:
* {
box-sixing: border-box;
}
.outerDiv {
background: grey;
height: 200px;
width: 100px;
border: 1px solid black;
border-bottom: 0;
border-left: 0;
transform: skew(-30deg) translateX(58%);
}
th:first-child .outerDiv {
border-left: 1px solid black;
position: relative;
}
.innerDiv {
position: absolute;
width: 250px;
height: 85px;
bottom: -34%;
left: 10px;
transform: skew(30deg) rotate(-60deg);
transform-origin: 0 0;
text-align: left;
}
This seems like the best solution I could come up with. http://jsbin.com/yecevosunu
<!doctype html><style>
table { border-collapse: collapse;}
td {border: 1px solid #000;}
</style><table class=tilttable>
<thead>
<tr>
<th>word</th>
<th>words and more and more words</th>
<th>two<br>words</th>
<th>word</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</tbody>
</table>
<script>
function toDegrees (angle) {return angle * (180 / Math.PI);}
function toRadians (angle) {return angle * (Math.PI / 180);}
onload=function(){
var tilttables = document.getElementsByClassName('tilttable');
[].forEach.call(tilttables[0].rows[0].cells
, function (i) {
i.style.verticalAlign = "bottom";
i.innerHTML = "<div><div>" + i.innerHTML + "</div></div>";
i.firstChild.firstChild.setAttribute('style',"transform-origin:top left;transform: rotate(-45deg);");
tilt(i.firstChild.firstChild);
});
}
function tilt(d){
var w = d.clientWidth;
var h = d.clientHeight;
d.parentElement.style.width = h*Math.cos(toRadians(45))+"px";
d.style.whiteSpace="nowrap";
d.parentElement.style.paddingTop = w*Math.sin(toRadians(45))+"px";
d.parentElement.style.height = h*Math.sin(toRadians(45))+"px";
}
</script>
you could use writing-mode, a pseudo element, transform and some tunning with margins:
table {
margin-right: 6em;
}
th {
padding: 0;
position: relative
}
th>div {
white-space:nowrap;
margin: -1em -2em -1em 6em;/* tune this to your needs, you might also see & update transform-origin */
padding: 0;
-webkit-writing-mode: vertical-lr;
/* old Win safari */
writing-mode: vertical-lr;
writing-mode: tb-lr;
transform: scale(-1, -1) rotate(45deg);
}
th:before {
position: absolute;
content: '';
top: 0;
left: 0;
right: 0;
bottom: 0;
border: solid;
transform: skew(-45deg);
transform-origin: bottom left;
}
td {
border: solid;
}
<table>
<tr>
<th>
<div class="innerDiv"> first </div>
</th>
<th>
<div class="innerDiv">This is second column header</div>
</th>
<th>
<div class="innerDiv">This is third header</div>
</th>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
</tr>
<tr>
<td> 4 </td>
<td> 5 </td>
<td> 6 </td>
</tr>
<tr>
<td> 7 </td>
<td> 8 </td>
<td> 999999999999999 </td>
</tr>
<tr>
<td> 10 </td>
<td> 11 </td>
<td> 12 </td>
</tr>
</table>
http://codepen.io/gc-nomade/pen/xqdNGN

Table: expand column to fit width, but keep row height to single line

I have a table that fills its container's width. It has three columns. Two columns have a fixed width while the third must expand to fill the remaining width. The width of the table is dynamic.
I can get this to work if a row's height may expand, but I need each row to only have a single line of text. If the text is too long then it must be cut off.
Code + examples: (https://jsfiddle.net/o044tq53/5/)
EDIT: I rewrote the above description and updated the jsfiddle with an example showing what it should look like.
#wrapper {
width:200px;
background:white;
}
table {
border-collapse:collapse;
text-align: left;
width:100%;
margin-bottom:30px;
}
div {
overflow:hidden;
white-space: nowrap;
text-align:left;
}
.td_col_two {
width:100%;
}
.col_one_div {
width:90px;
background: red;
}
.col_two_div {
background: lightblue;
overflow:hidden;
}
.col_three_div {
width:20px;
background: lightgreen;
}
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<div class = "col_one_div">
Robin
</div>
</td>
<td class = "td_col_two">
<div class = "col_two_div">
nowrap does not work for long text.
</div>
</td>
<td>
<div class = "col_three_div">
x
</div>
</td>
</tr>
</table>
Consider using display:flex and flex:1(flex-grow:1)
check this snippet
#wrapper {
background: white;
display: flex;
flex-direction: column;
border:1px solid black;
}
#wrapper table {
flex: 1;
width: 100%;
}
table,
tr,
td {
border: 1px solid;
}
#table-two div {
word-wrap:break-all;
}
.td_col_two {
width: 100%;
}
.col_one_div {
background: red;
}
.col_two_div {
background: lightblue;
}
.col_three_div {
width: 20px;
background: lightgreen;
}
<div id="wrapper">
<table id="table-one">
<tr>
<td>
<div class="col_one_div">
Robin
</div>
</td>
<td class="td_col_two">
<div class="col_two_div">
Short works
</div>
</td>
<td>
<div class="col_three_div">
x
</div>
</td>
</tr>
</table>
<table id="table-two">
<tr>
<td>
<div class="col_one_div">
Robin
</div>
</td>
<td class="td_col_two">
<div class="col_two_div">
White-space normal also works.
</div>
</td>
<td>
<div class="col_three_div">
x
</div>
</td>
</tr>
</table>
<table id="table-three">
<tr>
<td>
<div class="col_one_div">
Robin
</div>
</td>
<td class="td_col_two">
<div class="col_two_div">
nowrap does not work for long text.
</div>
</td>
<td>
<div class="col_three_div">
x
</div>
</td>
</tr>
</table>
</div>
Hope this helps

How do I display tables side by side [duplicate]

This question already has answers here:
HTML -- two tables side by side [duplicate]
(4 answers)
Closed 6 years ago.
This is how the display currently looks:
This is how I want it to look:
.d1{
background:#F0F0F0;
border: 1px solid #A4A4A4;
}
#designs input, #itemz input{
height:19px;
font-size: 15px;
}
#designs #fds_image {
background-size: 190px 221px;
height: 221px;
width: 190px;
overflow:hidden;
}
#designs #fds_image img{
width: 190px;
}
<table id="designs" width="auto" align="center" border="0" bgcolor="#EBEBEB" cellspacing="5">
<tbody>
<tr>
<td class="d1" name="item">
<div id="fds_image">
<button class="preview_switch">M</button>
</div>
<hr>
<div class="bottom_bar">
<button name="preview" data-original="m">Preview</button>
<br>
<tbody>
<td class="d1" name="item">
<div id="fds_image">
<button class="preview_switch">M</button>
</div>
</div>
<hr>
<div class="bottom_bar">
<button name="preview" data-original="m">Preview</button>
I tried to do many things and researched online, but for some reason, it is not working. How can I make the tables display side-by-side, like I've shown?
You can override the default display: table to inline-table.
table {
display: inline-table;
}
Example of horizontally align multiple <table> elements.
body {
text-align: center;
}
table {
width: 100px;
height: 200px;
border-collapse: collapse;
display: inline-table;
}
td {
border: 1px solid grey;
}
tr:first-child {
height: 100%;
}
<table>
<tbody>
<tr>
<td>Content</td>
</tr>
<tr>
<td><button>Button</button></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Content</td>
</tr>
<tr>
<td><button>Button</button></td>
</tr>
</tbody>
</table>
You could try floating the tables.
table {
float: left;
}
You can make a bigger table that holds both of your tables currently side by side.
<table style="margin: auto;">
<tbody>
<tr>
<td><!-- Your first table --></td>
<td><!-- Your second table --></td>
</tr>
</tbody>
</table>

Changing layout in table row using CSS only

I have a table using jQuery datatables with some rows in it.
Per this example, the table could look something like this:
https://jsfiddle.net/ef42942g/
I want to use CSS to change the way the table rows are shown so the final result will look something like this: https://jsfiddle.net/hamx5tas/ (please ignore the actual code)
It's very important to keep the basic table structure. I want that the change will be made entirely using CSS (if at all possible).
I have tried various changes to the tr's and td's css but with not much luck.
<table cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
</tbody>
</table>
I will appreciate any help that can be handed.
Thanks in advance.
You can adjust the layout using CSS flexbox. No changes necessary to the HTML.
CSS
thead > tr { display: none; }
tbody { display: flex; }
tbody > tr {
display: flex;
flex-direction: column;
align-items: center;
margin: 5px;
border: 1px solid black;
}
tbody > tr > td {
display: flex;
justify-content: center;
align-items: flex-start;
padding: 5px;
}
tbody > tr > td img {
width: 100px;
height: 65px;
}
tbody > tr > td:last-child {
flex-direction: row;
}
tbody > tr > td:last-child button {
margin: 5px;
}
Revised Demo
Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, use Autoprefixer. More browser compatibility details in this answer.
You can achieve what you want with pure CSS, changing the display property.
In this way, let's say that the table will not behave like a table anymore.
After that, you just have to adjust minor details to achieve your goal.
CSS:
tr {
width: 120px;
display: inline-block;
padding: 5px;
border: 1px solid black;
margin-right: 20px;
}
td {
padding: 5px;
display: block;
text-align: center;
}
Check it here: JSFiddle.
Hope it helps!
Setting the display on the td elements to block, and the display on the tr elements to inline-block gets you most of the way there.
td { padding: 5px;
border-bottom: none;
border-right:1px solid black;
display:block;}
tr{
display:inline-block;
}
thead{
display: none;
}
It sounds like you have some specific requirements for the format, so I won't go into further detail on assumptions, but from here you should be able to get what you want by turning borders on and off where needed.
https://jsfiddle.net/ef42942g/3/
I saw that Op wanted the structure unaltered so I had to go back to the drawing board. The following are the CSS changes:
Assigned display: none; to <thead>
Made the 3 tr into display: inline-table
Made all of the td to tr
Made the .main table 90vw which...
...made it easy for me to get the 3 tables symmetrical...
...some margins and padding...
...compared to the model we were provided, my table are 2px further apart, 10px wider, and 20px taller.
Fiddle
Snippet
thead {
display: none;
}
table {
width: 90vw;
}
tr {
display: inline-table;
outline: 1px solid black;
margin: 10px 5px;
}
td {
padding: 10px 15px;
display: table-row;
height: 30px;
text-align: center;
}
img {
width: 120px;
margin: 10px 15px;
}
<table cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
<tr>
<td>
<img src="http://www.cmacgm-marcopolo.com/wp-content/uploads/2013/01/vignette-cargo-carnet-pratique-zeebrugge.jpg" width="100">
</td>
<td>Title 1</td>
<td>100$</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
</tbody>
</table>