How to pack contents in a div to move to the right when content reaches bottom of the div

css, html, javascript, jquery, php

Solution

You can achieve the output you want using only PHP,HTML,CSS solution which you are already using:

PHP

$i=1;
echo '<a class="LibSectOpen">
<span style="display:none" class="SectionName">'.$Section.'</span>
<div class="LibrarySects"><div class="LibrarySectsHeader">'.$Section.'</div>
<div class="LibrarySectsShelf"><div class="move_right">';
while($row = mysql_fetch_array($log2)){ 
echo '<div class="LibrarySectsShelf_Book" style="background-color:'.$Color.'"
      title="Author: '.$row['bbookauthor'].'">'.$row['bbookname'].'</div>';
if($i%5==0)
{
    echo '</div><div class="move_right">';
}
$i++;
}
echo '</div></div></div></a>';

The above code uses `<div class="move_right"> ... </div>` to divide elements in group of 5 so as to display each group in a new column.

CSS (.move_right)

.move_right{
    display:inline-block;
    vertical-align:top;
}

Fiddle of how the generated HTML's output will be

Problem

Im generating some content with PHP, but after the number of contents is more than 5 the height becomes greater than that of the div, so i don't want it to stack on top of the div, but to move to the right of the div and start from the top. Here's an image. PHP ``` echo '<a class="LibSectOpen"> <span style="display:none" class="SectionName">'.$Section.'</span> <div class="LibrarySects"><div class="LibrarySectsHeader">'.$Section.'</div> <div class="LibrarySectsShelf">'; while($row = mysql_fetch_array($log2)){ echo '<div class="LibrarySectsShelf_Book" style="background-color:'.$Color.'" title="Author: '.$row['bbookauthor'].'">'.$row['bbookname'].'</div>'; } echo ' </div> </div> </a>'; ``` As it looks, the philosophy books in the example goes down, and i want it to go to the right and start another column of five books and so on. Any ideas i can do this with JQuery and CSS? ``` .LibrarySectsHeader { border:1px #CCC solid;width:500px; margin:2px; padding:1px; height:18px;border-radius:2px 2px 2px 2px; font-size:10px; color:rgba(0,0,0,1) !important; background-color: rgba(255,255,255,0.6); line-height:18px; } .LibrarySectsShelf { border:1px #CCC solid;width:499px; margin:2px; padding:1px; height:129px;border-radius:2px 2px 2px 2px; font-size:10px; background-color: rgba(255,255,255,0.2); line-height:18px; background-image:url(images/bg/wood.jpg); background-size:100%; background-repeat:no-repeat; } .LibrarySectsShelf_Book { border:1px #C90 solid;width:148px;height:23px; margin-bottom:1px;border-radius:3px 3px 3px 3px; font-size:10px; background-color: rgba(51,153,255,0.9); padding-left:2px; line-height:22px; color:rgba(255,255,255,1) !important; overflow: hidden; } .LibraryBooks { border:1px #CCC solid;width:502px; margin:2px; padding:1px;border-radius:2px 2px 2px 2px; font-size:10px; background-color: rgba(102,102,102,1); line-height:18px; } ```

Original source