0

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:

enter image description here

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 ?

lxg
  • 12,375
  • 12
  • 51
  • 73
Shibbir
  • 257
  • 4
  • 24

2 Answers2

5

I solved a similar issue by dividing the page into columns. The divs are then basically underneath each other. This is a simple, CSS only solution, but it's not perfect. There is a gap at the bottom of the page, and above that, the order changes of course from a horizontal to a vertical ordering.

Nevertheless, it might be useful for you. This is the page it is used on:

http://www.eftepedia.nl/lemma/Categorie%C3%ABn

You can do this with CSS columns, like this:

.container {
    -webkit-columns: auto 3; /* Chrome, Safari, Opera */
    -moz-columns: auto 3; /* Firefox */
    columns: auto 3;
}

For the boxes you can use break-inside: avoid-column; to avoid them breaking over multiple columns. Also see this question for more information about specific issues with that.

Bringing it together in a minimal example fiddle: http://jsfiddle.net/02f4wqcm/

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

You need to use jQuery for removing the spaces.

Try following libraries

http://isotope.metafizzy.co/layout-modes.html

http://packery.metafizzy.co/options.html

Varun Victor
  • 100
  • 5