I'm getting all data from mysql database and showing in CSS Div box {class = box} using php while loop. Now all data is showing perfectly but I see that there a big gap between the boxes. How can i remove the gap/space ? It's look like the following image:

I'm using following CSS Code:
.box{
    float:left;
    position: relative;
    width:320px;    
    border:1px #ccc solid;
    padding:5px;
    margin:0 5px 5px 0;
}
It's should be show like http://www.pinterest.com/ home page.
Here is the php code:
<?php
$query =  mysql_query("SELECT DISTINCT * FROM keyword_type ORDER BY keyword_full_name ASC");
$numType =  mysql_num_rows($query);
while($result = mysql_fetch_array($query)){ 
    $typeID =  $result['keyword_typeID'];
    $keywordType =  strtoupper($result['keyword_full_name']);
    echo "<div class='box'>";
    echo "<h3><strong>$keywordType</strong></h3>";
    $query2 =  mysql_query("SELECT * FROM keywords WHERE keyword_typeID = '$typeID' ORDER BY keywordName ASC ");
    $num2 =  mysql_num_rows($query2);
    while($result2 =  mysql_fetch_array($query2)){
        $kid = $result2['kid'];
        $keywordName = ucfirst($result2['keywordName']);
        $query4 =  mysql_query("SELECT kid FROM userkeywords WHERE cdid = '$cdid' AND kid='$kid'");
        $num = mysql_num_rows($query4);
        if($num > 0){
            $class = "keywordHighlight";    
        }else{
            $class = "";
        }
        echo "<div onclick='keywordclick($kid,$cdid);' class='$class'>$keywordName</div>";          
    }
    echo "</div>";
}
?>
Any help ?
 
     
     
     
    