简体   繁体   English

CSS:是否可以删除所有填充和间距(小于0像素)?

[英]CSS: It is possible to remove all padding and spacing, below 0 pixels?

I've put a new image in my table cells and it's making the table a bit bulky. 我在表格单元格中放置了一个新图像,这使表格有点笨重。 I've reduced all padding and spacing to 0, but there are still gaps of about 4 pixels around the edge of the images (32 pixel rectangular flags). 我将所有填充和间距都减小为0,但是在图像边缘(32个像素的矩形标志)周围仍然有大约4个像素的间隙。 It is possible to remove all white space from a table cell? 可以从表格单元格中删除所有空白吗?

My CSS for the cells is : 我的单元格的CSS是:

.flag {
    margin: 0px !important;
    padding: 0px !important;
    border-spacing:0 !important;
    height:28px !important;
    width:32px !important;
    line-height:32px !important;
}

Note that they need '!important' because they're over-riding another CSS. 请注意,它们需要'!important',因为它们将覆盖另一个CSS。 The exact height of the flags appears to be 28px. 标志的确切高度似乎是28px。 If I reduce height below that, the flag starts to get shaved off at the bottom of the cell. 如果我将高度降低到低于该高度,该标志将在单元格的底部开始被刮掉。

More info so people can try for themselves. 更多信息,以便人们可以自己尝试。

 td { border: 1px solid black; } 
  <link rel="stylesheet" type="text/css" href="//cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" /> <table class = 'f32'><tr><td class = 'flag us'></td></tr></table> 

Make sure your table has visible borders and you'll see that there's a fair bit of spacing above and below the flag image. 确保您的桌子上有可见的边框,并且您会看到标记图像的上方和下方都有一定的间距。 I want to get rid of that, or preferably all except 1 pixel above and below. 我想摆脱它,或者最好除上面和下面的1个像素以外的所有元素。

It depends if the white space is all around the images or only below them. 这取决于图像周围是空白还是仅在图像下方。

Try to add vertical-align:baseline; 尝试添加vertical-align:baseline; to your CSS rule for the cells - this should at least eliminate unwanted space below the image. 单元格的CSS规则-至少应消除图片下方的多余空间。

If there is whitespace within the image itself, you might try negative margin values on the images, like 如果图像本身内有空格,则可以在图像上尝试使用负空白值,例如

.flag img { 
  margin-top: -4px;
}

The whitespace is within the image itself, and it's using a "sprite", wherein there are multiple images and a background-position is set such that the appropriate icon is displayed. 空格位于图像本身内,并且使用“ sprite”,其中有多张图像,并且设置了background-position以显示适当的图标。

I've replicated what I think you want below. 我在下面复制了我想你想要的东西。 Note that I've overridden the styles without the use of !important . 请注意,我不使用!important覆盖了样式。 You just need the selector to be more specific than the one you're overriding. 您只需要选择器比您要覆盖的选择器更具体即可。

 .f32 td.flag.us { border: 1px solid black; height: 23px; overflow: hidden; background-position: 0 -7332px; width: 30px; } 
 <link rel="stylesheet" type="text/css" href="//cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" /> <table class='f32'> <tr> <td class='flag us'></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM