简体   繁体   English

如何消除块div之间的所有空间?

[英]How can I eliminate all space between block divs?

In the fiddle below, I've created a 16x16 table using jQuery. 在下面的小提琴中,我使用jQuery创建了一个16x16表。 table fiddle 桌子小提琴

Here is the CSS: 这是CSS:

td {
width: 30px;
height: 30px;
background-color: blue;
border: 1px solid red;
display: inline-block;
}

And here is the jQuery: 这是jQuery:

$(document).ready(function() {
    $('body').append('<table></table>')
    for (var i=1; i<=16; i++) {
        $('table').append('<tr></tr>');
        for (var j=1; j<=16; j++) {
            $('table:nth-last-child(1)').append('<td></td>');
        }
    }
});

As it stands, there is white space between each row of the table, and I would like to remove it. 就目前而言,桌子的每一行之间都有空白区域,我想将其删除。 Can anyone offer advice on how to achieve this? 任何人都可以提供如何实现这一目标的建议吗

Things I've tried: 我试过的事情:

  1. A reset.css file. 一个reset.css文件。
  2. Using divs instead of a table (see this other fiddle or the alternate jquery below (CSS same as before but targeting has been changed to .container ) 使用div而不是表格(请参阅下面的另一个小提琴或替代jquery(CSS与之前相同,但目标已更改为.container

     $(document).ready(function() { for (var i=1; i<=16; i++) { $('body').append('<div class="row"></div>'); for (var j=1; j<=16; j++) { $('body:nth-last-child(1)').append('<div class="container"></div>'); } } }); 

So, can anyone help me remove the white space? 那么,任何人都可以帮我删除空白区域吗?

Part of the problem is that you're appending <td> to the <table> and not to the <tr> . 部分问题是您将<td>附加到<table>而不是<tr> Also, you should use border-collapse: collapse; 另外,你应该使用border-collapse: collapse; to eliminate default borders from the table. 消除表中的默认边框。

Here is a working example : 这是一个工作示例:

 $(document).ready(function() { $('body').append('<table></table>') for (var i=1; i<=16; i++) { $('table').append('<tr></tr>'); for (var j=1; j<=16; j++) { $('table tr:nth-last-child(1)').append('<td></td>'); } } }); 
 table { border-collapse: collapse; } td { width: 30px; height: 30px; background-color: blue; border: 1px solid red; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Alternate example with <div> <div>替代示例

 $(document).ready(function() { for (var i = 1; i <= 16; i++) { var $row = $('<div />') .addClass('row'); $('body').append($row); for (var j = 1; j <= 16; j++) { $row.append('<div class="container"></div>'); } } }); 
 div.row { clear: both; } div.container { width: 30px; height: 30px; background: #ccc; border: 1px solid #000; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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