为什么我的表格单元格似乎有填充,即使我已将其设置为不填充?

发布于 2024-10-04 14:00:51 字数 991 浏览 1 评论 0原文

我创建了一个没有边距、填充或边框的 skinny CSS 类:

.skinny
{
    margin:0 0 0 0;
    padding:0 0 0 0;
    border:0 0 0 0;
}

我将其应用到包含图像的行,该图像也应用了 skinny 类:

<td width="33%" align="center" class="skinny">
    <table width="400px" height="180px" class="skinny">
        <tr class="skinny">
            <td class="skinny" width="60px" height="100px"><a class="skinny" href="/"><img class="skinny" width="60px" height="100px" id="snapshot" src="/images/snapshot.png"></a></td>
            <td class="skinny" width="120px" height="100px"><a class="skinny" href="/"><h1 class="skinny">Product</h1></a></td>
        </tr>
    </table>
</td>

I我试图让图像尽可能接近下一个单元格中的

文本,以便它们从左到右相互推挤。

但无论我将 skinny 类应用到多少个元素,每个表格单元格周围似乎都有类似“填充”的东西,在图像和文本之间创建了一个空间。

我该如何删除它?

I created a skinny CSS class that has no margin, padding or border:

.skinny
{
    margin:0 0 0 0;
    padding:0 0 0 0;
    border:0 0 0 0;
}

And I applied it to a row containing an image which also has the skinny class applied to it:

<td width="33%" align="center" class="skinny">
    <table width="400px" height="180px" class="skinny">
        <tr class="skinny">
            <td class="skinny" width="60px" height="100px"><a class="skinny" href="/"><img class="skinny" width="60px" height="100px" id="snapshot" src="/images/snapshot.png"></a></td>
            <td class="skinny" width="120px" height="100px"><a class="skinny" href="/"><h1 class="skinny">Product</h1></a></td>
        </tr>
    </table>
</td>

I'm trying to get the image to appear as close as possible to the <h1> text in the next cell so that they are pushed up against each other, left-to-right.

But no matter how many elements I apply the skinny class to, there seems to be something like a 'padding' around each of the table cells that creates a space between the image and the text.

How do I remove that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

晨与橙与城 2024-10-11 14:00:52

桌子本身也提供填充。
所以表定义需要是

<table width="400px" height="180px" class="skinny" cellspacing="0" cellpadding="0">

the table itself also give padding.
so the table definition needs to be

<table width="400px" height="180px" class="skinny" cellspacing="0" cellpadding="0">
软糯酥胸 2024-10-11 14:00:52

可能不是填充
试试这个

border-collapse:collapse;

It may not be padding
just try this

border-collapse:collapse;
做个ˇ局外人 2024-10-11 14:00:52

图像是内联元素,位于基线上,它们被视为没有下伸部的字母(即像 a、b 和 c,但不像 g、j 和 y)。

将图像设置为 display: block 以避免这种情况(或使用 vertical-align 进行调整)

更好,因为它看起来像你有一个 1x2 的桌子:don' t 使用表格进行布局

Images are inline elements and sit on the base line, they are treated just like a letter with no descender (i.e. like a, b and c but not g, j and y).

Set the image to display: block to avoid this (or twiddle with vertical-align)

Better yet, since it looks like you have a 1x2 table: don't use tables for layout

鸢与 2024-10-11 14:00:52

我遇到了同样的问题,并用谷歌搜索了几个小时。问题在于设置 td 元素的高度。如果内容高度为 60px,td 高度为 120px,则顶部和底部各有 30px 的填充。

所以正确答案是:(

<td width="33%" align="center" class="skinny">
    <table width="400px" height="180px" class="skinny">
        <tr class="skinny">
            <td class="skinny" width="60px"><a class="skinny" href="/"><img class="skinny" width="60px" height="100px" id="snapshot" src="/images/snapshot.png"></a></td>
            <td class="skinny" width="120px"><a class="skinny" href="/"><h1 class="skinny">Product</h1></a></td>
        </tr>
    </table>
</td>

删除高度)

I had same issue, and googled for hours. Issue was with setting height on td elements. If content height is 60px, and td height is 120px, there will be 30px padding on top and bottom each.

So the correct answer will be:

<td width="33%" align="center" class="skinny">
    <table width="400px" height="180px" class="skinny">
        <tr class="skinny">
            <td class="skinny" width="60px"><a class="skinny" href="/"><img class="skinny" width="60px" height="100px" id="snapshot" src="/images/snapshot.png"></a></td>
            <td class="skinny" width="120px"><a class="skinny" href="/"><h1 class="skinny">Product</h1></a></td>
        </tr>
    </table>
</td>

(removed height)

蓝礼 2024-10-11 14:00:52

边距崩溃

迟到了,但这是因为您的 h1 标记很可能在顶部或底部有一些边距。边距一直折叠到 dom 中最上面的元素,直到找到 padding-top 或 padding-bottom 不等于 0 的元素(这会中断折叠)。这是一个非常令人恼火的功能。

当我最意想不到的时候,崩溃的边距总是会回来咬我,而且调试起来非常棘手。

Collapsing margins

Late to the party but it is because your h1 tag most likely has some margins top or bottom. Margins collapse all the way up through to there uppermost element in the dom until it finds an element with padding-top or padding-bottom that does not equal 0 (this interrupts the collapsing). It is a very infuriating feature.

Collapsing margins always come back to bite me when I least expect it and there very tricky to debug.

我ぃ本無心為│何有愛 2024-10-11 14:00:52

将其添加到您的 tr 的 & td

cellpadding="0"
cellspacing="0"

Add this to your tr's & td's

cellpadding="0"
cellspacing="0"
旧夏天 2024-10-11 14:00:52

对我来说,这是一个 line-height 和我放置的

之间不匹配的问题在它内部,这又是由 tailwind text-* 类引起的。

For me it was an issue with a mismatch between the line-height of the <td> and the <div> I had placed inside it which was in turn being caused by the tailwind text-* classes.

┊风居住的梦幻卍 2024-10-11 14:00:52

/* 去除内边距和边距 */

*  
    { 
    margin: 0; 
    padding: 0; 
    border: 0; 
}

/* Remove padding and margin */

*  
    { 
    margin: 0; 
    padding: 0; 
    border: 0; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文