ioan ioan - 11 months ago 39
PHP Question

Images loop and videos loop don't line up correctly

The videos don't line up with the images

I have td container where I use php to loop through a set of videos and a set of images. Each image and video are in a div with the same class, but for some reason the videos end up higher than the images. Any idea what causes this?

two PHP loops that output an html div for each image/video:

<td colspan="5" style="max-width: 1000px; overflow-x: auto;">
$fk = $db_table_row["images_fk"];
$image_row = find_images_by_id($fk);//get images from an sql table
for ($i = 1; $i < count($image_row)+1; $i++) {
if($image_row[$i] != null){
echo "<div id='media_wrap'><img src='{$image_row[$i]}' height='200'/>&nbsp</div>";

$fk = $db_table_row["videos_fk"];
$video_row = find_videos_by_id($fk);//get videos from an sql table
for ($i = 1; $i < count($video_row)+1; $i++) {
if($video_row[$i] != null){
echo "<div id='media_wrap'><video width=\"270\" height=\"200\" controls><source src=\"{$video_row[$i]}\"></video>&nbsp</div>";


The wrap div css:

#media_wrap{display: inline; white-space: nowrap; max-height: 200px;}

If I make 2 video or 2 image loops they line up perfectly. If I put the videos loop before the image loop then the images end up higher.

They are all set to 200 height, and are in the same id="media_wrap" div, so they should behave the same right?

it was caused by having bootstrap in the header:

<link rel="stylesheet" href="">

I could take it out, but I might really need it for dropdown menus later :(

Answer Source

A simple fix :)

You're setting max-height on the parent #media_wrap, when it needs to be applied to the children. You should define it in either #media_wrap img, #media_wrap video or #media_wrap * instead. Then you just need to float the two divs next to each other :) Note that neither display: inline or white-space: nowrap are necessary.

Also note that you're also defining two divs with the same ID, which is very bad practice. Instead, I recommend giving both of your divs a class of media_wrap instead of using it as an ID.

Finally, note that for questions pertaining to CSS, it helps to include a copy of the actual HTML output, rather than the code used to execute the output :)

I've created a fiddle with both images and videos forcibly scaled down in height, displayed next to each other. I also replaced your IDs with classes of the same name, and added a slight offset. The fiddle can be found here.

Hope this helps!


Based on your new comments, images, and code, I've got a slightly better idea of what you're trying to achieve. As such, I've slightly modified the code.

Note that you can't have two divs displayed next to each other in combination with a horizontal scrollbar. As such, I've combined both the images and video into one div.

Also, now white-space: nowrap is required ;)

New code:

Now images and videos have a fixed height, and will display next to each other with a horizontal scrollbar. Hopefully this is what you're after :)