How to set spacing after a particular row in html table? - html

I have a table and I want to style spacing for every row in the table. I want the gap after the first row to be small (for example 1px) and 20px after the second row. Margin and padding does not seem to work. I also tried cellspacing = 20 but it sets spacing for every cell and I want to style gaps after the rows. How can I achieve that?
This snippet is what I currently have and what I tried :)
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table cellspacing="20">
<tr style = "margin-bottom: 1px; padding-bottom: 1px">
<th style = "padding-bottom: 1px">Month</th>
<th style = "padding-bottom: 1px">Savings</th>
</tr>
<tr>
<td style = "margin-bottom: 20px">January</td>
<td style = "margin-bottom: 20px">$100</td>
</tr>
<tr style = "border-collapse: separate; border-spacing: 1px">
<td>February</td>
<td>$20</td>
</tr>
<tr>
<td>June</td>
<td>$50</td>
</tr>
</table>
</body>
</html>

padding is not allowed on table row so you can apply padding to td or you can also add div inside td and give div tag margin bottom.
here, I have taken table inside td and gave margin to table
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<td colspan="2" style="border:0px;">
<table style="margin-bottom:10px;border:0px;">
<tr>
<td>Month</td>
<td>Savings</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" style="border:0px;">
<table style="width:100%;margin-bottom:10px;border:0px;">
<tr>
<td >January</td>
<td>$100</td>
</tr>
</table>
</td>
</tr>
<tr style = "border-collapse: separate; border-spacing: 20px">
<td>February</td>
<td>$20</td>
</tr>
<tr>
<td>June</td>
<td>$50</td>
</tr>
</table>
</body>
</html>

I want the gap after the first row to be small (for example 1px) and
20px after the second row.
You can achieve it by using padding on the td, and with pseudo selectors.
Something like this:
table tr td {padding-top: 20px;}
table tr:nth-of-type(2) td {padding-top: 1px;}
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$20</td>
</tr>
<tr>
<td>June</td>
<td>$50</td>
</tr>
</table>

Related

Wrapping table data without table-layout?

I need to wrap text within a <td> element, but I can't use the css table-layout property as the output is to html e-mail body and Outlook doesn't support the table-layout property.
Is there some other way to wrap text within a <td> element, or do I need to do the line breaking and measuring manually in code?
Here is an example of what I am trying to acheive:
td {
border: solid black 1pt;
font-family: arial;
font-size: 10pt
}
thead td{
text-align:center;
font-weight:bold;
}
table {
border-collapse: collapse
}
<html>
<body>
<table style="width:35pt;height:24pt;table-layout:fixed">
<thead>
<tr>
<td style="width:35pt;height:12pt">Good</td>
</tr>
</thead>
<tbody>
<tr>
<td style="width:35pt;height:12pt;word-wrap:break-word">Costingly Cost Cost</td>
</tr>
</tbody>
</table>
<div style="height:50pt"></div>
<table style="width:35pt;height:24pt;">
<thead>
<tr>
<td style="width:35pt;height:12pt">Bad</td>
</tr>
</thead>
<tbody>
<tr>
<td style="width:35pt;height:12pt" nowrap>Costingly Cost Cost</td>
</tr>
</tbody>
</table>
</body>
</html>
Use the Microsoft proprietary word-break:break-all;
<td style="word-break:break-all;">
That should fix things.
You can try this:
<tr>
<td nowrap>Never increase, beyond what is necessary, the number of entities required to explain anything</td>
<td>Never increase, beyond what is necessary, the number of entities required to explain anything</td>

Giving border to <tr> of table in a table

How do i give a border to the <tr>(t_border).
The inner table shouldnt inherit the style from the outer one
<table>
<tr>
<td>A</td>
</tr>
<tr>
<td>
<table class="t_border">
<tr>
<td>B</td>
</tr>
</td>
</tr>
</tbody>
You can try as per my code:
<style>
table{
border-collapse: collapse;
}
table.t_border tr{
border:solid 1px red;
}
</style>
<table>
<tr>
<td>A</td>
</tr>
<tr>
<td>
<table class="t_border">
<tr>
<td>B</td><td>B</td><td>B</td>
</tr>
</table>
</td>
</tr>
</table>
Hope it might help you!
you can set separate style for the class .t_border, see example code..
<table>
<tr>
<td>A</td>
</tr>
<tr>
<td>
<table class="t_border">
<tr>
<td>B</td>
</tr>
</td>
</tr>
</table></td></tr></table>
<style>
.t_border
{
border: 1px solid red;
}
</style>
just give style of tr tag
<tr>
<td style="border : 1px solid black">B</td>
</tr>
If you need to style your border, you may use other answers.
But if you only need to use the pure HTML table style, you can use this :
<table border=1> to give border on your table inside table.
You can't add a border to a <tr>. You would have to add it to the <td>s. Also your t_border table isn't be being closed </table> and your outer table should be closed with </table> instead of </tbody>.
Just giving the border to the tr won't really have an effect. It would be better to give the td's a border top or bottom of what you want it to actually show up. For example, border-bottom:2px solid black;

html5 compliant cellpadding in only some tables without editing td elements

Maybe I am being too picky. I want to put cell padding in some tables but not others, without editing every single td element. I would like to make it html5 compliant, which means not using the cellpadding property of the table. But I would like something equivalent to cellpadding - ie something I can apply to the properties of a whole table, on a table by table basis.
To make it even more complicated, I want collapsed borders, which I think rules out using the cell spacing property. Is there something tricky I can do there?
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
.cell-pad th,.cell-pad td{padding:10px}
</style>
</head>
<body>
<p>Table without cellpadding:</p>
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<p>Table with cellpadding:</p>
<table cellpadding="10">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
<p>Table with css:</p>
<table class="cell-pad">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
</body>
</html>
You can solve these problems using CSS.
table.table-big td {
padding: 10px;
}
table.table-collapse {
border-collapse: collapse;
}
table td {
border: 1px solid #333;
}
<table class="table-big table-collapse">
<tr>
<td>foo</td>
<td>bar</td>
</tr>
</table>
<table>
<tr>
<td>Hello</td>
<td>World</td>
</tr>
</table>
padding on the cell level can be used to create spacing between data and border.
border-collapse on the table can be used to collapse or separate borders.
You can create classes with these styles so you keep direct control on which table gets which styling. In my example the second table did not get any styling.

How to top, left justify text in a <td> cell that spans multiple rows

I have the following html code:
<table border="1">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td rowspan="2">Save a lot</td>
</tr>
<tr>
<td>March</td>
</tr>
</table>
Using CSS styling or another method, can I make the text "Save a lot" top, left justified?
td[rowspan] {
vertical-align: top;
text-align: left;
}
See: CSS attribute selectors.
<td rowspan="2" style="text-align:left;vertical-align:top;padding:0">Save a lot</td>
That should do it.
try this
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table style="width:50%;">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr style="height:100px">
<td valign="top">January</td>
<td valign="bottom">$100</td>
</tr>
</table>
<p><b>Note:</b> The valign attribute is not supported in HTML5. Use CSS instead.</p>
</body>
</html>
use valign="top" for td style

Remove column dividers in a html table

I have a html table with CSS. Currently all the cells have a white border around them, I am having trouble removing the column borders for each cell so the rows are divided by a white line. A similar table to what I'm trying to achive can be seen at http://www.smashingmagazine.com/2008/08/13/top-10-css-table-designs/, look at example 3 (the top table in this example). So far my code looks like:
<html>
<head>
<style type="text/css">
table, td, th
{
font-family:calibri;
border:collapse:collapse;
}
th
{
background-color:#b9c9fe;
color:#006add;
}
td
{
background-color:#e8edff;
color:#666699;
}
</style>
<body>
<table cellpadding="5" >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Looks like there's a slight error in the table style: it says border:collapse:collapse where it should be border-collapse: collapse;.
From there, you'll just need to add border-bottom: 1px solid #fff; to the table, th, td block, and you should be all set!