html multiple tables side by side - html

I have six tables side by side. Each table contains a top cell with a header and the next cell with varying amount of text. Each table is set to 30% width and has padding of 10px. The tables are enclosed in an overall <div> and each table is set float: left. On a laptop screen these will normally align 3 tables in the top of the screen and then 3 tables underneath.
[1][2][3]
[4][5][6]
However on some browsers or other devices, the text will occupy more space in table 1 making the table longer. This results in table 4 sorting under table 2 rather than under table 1, e.g.
[1][2][3]
[4][5]
[6]
Is there some way whereby I can get them to line up sequentially always taking up the left most space first?
Code example:
table,
td {
border: 1px solid black;
}
table {
float: left;
width: 30%;
margin: 10px;
}
<div>
<table>
<tbody>
<tr>
<td>Title 1</td>
<td>Text text text text text text text text Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 2</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 3</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 4</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 5</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 6</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>

Based on the link you've provided, this is not the place to be using tables, but rather div elements with css markup.
Some will say Flexbox is the way to go, and it may be.
An alternative is to use div elements that are set to display: table-cell, but those have their own issues, and wouldn't be my recommendation for this layout.
The solution I'm offering refactors your code to div elements, with some features that ensure it will always look good at various widths (allowing it to "flex" at responsive sizes).
See the Fiddle Here - stretch the widths, you'll see that the boxes widths are constrained, and they move up and down to adjust.
Here's the markup:
<div class="container">
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
<div>
<h2>Title</h2>
<img src="http://weavingtheworld.co.uk/images/slide01_small_brilliant-2.jpg">
<div class="text">
Body text goes here
</div>
</div>
</div>
There's one fundamental weakness to this layout, and that is that the height of each element is not automatic / guaranteed to be the same (you have the same flaw currently in your table layout, FYI). (Flexbox fixes that flaw). To overcome it, you have to do two things:
Be sure your images are all exactly the same proportions (not size, proportion).
Apply a fixed height to the text container.
And here's the commented CSS, with notes about why each (interesting) declaration is used.
.container > div {
/* block properties become available, but only takes the designated width */
display: inline-block;
/* this tells it to include padding & borders when calculating width */
box-sizing: border-box;
width: 30%;
padding: 15px;
/* min / max width ensure it's always within a "reasonable" width window.
This means at responsive it can / will reduce to 2 side-by-side,
and at larger sizes would increase to 3 or 4 side-by-side */
max-width: 250px;
min-width: 200px;
}
.container > div img {
/* ensure the image takes the full width of the space */
width: 100%;
/* this ensures the image is always proportioned properly */
height: auto;
}
.container div.text {
/* this is the weakness of this layout. To get all the boxes to be the same height, you have to set this to a fixed height. */
height: 200px;
}

use inline-block instead of float. I don't recommend changing the tables display property either as it might have unwanted affects of the layout but you could wrap the tables in a div and add the style to them.
table,
td {
border: 1px solid black;
}
table {
width: 100%;
}
div.wrap {
width: 30%;
display: inline-block;
margin: 10px;
}
<div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 1</td>
<td>Text text text text text text text text Text text text text text text text text Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 2</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 3</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 4</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 5</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
<div class="wrap">
<table>
<tbody>
<tr>
<td>Title 6</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>
</div>

What you need is vertical-align: top along with display: inline-block.
Here is a fiddle with an example: https://jsfiddle.net/ocv2y1t7/
PS: Make sure you don't apply any floats to your tables (or set float: none if inherited)

One solution the other answers haven't mentioned is to clear the floats every third table. Since the tables are 1/3 of the window in width, this will create "rows" of three tables each.
table,
td {
border: 1px solid black;
}
table {
float: left;
width: 30%;
margin: 10px;
}
table:nth-child(3n+1) {
clear:left;
}
<div>
<table>
<tbody>
<tr>
<td>Title 1</td>
<td>Text text text text text text text text Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 2</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 3</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 4</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 5</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Title 6</td>
<td>Text text text text text text text text</td>
</tr>
</tbody>
</table>
</div>

Related

table cell alignment with image and text

<table style="width:100%">
<tr>
<th>type</th>
<th>descritpion</th>
</tr>
<tr>
<td><p><img src="mail.ping" style="width: 30px;"></p>
<span>type-cedit ad=nd debit and shopping</td>
<td>description</td>
</tr>
</table>
The above image is show table data. when image and text aligned inside table cell.
This works fine when text is smaller. but when text size is large. text is appearing below image.
In above image i need to make the 'shopping' work should come jus below 'type' if length is large. Text should not appear below image. how can i fix this alignment issue
Applying display:inline-block or float:left you can align both
.divin{
display:inline-block;}
<table style="width:100%">
<tr>
<th>type</th>
<th>descritpion</th>
</tr>
<tr>
<td><p class="divin"><img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" style="width: 30px;"></p>
<span class="divin">type-cedit ad=nd debit and shopping</span></td>
<td>description</td>
</tr>
</table>

How to make CSS table-cell contents fill the height of the cell?

I have a div table layout like this:
...but I'd like the nested table on the right to match the height of the table on the left, so that the right table rows are equally spaced apart. It seems that there's some padding going on that's preventing the right table from filling the full height of it's container.
Want I want is this (it's a photoshop mock, but you get the idea):
I do not want to set a fixed height on the outer container. Both left and right table heights should match whichever one is tallest. I'm using a div table solution at the moment to contain the tables because it solves the problem whereby the table containers (light-green) height will match (I'm open to other possible solutions). However, it still leaves the problem of the shorter table not filling the height of it's container, as in the image.
Here's a fiddle.
The HTML:
<div class="outer-container">
<div class="inner-container">
<div class="child">
<table>
<tr>
<td>Label 1a</td>
<td>Value 1a</td>
</tr>
<tr>
<td>Label 1b</td>
<td>Value 1b</td>
</tr>
<tr>
<td>Label 1c</td>
<td>Value 1c</td>
</tr>
<tr>
<td>Label 1d</td>
<td>Value 1d</td>
</tr>
<tr>
<td>Label 1e</td>
<td>Value 1e</td>
</tr>
</table>
</div>
<div class="spacer"></div>
<div class="child">
<table>
<tr>
<td>Label 2a</td>
<td>Value 2a</td>
</tr>
<tr>
<td>Label 2b</td>
<td>Value 2b</td>
</tr>
<tr>
<td>Label 2c</td>
<td>Label 2c</td>
</tr>
</table>
</div>
</div>
</div>
and the styling:
.outer-container {
display: table;
padding: 10px;
background: #5f5f5f;
width: 400px;
margin-left: auto;
margin-right: auto;
}
.inner-container {
display: table-row;
padding: 10px;
margin-top: 50px;
width: 100%;
}
.child {
display: table-cell;
background: #d3e4d1;
border: 1px solid white;
}
.spacer {
display: table-cell;
width: 10%;
}
.child table {
width: 100%;
height: 100%;
padding: 10px;
}
.child td {
width: 50%;
}
.child td:first-child {
text-align: right;
padding-right: 10px;
}
IE8 and up solution required.
The table-cell contents were filling up the height of the cells, the tables (and table cells) were just different sizes because the contents were different and they weren't declared to be the same height.
Here's a very minimal working example: https://jsfiddle.net/egzs1sm3/
If you'd like you could remove the divs they're nested in and just apply the static height to the tables, I don't see why not.
Here's how to accomplish it via flexbox:
https://jsfiddle.net/tqvqsd2y/1/
display:flex on the container is inconsequential, that's just to make to make them be right next to eachother for comparison's sake. No reason you couldn't use that though, I think it's what you wanted.
Me again.
Do they have to be separate tables? Because two cells in the same table would automatically adjust the height and justify the content...
<table>
<tbody>
<tr>
<td>
<h3>
<span>asdf 1</span>
<span>asdf 2</span>
</h3>
<h3>
<span>asdf 1</span>
<span>asdf 2</span>
</h3>
</td>
<td>
<h3>
<span>asdf 1</span>
<span>asdf 2</span>
</h3>
<h3>
<span>asdf 1</span>
<span>asdf 2</span>
</h3>
<h3>
<span>asdf 1</span>
<span>asdf 2</span>
</h3>
</td>
</tr>
</tbody>
</table>
https://jsfiddle.net/7w963q7v/
Notice how different labels are now inline elements within block elements.
I have changed your existing HTML, that will work on IE8 too. Though I didn't tested on it, but I am sure, it won't deviate.
Option 1:
Using a cellspacing on the main table.
Please find the structure below:
<div class="outer-container">
<div class="inner-container">
<table cellspacing="15">
<tbody>
<tr>
<td class="child">
<table>
<!-- put your contents of the first table -->
</table>
</td>
<td class="child">
<table>
<!-- put your contents of the second table -->
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
Watch the demo here.
Option 2:
Without using the cellspacing.
<div class="outer-container">
<div class="inner-container">
<table> <!-- no 'cellspacing' here -->
<tbody>
<tr>
<td class="child">
<table>
<!--put your contents of the first table -->
</table>
</td>
<td class="spacer"></td> <!-- class spacer with 'width: 10%' -->
<td class="child">
<table>
<!--put your contents of the second table -->
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
Watch the demo here.
Update 1:
See the updated demo:
I don't think, the height of the nested table would be able to occupy the height of the largest table using only CSS. That's why I have made their parents as td, where it fills up the containers and doesn't depends upon the content size of its elements only, but the largest one out of all its siblings td's.
Anyways, one can always target the child elements of any other elements using either JS or CSS.

100% height in a td, positioning the text in the vertical middle

I am currently using EvoPDF (HTML to PDF) to build up a report in C#.
In this report we have a table, looking something like this in the fiddle:
http://jsfiddle.net/bk48srw1/3/
with the important part:
<tr>
<td>label for something</td>
<td style="position:relative;" rowspan="99999">
<div id="absolute">
<div class="centerd">
<span>value for all</span>
</div>
</div>
</td>
</tr>
The idea is that there are a variety of products, but they will have the same value in the second column, hence:
rowspan="99999"
The client wants the styling (border and background) to span the height of the second column, with the text in the middle. I have tried the position absolute, display:table, as well as vertical-align.
I cannot use a solution like Vertically align text in a span with fixed width and height
because I have no idea what the height would be (there could be an arbitrary number of tr's)
How do I make the div 100% height of the td, with the text displaying in the middle?
My preference is not to use JS, seeing that this code doesn't see a browser at all.
For styling purposes, I need to have about 3px padding inbetween the td and the div, seeing that I cannot add cellspacing to only 1 td.
Used to this change your html as like this
td{
width:50%;vertical-align:middle;
}
<table style="width:100% ">
<tr>
<td>
<table>
<tr><td>label for something</td></tr>
<tr>
<td>label for this:</td>
</tr>
<tr>
<td>label for this:</td>
</tr>
<tr>
<td>label for this:</td>
</tr>
<tr>
<td>label for this:</td>
</tr>
</table>
</td>
<td style="background:yellow; border: 1px solid black;text-align:center;">
<table width="100%">
<tr><td>value for all</td></tr>
</table>
</td>
</tr>
</table>
Here you go: http://jsfiddle.net/sjncndvn/2/
HTML markup:
<table style="width:100% ">
<tr>
<td>label for something</td>
<td style="position:relative;" rowspan="99999">
<div id="absolute">
<div class="centerd">
<span>value for all</span>
</div>
</div>
</td>
</tr>
<tr>
<td>label for this:</td>
<td></td>
</tr>
<tr>
<td>label for this:</td>
<td></td>
</tr>
<tr>
<td>label for this:</td>
<td></td>
</tr>
<tr>
<td>label for this:</td>
<td></td>
</tr>
</table>
CSS:
td{
width:50%;
display:table-cell ;
vertical-align:middle ;
}
#absolute {
width:100%;
background:yellow;
display:table;
border: 1px solid black;
}
.centerd {
display:table-cell;
vertical-align:middle;
text-align:center;
background-color:#pink;
}
Since #absolute no longer uses absolute positioning, you may want to change the ID name to something else.

Center text in td element based on 2. td element in previous tr element

I have the folowing:
<tr>
<td class="neighbor">
text
</td>
<td id="aim">
text
</td>
<td class="neighbor">
text text
</td>
</tr>
<tr>
<td id="text" colspan="3">
some long text
</td>
</tr>
How do I let the content of #text be centered based on #aim? note that .neighbor tds are not equal spaced.
Simply fill in the blank tags on either side of text instead of using a colspan. This will place the columns beneath each other.
css:
#aim, #text
{
text-align: center;
}
html:
<tr>
<td class="neighbor">
text
</td>
<td id="aim">
text
</td>
<td class="neighbor">
text text
</td>
</tr>
<tr>
<td>
</td>
<td id="text">
some long text
</td>
<td>
</td>
</tr>
If your goal is not to center the text in this way please provide a quick image of how it should look.

Why does text sink to the bottom of a table cell?

<table>
<tr>
<td>
random text here
<textarea value=""></textarea>
</td>
</tr>
</table>
In the browser the text is at the bottom. How do I make it center or to top? vertical align on TD doesn't seem to work.
Text is rendered along a line. Inline elements (such as the textarea) are also rendered on that line.
The text is at the bottom of the table cell because it sits on the same line that the textarea sits on and that textarea takes up the entire height of the cell.
You want to change the position of the textarea on that line, but you can do so:
textarea {
vertical-align: middle; /* other values are available */
}
gathcea:
You can put your text in a row above:
<table>
<tr>
<td>
<p>random text here</p>
</td>
</tr>
<tr>
<td>
<textarea value=""></textarea>
</td>
</tr>
</table>
Or use a line break to put the text area underneath it:
<table>
<tr>
<td>
<p>random text here</p>
<br />
<textarea value=""></textarea>
</td>
</tr>
</table>