Table Colspan not working when Table tbody set to Display:block; - html

I have a table with three sections, thead, tbody, and tfoot.
I wanted to have the tbody style set to display:block; so it would have scrollbars when it got to a certain height. But I noticed when I set the tbody style to display:block;, the colspans are not working in the table, as if the table is disregarding it. Is there something I am doing wrong, or is this a css3 bug?
Please refer to my example HTML & CSS for the table below (with JSfiddle)
Here is an image of my problem:
Here is what I'm trying to accomplish:
JSFIDDLE
http://jsfiddle.net/fT3EC/3/
TABLE:
<table>
<thead>
<tr>
<td class="name" colspan="2">Name</td>
<td class="price">Price</td>
</tr>
</thead>
<tbody>
<tr>
<td class="name" colspan="2">Product 1</td>
<td class="price">$100.00</td>
<td class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
</tr>
<tr>
<td class="name" colspan="2">Product 2</td>
<td class="price">$50.00</td>
<td class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
</tr>
<tr>
<td class="name" colspan="2">Product 3</td>
<td class="price">$10.00</td>
<td class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2"><b>Sub-Total:</b></td>
<td>$160.00</td>
</tr>
<tr>
<td colspan="2"><b>Free Shipping:</b></td>
<td>$0.00</td>
</tr>
<tr>
<td colspan="2"><b>Total:</b></td>
<td>$160.00</td>
</tr>
</tfoot>
</table>
CSS:
table {width:500px;}
tr,td {padding:5px; border:1px solid;}
.price {width:100px;}
.info {width:200px;}
tbody {
display:block; /* this is what causes the problem */
max-height:200px;
overflow-y:scroll;
}

defaut display of tbody is table-row-group, if you change it or set tbody in absolute or fixed position, it will not be part of the table-layout anymore.
In the flow with a reset on display, it will have the space of a first cell left. Browser will still try to keep coherent the layout. and will leave a gap for the missing cells.
You can try some work around, like using table-layout on table, so you can set you tbody block on 100% width. DEMO
tbody tr can optionnaly be set as display:table.
Anyhow, tbody will be off the flow of it's parent table, and columns will not be sized the same in tbody and theader
and with 3rd col overflowing
some cloning with jquery to avoid splitting table ?

Try splitting your table into three tables
One for the header
One for the body
One for the Footer
Then put a div around the body table which will allow you to control things like overflow: scroll; correctly

Forget the hacks, just don't use a table. Create a grid with divs, then you can have the behavior you want and it can be styled to look exactly the way you want.

Related

Make paragraph only use as much space as table in same container

I have a paragraph and a table. The table should only be as wide as its content. Usually the table cells only have short text in it so it won't span the whole page. The table is horizontally centered and so should be the paragraph. The paragraph should only be as wide as the table below it, horizontally centered and left aligned.
I'm able to get the table and the paragraph centered on the page, but the text always uses the whole width. How can I fix that?
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
<table>
<tr>
<td>Project 1234</td>
<td>#flowers #bread</td>
<td>First round</td>
</tr>
<tr>
<td>Project 1234</td>
<td>#flowers #bread</td>
<td>First round</td>
</tr>
<tr>
<td>Project 1234</td>
<td>#flowers #bread</td>
<td>First round</td>
</tr>
</table>
</div>
div{
width: 100%;
}
p {
text-align: center;
}
table {
margin: 0 auto;
width: auto;
}

Move button up without affecting css of above tr elements in a table

I am trying to create an email template so I doing the thing in table only and inline CSS. Problem I am facing with the styling is that I am not able to move the button 40px down the text You are a .... some task. without affecting the above 2 <tr>s. I applied height to tr via inline css but somehow it is not taking the height. I also tried setting margin, padding to the last tr, td but no luck.
Let me know how could I achieve this without affecting the heights of above 2 tr, tds or a better way to handle this.
Fiddle - https://jsfiddle.net/qd00shr3/
Code -
<body style="background: #2D2D2D; font-family: arial;">
<table width="600" border="0" cellspacing="0" cellpadding="0" align="center" style="background: green;height: 320px;">
<tr>
<td style="text-align: center;">
<img src="http://dummyimage.com/600x400/008000/fff.png" style="width:100px;height:100px;" />
</td>
</tr>
<tr>
<td style="text-align: center;">
<h2 style="margin-top: -55px;color: #ffffff;padding: 0 68px 0 60px;font-weight: normal;font-size: 32px;">You are a good developer waiting for some task.</h2>
</td>
</tr>
<tr style="height: 100px;">
<td style="text-align: center;">
<button style="background: #ff744f; border:none; width: 210px; height: 45px; text-align:center;color: #ffffff;border-radius: 5px;">Check Task</button>
</td>
</tr>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="0" align="center" style="background: #ffffff;height: 134px;">
<tr>
<td>
<p style="font-size: 11px;color: #16325c; text-align: justify;padding: 0 30px;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. anim id est laborum.
</td>
</tr>
</table>
</body>
I am not sure that I understand correct... This will move your button up:
position:relative;top:-40px;

HTML/CSS Splitting container into 3 parts

I want to split container into three sections and it's already done but I can't figure out how to align all these sections vertically.
HTML:
<table width="100%">
<tbody>
<tr>
<td class="description">
<div>
<section class="col">
<h4>Description1</h4>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</section>
<section class="col">
<h4>Description2</h4>
<div>blablalblablablalbla</div>
</section>
<section class="col">
<h4>Description3</h4>
<div>bl
</section>
</div>
</td>
</tr>
</tbody>
CSS:
.description {
background: lightgrey;
}
.description .col {
display: inline-block;
width: 30%;
}
https://jsfiddle.net/kg4xao6m/
Use vertical-align: top in your CSS for the inline-block elements.
.description .col {
display: inline-block;
width: 30%;
vertical-align: top;
}
JSFiddle
I agree with #David answer that is the way to align the sections vertically but you are also using a table that clearly has two <tr> with three <td> rather than one of each.
Semantically this is more appropriate:
table {
background: lightgrey;
}
td {
width: 30%;
vertical-align: top;
}
<table width="100%">
<tbody>
<tr>
<td>
<h4>Description1</h4>
</td>
<td>
<h4>Description2</h4>
</td>
<td>
<h4>Description3</h4>
</td>
</tr>
<tr>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td>
<td>blablalblablablalbla</td>
<td>bl</td>
</tr>
</tbody>
</table>

Removing cell padding (email purposes)

I'm asking why there's 1px of padding in the cells in the following code:
<table style="border: 1px solid #B0B0B0; width: 900px; height: 196px;border-collapse: collapse;">
<tr>
<td rowspan=2 style="vertical-align: top;">
<img src="http://ayudawp.com/wp-content/uploads/2008/08/imagen.jpg" alt="imagen" style="height:193px; width:285px;">
</td>
<td colspan=2 style="text-align: center;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna $
</td>
</tr>
<tr>
<td>
20e
</td>
<td>
-60 %
</td>
</tr>
</tr>
</table>
I want the image to be without top and left padding. And I can't use the padding attribute. Reading on CSS table specs, I read that extra padding is added if there are cells of different heights on a row, but even forcing each cell to have the same height doesn't fix that.
based on what you said
And I can't use the padding attribute
This seems to me that you are using this code for Email purposes.
So, here is a possible solution, by using the old cellpadding and cellspacing properties for table (plus I added display:blockto your img to fix the gap caused by img being an inline element)
<table cellpadding="0" cellspacing="0" style="border: 1px solid #B0B0B0; width: 900px; height: 196px;border-collapse: collapse;">
<tr>
<td rowspan="2" style="vertical-align: top;">
<img src="http://ayudawp.com/wp-content/uploads/2008/08/imagen.jpg" alt="imagen" style="height:193px; width:285px;display:block" />
</td>
<td colspan="2" style="text-align: center;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna $</td>
</tr>
<tr>
<td>20e</td>
<td>-60 %</td>
</tr>
</table>
If this is not for email purposes, do not use cellpadding nor cellspacing since they are deprecated, thus you have to use CSS atributes instead, like padding.
Browsers have an internal stylesheet which defines the default styles for that browser.
In my case, Firefox has (see it in the source code)
td {
padding: 1px;
}
You can remove it with
td {
padding: 0;
}
td {
padding: 0;
}
<table style="border: 1px solid #B0B0B0; width: 900px; height: 196px;border-collapse: collapse;">
<tr>
<td rowspan=2 style="vertical-align: top;">
<img src="http://ayudawp.com/wp-content/uploads/2008/08/imagen.jpg" alt="imagen" style="height:193px; width:285px;">
</td>
<td colspan=2 style="text-align: center;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna $
</td>
</tr>
<tr>
<td>
20e
</td>
<td>
-60 %
</td>
</tr>
</table>
It's a common practice to just add * { margin: 0; padding: 0;} at top of each css file instead of having to deal with overriding default padding and margin values everywhere.
http://jsfiddle.net/50cg5fyj/

How to align based on a middle dash (or other character) inside cells

How can I obtain in a simple way this format inside cells of a table?, I mean all dashes aligned, when I am using a proportional font.
example:
Note: There are more cells on each row, and table uses borders, so I think that using three consecutive cells without borders, is not an option
Well using a table is not exactly would I would recommend but keeping to the request of your posting you can try something like this...
<table id="myTable">
<tbody>
<tr>
<td>1</td>
<td>-</td>
<td>Lorem ipsum dolor sit amet</td>
</tr>
<tr>
<td>2039</td>
<td>-</td>
<td>consectetur adipisicing elit</td>
</tr>
<tr>
<td>49</td>
<td>-</td>
<td>sed do eiusmod tempor incididunt </td>
</tr>
<tr>
<td>560</td>
<td>-</td>
<td>ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud</td>
</tr>
</tbody>
And some small CSS
#myTable tr td:first-child{ text-align:right; }