...

View Full Version : fit images in div grid (exciting problem to think about)



jimmyfortune
11-09-2010, 08:21 PM
hi guys,

i have a div with a width of 500px and an undefined height.
i'm using images of 20px x 15px as a base. i want to fill
this div with this images. if i give them a border, a grid will
be visible.

however, i'm also using images of 40px x 15px, or images
of 40px x 30px.

so i want the div to be full with images. so all spaces must
be filled. one of the problems is that the images will be aligned
by the 30px height of some of the images, so there will be
spaces on top of the smaller images..

can anyone help me? it's a pretty exciting problem to figure out i think :)

eventually i will have to display the images randomly and it
still has to fit every time...

grtz,
jimmy

optimus203
11-10-2010, 03:46 AM
For something like this, if I'm understanding correctly, your best bet might be absolute positioning. Here an idea (not saying its the way to go, but its an idea).



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Four images All in a Row!</title>
<style type="text/css" media="screen">
div {width: 200px;}
img {
border:1px solid red;
}
</style>
</head>

<body>
<div>
<!-- row 1 -->
<img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:0;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:22px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:44px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:66px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:88px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:110px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:132px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:154px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:176px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:198px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:0; left:220px;" />

<!-- row 2 -->
<img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:0;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:22px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:44px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:66px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:88px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:110px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:132px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:154px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:176px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:198px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:17px; left:220px;" />

<!-- row 3 -->
<img
src="pic.jpg" width="40" height="30" style="position:absolute; top:34px; left:0;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:34px; left:42px;" /><img
src="pic.jpg" width="20" height="15" style="position:absolute; top:50px; left:42px;" />
<!-- etc -->

</div>
</body>
</html>

mahesh2010
11-10-2010, 01:13 PM
hi guys,

i have a div with a width of 500px and an undefined height.
i'm using images of 20px x 15px as a base. i want to fill
this div with this images. if i give them a border, a grid will
be visible.

however, i'm also using images of 40px x 15px, or images
of 40px x 30px.

so i want the div to be full with images. so all spaces must
be filled. one of the problems is that the images will be aligned
by the 30px height of some of the images, so there will be
spaces on top of the smaller images..

can anyone help me? it's a pretty exciting problem to figure out i think :)

eventually i will have to display the images randomly and it
still has to fit every time...

grtz,
jimmy
Hi,
can you post the link you didn't reply this seems your problem is unsolved if you post the link i can tell you how to make it any query let me know

DrDOS
11-11-2010, 03:07 AM
This will involve something besides HTML and CSS, it will need javascript or a server side language, and probably both, to handle all the size information and perform all the conditional operations that will be needed to do this automatically. I doubt you'll get someone to jump up and say 'that would be a fun project, I'll just do it for free'. So maybe first just do an intensive search, in case someone has already published a way. Or you may figure out a way using HTML/CSS by separating the images by size in different divs, that suits you.

jimmyfortune
11-11-2010, 10:04 AM
hi guys,

thanks a lot for your help already! thanks optimus for getting me started on this!

i didn't have time to answer, but it's not resolved. i'm planning on using
php to do this in an automatic way. i don't have a link yet. i will start on the script today.

any thought are welcome ofcourse :)

cheers

jimmyfortune
11-11-2010, 11:43 AM
hi guys,

i made a test, but something very strange happens:
http://www.storylinedesign.be/sites/test.php

when i compare my sourcecode to what firebug makes of it, it seems like the "left:xx" style parts just disappear...

source code:


<div width="1000">
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:0;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:50;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:100;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:150;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:200;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:250;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:300;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:350;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:400;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:450;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:500;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:550;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:600;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:650;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:700;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:750;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:800;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:850;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:900;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:950;" />
</div>


the source code looks good, but the images are all stacked on top of eachother...

anyone know why?

thanks!

jimmy

jimmyfortune
11-11-2010, 04:19 PM
hi guys,

i made a test, but something very strange happens:
http://www.storylinedesign.be/sites/test.php

when i compare my sourcecode to what firebug makes of it, it seems like the "left:xx" style parts just disappear...

source code:


<div width="1000">
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:0;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:50;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:100;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:150;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:200;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:250;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:300;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:350;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:400;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:450;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:500;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:550;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:600;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:650;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:700;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:750;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:800;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:850;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:900;" />
<img src="/empty.jpg" width="50" height="30" style="position:absolute; top:0; left:950;" />
</div>


the source code looks good, but the images are all stacked on top of eachother...

anyone know why?

thanks!

jimmy

tracknut
11-11-2010, 04:24 PM
For one, you need some units on the left tags. Inches? Miles? Pixels? My guess is pixels, so left:100px, etc.

Dave

jimmyfortune
11-11-2010, 05:18 PM
i tried that already and it's not helping :(

tracknut
11-11-2010, 05:35 PM
Could you show the actual code that's failing? I can't get to the link you provided. You might also clean up the rest of your code so that it validates, but I don't see a reason it would fail as shown, if you updated it with the "px" I mentioned. You might correct the <div width="1000"> to validate as well, but I don't know that it would cause the issue you're seeing.

Dave

Excavator
11-11-2010, 05:38 PM
Hello jimmyfortune,
Run your code through the validator real quick. It'll show you a few more errors that are causing you trouble:

http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.storylinedesign.be%2Fsites%2Ftest.php


Would resizing the images be easier? Have a look at this -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {background: #FC6;}
#container {
width: 500px;
margin: 30px auto;
background: #999;
overflow: auto;
}
img {
width: 50px;
height: 30px;
float: left;
border: 1px solid #ff0;;
}
</style>
</head>
<body>
<div id="container">
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="20x15.jpg" width="20" height="15" alt="20x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x15.jpg" width="40" height="15" alt="40x15" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<img src="40x30.jpg" width="40" height="30" alt="40x30" />
<!--end container--></div>
</body>
</html>

jimmyfortune
11-11-2010, 06:36 PM
damn you were right. i needed to place "px" for the top and left position.
i thought i did it, but i only did it for the width and height.

thanks guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum