Why isn't padding applied to table elements? - html

This is more of a "tell me why it doesn't work" instead of "help me fix it" question. If I try to apply padding to a thead or tr element within a table, it doesn't work. The only way padding works is if I apply it directly to the th or td element. Why is this so? Is there an easy way to apply padding to the entire thead or tr or is adding it to the th and td the only option?
<table>
<thead>
<tr>
<th>Destination</th>
<th>Size</th>
<th>Home Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>test 1</td>
<td>test 2</td>
<td>test 3</td>
</tr>
</tbody>
</table>
Notice the 10px of padding on the thead.
table {
width: 100%;
}
thead {
text-align: left;
background-color: yellow;
padding: 10px;
}
http://jsfiddle.net/5VQB7/

http://www.w3.org/TR/CSS2/box.html#propdef-padding
'padding'
Applies to: all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column

Try placing the padding in the th element instead. Typically you want to add padding to the th or td element, depending on the circumstance.
thead th {
padding: 10px;
}

Relevant part of CSS2.1: Tables
Please have a look at this diagram: table layers. padding can only be applied to table as a whole or th and td cells afaik. Not to forget caption also. Other layers are complicated enough in the various table layout algorithms not to have padding applied to them ^^
Here's a fiddle http://jsfiddle.net/QB97d/1/ showing other properties you can play with.
border-spacing: 8px 10px; is like a margin around each cell of a table. Get rid of it with border-collapse: collapse;
table-layout: fixed; will trigger a completely different algorithm ("render widths as I tell you to, don't care about the relative quantity of content in each cell anymore")
border is another way of giving space around elements, around padding
empty-cells: hide may trigger special behavior
Not shown in this fiddle:
playing with selectors to select the 4 corners of a table in IE9+ with a thead element and unknown type of cell in each corner (I'll let you find the 4 edges ;) ):
thead th:first-child, thead td:first-child,
thead th:last-child, thead td:last-child,
tbody:last-child tr:last-child th:first-child, tbody:last-child tr:last-child td:first-child
tbody:last-child tr:last-child th:last-child, tbody:last-child tr:last-child td:last-child
box-sizing: border-box (and its vendor prefixes) for calculating cell widths taking into account padding and border widths (like IE6 did in Quirks mode, oh irony...)

Padding between thead and tbody
Unfortunately, padding is not available for thead, tbody nor tr.
Nonetheless, more padding between thead and tbody can be achieved by selecting the data cells of the first row of the table body:
tbody tr:first-child td {
padding-top: 2.5ex;
}
Doing so, preserves the relative position of any header borders.

thead don't support css attribute "padding" if you need apply css in thead then css modify like :
thead tr th {
text-align: left;
background-color: yellow;
padding: 10px;
}
Or
th {
text-align: left;
background-color: yellow;
padding: 10px;
}

paddling left & right would always work for td or th as well.
But, in case of padding-top & padding-bottom, you are asking the td (or th) to increase it's height. Then what about the siblings?? What do you expect to happen??
Hence for padding top or bottom to work, you apply to it's parent which is the row of cells.

because tags like and they are not meant to populate the table, but to contain other elements.
lets make it clear : if you want to add header to your table, you wont insert it in , instead you will add it inside , and the same for , if you want to populate data inside a table, you will need to insert them inside
i hope this answer clarify your question

Related

Change the td padding for only one table, not effect style of other tables?

I have several tables and want to pad them differently. I've tried the following:
table#mapTable > tr > td{
padding-left: 4px;
padding-bottom: 20px !important;
}
for table with the id 'mapTable', but it hasn't been working (nothing happens). Same result, when trying 'margin-bottom' rather than 'padding-bottom'.
Is it possible to change <td> padding of only one table? I have a custom <td> style that I don't want to mess with for all the other tables.
For completeness, here's the table:
<table id='mapTable'>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>info</td><td>info</td></tr>
</table>
Thanks!
You are selecting the tds wrong. Do not use > just use spaces.
Spaces find elements that are children of the previous listed element.
table#mapTable tr td{
padding-left: 4px;
padding-bottom: 20px !important;
}
<table id='mapTable'>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>info</td><td>info</td></tr>
</table>
<table id='noStyle'>
<tr><td>stuff</td><td>stuff</td></tr>
<tr><td>info</td><td>info</td></tr>
</table>
There is no need to select table#mapTable tr td
It's enough to write
#mapTable td {
padding-left: 4px;
padding-bottom: 20px !important;
}
(= all <td> elements inside id "mapTable")
You got an answer already, just some more explanation:
table > tr won’t ever match table rows – because table rows never are children of a table.
They are children of a tbody, thead or tfoot element. The fact that there is no such element in your HTML code is not relevant here – in that situation, browsers create a tbody element implicitly when creating the DOM – they have to, because the specification says so.
You can easily verify that using your browser’s DOM inspector. Even for your minimal table example above, you’ll see that there is a tbody.

How to fit table rows that are too big for page display?

I have a table:
table.tablesorter {
border: 1px solid #D9D9D9;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
background-color: #04659D;
border-bottom: 1px solid #0F5E76;
border-left: 1px solid #0F5E76;
font-weight: bold;
text-align:left;
padding: 5px 19px 5px 9px;
color: #fff;
}
Some of the text in the table is too big and is being cut from the page, I have try using word-wrap:break-word; and setting width but nothing its working the text still overflows?
Any tips on how i can fix this?
Give your table element a fixed table-layout:
table.tablesorter {
...
table-layout: fixed;
}
From the CSS2.1 specification (linked above):
17.5.2.1 Fixed table layout
With this (fast) algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table's width, the width of the columns, and borders or cell spacing.
What is the markup of your table? Perhaps your table's width isn't wide or tall enough to compensate for the auto-adjusting nature of the table cell.
You may want to give this a try: I added the word inside a div and gave it a fixed width plus the word wrap, and it worked for me:
<div id="test">#DecimalFormat(total/counter)#</div>
#test {
word-wrap:break-word;
width:40px;
}
Here is your updated jsfiddle
EDITED: While my suggestion works perfectly in the above demo, if it doesn't work in your specific case, you may need to do some cleanup of your code and maybe use !important as a workaround (ie. word-wrap:break-word !important; width:40px !important;), or try adding display:block - that might help too.
You could add a scrollbar to the cells that have too long words.
just add the following:
table.tablesorter tbody td {
max-width: 250px;// you can use any width you like, the scrollbar will show up only when a cell exceeds the max-width.
overflow-x: auto;
}
another option would be to use white-space: normal, see: http://www.w3schools.com/cssref/pr_text_white-space.asp
for example
table.tablesorter tbody td {
white-space: normal;
}
If this does not work to wrap your text inside a cell, you should check your source and see if there is anything that overwrites your css and fix it.

How to align TH Header with TD in TBody

I'm having problems trying to embed a table in an existing HTML page with some CSS.
This CSS is hiding the header of the table by default with a style definition like:
.tablestuff thead {
display: none;
}
But I want the table to show, so I tried setting the style on the thead element with "display:block" (with javascript). That makes the header display, but the columns of the header don't line up with the td columns.
I have reduced my HTML to the following (hopefully with minimal typos) and showing the style on the thead element as set by javascript.
<div class="tablestuff">
<table border="1">
<thead style="display:block">
<tr>
<th id="th1" style="width: 20px"></th>
<th id="th2" style="width: 20px"></th>
</tr>
</thead>
<tbody>
<tr>
<td headers="th1" style="width: 20px"></td>
<td headers="th2" style="width: 20px"></td>
</tr>
</tbody>
</table>
</div>
How can I make both the header show and also align correctly with the td columns?
CSS includes more display modes than the commonly used none, inline, inline-block, and block. There are quite a few, in fact.
In this case, it appears what you want to use instead of display:block; is display:table-header-group;.
Here are some examples of the different styles, as applied to your table:
http://jsfiddle.net/CrYdz/1
The problem is caused by the display:block in the style attribute for the thead.
Change it to display:table-header-group
When you want to show the thead element use this value: display: table-header-group;
To set same width for table header and table body in table:
<table style="table-layout:fixed">
In case nothing fixes it. move your <tr> inside thead to tbody.
this was the only solution in my case since i had so many complications already.
Maybe the content of the THs is wider than the content of the TDs and in reality the width is not 20px as set.
So, because you first load the page without the thead, the table gets the width of the TDs. Then, when you display the THEAD by js, the table width continues being the same but probably the THs have different width.
By default, th and td should be aligned. If you want to leave it as default, just put display: unset:
.tablestuff thead {
display: unset;
}
Plain JavaScript:
document.querySelector("thead").style.display = "unset";
jQuery:
To make the jQuery's $(".tablestuff thead").show() method works, your css needs to be defined like this:
.tablestuff thead[style*='display: block'] {
display: unset !important;
}
This is because .show() will set the display to block by default. The above css will set it back to unset whenever it's set to block.
show and hide th instead of thead with the css
/* to hide */
.tablestuff thead th{
display: none;
}
/* to show */
.tablestuff thead th{
display: table-cell;
}

How do I get padding between table cells, but not around the entire table?

I want to have a table with padding between each of the cells, but not around the outer edge cells, that is, not around the table itself.
Using:
border-collapse : separate;
border-spacing : 0.5em;
gives me padding everywhere, while using:
border-collapse : collapse;
gives no padding anywhere.
Attempting an alternate approach, I can get padding solely between the cells horizontally using a td + td selector. However I can't use a tr + tr selector because it seems tr ignores margin, padding and border rules.
And, of course, padding on a plain td selector applies to all cells, even the outer-edges of the outer cells.
This only has to work for current-generation browser - no IE 6 or 7 for me thank you very much. And I am not going to loose any sleep over IE 8, though it would be nice if that worked.
This isn't a perfect solution, as it uses padding instead of border spacing. Possibly it will work for your problem though.
HTML:
<table>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</table>
And the CSS:
table {
border: 1px red solid;
border-collapse: separate;
}
td {
background-color: green;
border: 1px black solid;
padding: 10px;
}
td:first-child {
padding-left: 0px;
}
td:last-child {
padding-right: 0px;
}
tr:first-child td {
padding-top: 0px;
}
tr:last-child td {
padding-bottom: 0px;
}
Produces:
Also on jsFiddle.
Can you fiddle with the markup to add .first to the first cell in a row and .last to the last, and I guess also to the first and last rows, and then pad appropriately?

How to get css background color on <tr> tag to span entire row

I have tried everything I can think of in css in order to get a background color to span an entire table row (<tr> tag) But I keep getting a white border around each cell.
CSS (excerpt):
/*alternating row*/
table, tr, td, th {margin:0;border:0;padding:0;}
tr.rowhighlight {background-color:#f0f8ff;margin:0;border:0;padding:0;}
HTML (excerpt):
<tr class="rowhighlight"><td>A</td><td>B</td><td>C</td></tr>
It just does not want to cooperate. Thanks for helping...
table{border-collapse:collapse;}
I prefer to use border-spacing as it allows more flexibility. For instance, you could do
table {
border-spacing: 0 2px;
}
Which would only collapse the vertical borders and leave the horizontal ones in tact, which is what it sounds like the OP was actually looking for.
Note that border-spacing: 0 is not the same as border-collapse: collapse. You will need to use the latter if you want to add your own border to a tr as seen here.
Try this:
.rowhighlight > td { background: green;}
Removing the borders should make the background color paint without any gaps between the cells. If you look carefully at this jsFiddle, you should see that the light blue color stretches across the row with no white gaps.
If all else fails, try this:
table { border-collapse: collapse; }
tr.rowhighlight td, tr.rowhighlight th{
background-color:#f0f8ff;
}
Firefox and Chrome are different
Chrome ignores the TR's background-color
Example: http://jsfiddle.net/T4NK3R/9SE4p/
<tr style="background-color:#F00">
<td style="background-color:#FFF; border-radius:20px">
</tr>
In FF the TD gets red corners, in Chrome not
Have you tried setting the spacing to zero?
/*alternating row*/
table, tr, td, th {margin:0;border:0;padding:0;spacing:0;}
tr.rowhighlight {background-color:#f0f8ff;margin:0;border:0;padding:0;spacing:0;}
This worked for me, even within a div:
div.cntrblk tr:hover td {
line-height: 150%;
background-color: rgb(255,0,0);
font-weight: bold;
font-size: 150%;
border: 0;
}
It selected the entire row, but I'd like it to not do the header, haven't looked at that yet. It also partially fixed the fonts that wouldn't scale-up with the hover??? Apparently you to have apply settings to the cell not the row, but select all the component cells with the tr:hover. On to tracking down the in-consistent font scaling problem. Sweet that CSS will do this.