Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the margin padding doesn't work in table td and tr?

Tags:

html

css

<table  border="0" style="padding:10px;width: 960px;border-collapse: collapse;">

   <tbody>
<tr>
<td  style="padding: 0"><img src="8.jpg" alt="" /></td>
<td  style="padding: 0"><img src="98.jpg" alt="" /></td>
<td  style="padding: 0"><img src="998.jpg" alt="" /></td>
<td  style="padding: 0"><img src="1998.jpg" alt="" /></td>
</tr>
<tr>
<td  style="background-color: #757575;">11194</td>
<td  style="background-color: #757575;">11194</td>
<td  style="background-color: #757575;">11194</td>
<td  style="background-color: #757575;">11194</td>
</tr>
<tr>
<td  style="padding: 0"><img src="8.jpg" alt="" /></td>
<td  style="padding: 0"><img src="98.jpg" alt="" /></td>
<td  style="padding: 0"><img src="998.jpg" alt="" /></td>
<td  style="padding: 0"><img src="1998.jpg" alt="" /></td>
</tr>

</tbody>
</table>

why i can't add margin padding to the tr and td? i want the td (<td style="background-color: #757575;">11194</td>) have margin-right.namely, each separated by some space. but can't work. how to correct it. and the second tr have some space between the following tr.

like image 340
user1932607 Avatar asked Dec 30 '12 06:12

user1932607


People also ask

Can you put padding on a tr?

The key point here is that the tr element must be displayed as a block in order for padding to apply at the tr level. This totally screws with how table layout usually works. If you're doing this, you shouldn't be using a table - use a regular div .

How do you put padding on a table tr?

To set cell padding in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property padding.

Why margin right is not working on table?

margin-right will not work if you set width to 100%. What you could do is : wrap table in a div tag.


3 Answers

margin specifications are ignored for table cells Refer: http://www.w3.org/TR/CSS2/tables.html and CSS Cell Margin

Internal table elements generate rectangular boxes with content and borders. Cells have padding as well. Internal table elements do not have margins.

If you wanna add padding, just set border-collapse:separate refer: http://www.w3schools.com/cssref/playit.asp?filename=playcss_border-collapse

like image 98
macio.Jun Avatar answered Nov 07 '22 18:11

macio.Jun


I guess you are asking for border-collapse; so try using it...

table {
   border-collapse: collapse;
}

If you want to add padding you can do it like

table.class_name td {
   padding: 0; 
}
like image 24
Mr. Alien Avatar answered Nov 07 '22 18:11

Mr. Alien


The reason because you are giving padding on table element and on td and you are giving padding:0

Remove border-collapse: collapse from table element and you can handle padding and margin easily in this table.

See the demo fiddle for concepts: http://jsfiddle.net/GVt3z/

Note: Above fiddle is not containing exact answer.

See updated fiddle for margin-right space but with different technique.

http://jsfiddle.net/GVt3z/1/

like image 35
w3uiguru Avatar answered Nov 07 '22 16:11

w3uiguru