如何删除表中行和列之间不需要的空格?

时间:2010-02-17 09:06:30

标签: html css html-table

如何删除表格中行和列之间的额外空格。

我尝试更改表格和tr和td上的边距,填充和各种边框属性。

我希望这些照片彼此相邻,看起来像一个大图像。

我该如何解决这个问题?

CSS

table {
  border-collapse: collapse;
}

HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title>Tera Byte Video Game Creation Camp</title>
  <link rel="stylesheet" type="text/css" href="style.css"></link>
</head>

<body>
  <table class="mytable" align="center">
    <tr class="header">
      <td colspan="3"><img src="images/home_01.png" /></td>
    </tr>
    <tr class="top">
      <td colspan="3"><img src="images/home_02.png" /></td>
    </tr>
    <tr class="link-row">
      <td><img src="images/home_03.png" /></td>
      <td><img src="images/home_04.png" /></td>
      <td><img src="images/home_05.png" /></td>
    </tr>
    <tr class="link-row">
      <td><img src="images/home_07.png" /></td>
      <td><img src="images/home_06.png" /></td>
      <td><img src="images/home_08.png" /></td>
    </tr>
    <tr class="link-row">
      <td><img src="images/home_09.png" /></td>
      <td><img src="images/home_10.png" /></td>
      <td><img src="images/home_11.png" /></td>
    </tr>
    <tr class="link-row">
      <td><img src="images/home_12.png" /></td>
      <td><img src="images/home_13.png" /></td>
      <td><img src="images/home_14.png" /></td>
    </tr>
    <tr class="bottom">
      <td colspan="3"><img src="images/home_15.png" /></td>
    </tr>
  </table>

</body>

</html>

14 个答案:

答案 0 :(得分:181)

添加到vectran的答案:您还必须在表元素上设置cellspacing属性以实现跨浏览器兼容性。

<table cellspacing="0">

编辑(为了完整起见,我将在5年后扩展这个:):

Internet Explorer 6 Internet Explorer 7 要求您直接将cellspacing设置为表属性,否则间距不会消失。

Internet Explorer 8 及更高版本和所有其他版本的热门浏览器 - Chrome,Firefox,Opera 4 + - 支持CSS属性border-spacing。< / p>

因此,为了使跨浏览器表格单元格间距重置(支持IE6作为恐龙浏览器),您可以按照以下代码示例:

table{
  border: 1px solid black;
}
table td {
  border: 1px solid black; /* Style just to show the table cell boundaries */
}


table.no-spacing {
  border-spacing:0; /* Removes the cell spacing via CSS */
  border-collapse: collapse;  /* Optional - if you don't want to have double border where cells touch */
}
<p>Default table:</p>

<table>
  <tr>
    <td>First cell</td>
    <td>Second cell</td>
  </tr>
</table>

<p>Removed spacing:</p>

<table class="no-spacing" cellspacing="0"> <!-- cellspacing 0 to support IE6 and IE7 -->
  <tr>
    <td>First cell</td>
    <td>Second cell</td>
  </tr>
</table>

答案 1 :(得分:77)

将此CSS重置添加到您的CSS代码:(From here

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

它会有效地重置CSS,摆脱填充和边距。

答案 2 :(得分:64)

这对我有用:

#table {
  border-collapse: collapse;
  border-spacing: 0;
}

答案 3 :(得分:55)

对于td中的图像,请将其用于图像:

display: block;

为我删除不需要的空间

答案 4 :(得分:8)

table 
{
    border-collapse: collapse;
}

将折叠分隔表格列的所有边框...

或尝试

<table cellspacing="0" style="border-spacing: 0;">

将所有单元格间距设置为0,将边框间距设置为0以实现相同的效果。

玩得开心!

答案 5 :(得分:8)

对于符合标准的HTML5,添加所有这些css以删除表格中图像之间的所有空格:

table { 
    border-spacing: 0;
    border-collapse: collapse;
}
td {
    padding:0px;
}
td img {
    display:block;
}

答案 6 :(得分:7)

只需添加雅各布答案,img中的td

body {line-height: 0;}
img {display: block; vertical-align: bottom;}

适用于大多数电子邮件客户端,包括Gmail。但不是Outlook。 对于展望,您需要再做两个步骤:

table {border-collapse: collapse;}

并将每个td元素设置为与其包含的图像具有相同的heightwidth。例如,

<td width="600" height="80" style="line-height: 80px;">
    <img height="80" src="http://www.website.com/images/Nature_01.jpg" width="600" />
</td>

答案 7 :(得分:4)

将Cellpadding和cellspacing设置为0将删除行和列之间不必要的空间......

答案 8 :(得分:4)

试试这个,

table{
border-collapse: collapse; /* 'cellspacing' equivalent */
}

table td, 
table th{
padding: 0; /* 'cellpadding' equivalent */
}

答案 9 :(得分:2)

将此行添加到文件的开头为我工作:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

修改css以设置正确的边框属性不起作用,直到我添加上面的行

答案 10 :(得分:1)

我要做的就是添加:

line-height: 0px;

在我的

<tr ...>

答案 11 :(得分:0)

您是否尝试删除具有colspan的TR并查看它是否发生了任何变化?

在精确的桌面设计中,我经历过colspans和rowpans非常讨厌。如果你的图像在没有colspan-TR的情况下看起来没问题,我会从那里开始构建一个嵌套的表集。

你的style.css似乎也不完整,也许那里有什么问题?我至少会在表格中添加padding: 0; margin: 0;(或者在“mytable”类中添加img { border: 0; })。 请确保您的图片也没有空格和/或边框(例如,在样式表中添加{{1}})。

答案 12 :(得分:0)

我遇到了这个问题,好几个帖子的建议都没有奏效,我全部尝试了。我的问题原来是我正在创建的表嵌套在另一个表中,继承了它的属性。我需要覆盖容器表。

在此示例中,SuColTable是表上的类。

.SuColTable tr {
    border: none !important;
}

.SuColTable td {
    border-spacing: 0 !important;
    border: none !important;
    margin: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    padding-right: 0 !important;
}


.SuColTable {
    border-collapse: collapse !important;
    border: none !important;
    border-spacing: 0 !important;
    border-collapse: separate !important;
    padding-left: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
}
// all padding would be padding: 0 !important

边框塌陷,border:none和border-spacing摆脱了表格/行/列的行。填充0调用从容器表中删除了填充。我必须在每个样式上都包含!important才能起作用,以便覆盖容器表样式。

我有padding_top,left和bottom(而不是只有padding),因为我还有另外两个控制padding_right的类。

line-height: 0px;

行高零使行高一行。如果您有多行,则将它们压缩为一行。

答案 13 :(得分:-1)

Duration d = Duration.ofSeconds(ttlNumSec);
int hours = d.toHours();
int minutes = d.toMinutesPart();
int seconds = d.toSecondsPart();
table{
  border: 1px solid black;
}
table td {
  border: 1px solid black; /* Style just to show the table cell boundaries */
}


table.no-spacing {
  border-spacing:0; /* Removes the cell spacing via CSS */
  border-collapse: collapse;  /* Optional - if you don't want to have double border where cells touch */
}

相关问题