简体   繁体   English

CSS属性删除间距

[英]CSS Properties to remove spacing

First I admit, I know CSS but not a master of that. 首先,我承认,我了解CSS,但并不了解CSS。

I have to show 4 images in a table (td). 我必须在一张表(td)中显示4张图像。 Image placement is required as follow 图像放置要求如下

A B
C D

where A , B , C & D are 4 images with no spacing between them. 其中ABCD是4张图像,它们之间没有间距。 Width of TD is 40 px and of each image is 20px. TD宽度为40像素,每个图像的宽度为20像素。

I find other questions on Stackoverflow and played with table's border-spacing, border-collapse properties to remove space. 我在Stackoverflow上发现了其他问题,并使用table's border-spacing,border-collapse属性来删除空间。 But now problem is I'm getting my images as 但是现在的问题是我正在获取我的图像

A
B
C
D  //without spacing with display:block in `td img`

or 要么

A B
      //With space, with display:inline-block on `td img`
C D 

Working demo is given on Fiddle Fiddle上给出了工作演示

Code (HTML) 代码(HTML)

<table>
<tbody>
<tr>
    <td valign="top" width="40px">
        <img width="20px" src="https://m.ak.fbcdn.net/profile.ak/hprofile-ak-ash4/187580_670310756_917182522_q.jpg" title="a">
        <img width="20px" src="https://m.ak.fbcdn.net/profile.ak/hprofile-ak-ash4/187580_670310756_917182522_q.jpg" title="b">
        <img width="20px" src="https://m.ak.fbcdn.net/profile.ak/hprofile-ak-ash4/187580_670310756_917182522_q.jpg" title="c">
        <img width="20px" src="https://m.ak.fbcdn.net/profile.ak/hprofile-ak-ash4/187580_670310756_917182522_q.jpg" title="d">
    </td>
</tr>
</tbody>
</table>

Code (CSS) 代码(CSS)

table {
    display: table;
    border-collapse: separate;
    border-spacing: 2px;
    border-color: gray;
}
tbody {
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
}
tr {
    display: table-row;
    vertical-align: inherit;
    border-color: inherit;
}
td {
    display: inline-table;
    border-spacing: 0px;
    border-collapse: collapse;
}
td img {
    display: block;
}

What CSS properties should I change to make Fiddle as expected. 我应该更改哪些CSS属性才能使Fiddle达到预期的效果。

Add float:left to the td img class float:left to添加float:left to td img类

updated demo http://jsfiddle.net/59vGe/6/ 更新了演示http://jsfiddle.net/59vGe/6/

New demo http://jsfiddle.net/59vGe/14/ 新演示http://jsfiddle.net/59vGe/14/

You can either float them , or align them vertically with inline-block display 您可以浮动它们,或将它们与行内块显示垂直对齐

td img {
    float:left;    
}

Float 浮动

td img {
    display: inline-block;
    vertical-align:top;    
}

Vertical Align 垂直对齐

Used give to font-size:0; 用来给font-size:0; in your td and remove display:block; 在您的td中 删除 display:block; in your img tag 在您的img标签中

as like this 像这样

      td {
        display: inline-table;
        border-spacing: 0px;
font-size:0; //add this line
        border-collapse: collapse;
        }td img {
    display:block; // remove this line
        }

Upated Demo 升级演示

Take a look here: JSFiddle 在这里看看: JSFiddle

I used a combination of float:left clear:left :nth-child() and display:table-cell 我使用了float:left clear:left :nth-child()display:table-cell

If you can modify the HTML for this you can ad an class attribute to the td element like so: <td class="image-grid"> 如果您可以为此修改HTML,则可以为td元素添加class属性,如下所示: <td class="image-grid">

and then update the CSS to select specifically for that: 然后更新CSS以专门为此选择:

td.image-grid img {
    float:left;
    display: block;
}

td.image-grid img:nth-child(3){
    clear:left;
}

Float the images. 浮动图像。

td img {
    float:left;
}

Here is the Fiddle 这是小提琴

you can use float . 您可以使用float。 but please don't give for all td . 但是请不要全力以赴。 instead of that you can give particular class name . 而不是您可以给特定的类名。

.testclass td img {
float:left;    
}

It will apply to the testclass td only. 它将仅适用于testclass td。 it will not affect other table td values. 它不会影响其他表的td值。

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

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