tags:

views:

506

answers:

8

My code is at the end of this post.

How on earth do i get "box 3" to aligh next to "box 1" OR to the content inside "box 1" instead of aligning itself at the end of "box 2".

The important thing here is that all four boxes MUST use the same style - (style 2) as it's being used in an called by an array.

Here is what it is now and what i#m trying to make happen:

http://img524.imageshack.us/img524/1408/lastexample.gif

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {border:1px solid #000000; width:620px; position:relative;
}
.style2 {border:1px solid #000000; width:300px; float:left;}
-->
</style>
</head>

<body>
<div class="style1">

    <div class="style2">box 1<br>
      <br>
      <br>
    </div>
    <div class="style2">box 2<br>
      <br>
      <br>
      <br>
  </div>

    <div class="style2">box 3<br>
      <br>
    </div>
    <div class="style2">box 4<br>
      <br>
      <br>
    </div>
</div>
</body>
</html>
A: 

You could use a CSS or HTML table to arrange the divs exactly how you want.

HTML example (I changed the class attribute to a more concise version because you used the same classes for everything):

<div class="style1 style2">
  <table>
    <tr>
      <td>box 1</br></br></br></td>
      <td>box 3</br></br></br></td>
    </tr>
    <tr>
      <td>box 2</br></br></br></td>
      <td>box 4</br></br></br></td>
    </tr>
  </table>
</div>

Also, it's better style to use a closing slash for tags that don't have both an open and close tag; that is, to use </br> and </hr> instead of <br> and <hr>. That's up to you but I figured I'd point it out.

maksim
i don't see how that will work, can you show me? Did you understand what the issue at hand is here?
Jim
I answered your first question: you wanted to have box 3 line up next to box 1, right? By putting them into an HTML table in the same row in side-by-side cells, you achieve that.
maksim
To see it, paste my code into:http://www.w3schools.com/HTML/tryit.asp?filename=tryhtml_basic
maksim
that's that what i was after, see the problem?<div class="style1 style2"> <table> <tr> <td>box 1</br></br></br></td> <td><p>box 3 <br> is now making box 1 much bigger than it needs to be, i want box two to push up to box 1</br> </br> </br> </p> </td> </tr> <tr> <td>box 2</br></br></br></td> <td>box 4</br></br></br></td> </tr> </table></div>
Jim
If you want box 2 to be under box 1 and closer to it, you'd have to change the CSS of box 1 and/or remove the line breaks. Is that what you mean?
maksim
nope, image all the divs (box 1 to 4) have different amounts of content in them (text basically). Now imagine i want them to align vertically right next to each other.All four divs must use the same style to achieve this as they are being called to be displayed in a php loop.
Jim
A: 

I think it's impossible to do that with just one style. I would probably apply float: left to every odd box and float: right to every even box. That way you can loop over your array and apply an appropriate class and not give special treatment to any particular box.

MiseryIndex
that sure does seem to be the correct css solution to achieve what i need. Now i need to figure out how to get allow and even and odd style in the array, which i don#'t actually know how to do :(
Jim
What language/framework are you using to generate your HTML?
Larsenal
php, i'm not a coder, though, so to me its tricky to do things like this.
Jim
+2  A: 

Don't know of a way to get this with pure CSS using a single class for all the interior DIVs. You want something like Masonry.

Think of Masonry as the flip side of CSS floats. Where as floats arrange elements horizontally then vertically, Masonry arranges them vertically then horizontally. The result leaves no vertical gaps between elements of varying height, just like a mason fitting stones in a wall.

-The Masonry home page

Larsenal
i like the look of this, but i'd rather find a simpler solution, thanks though, was interesting.
Jim
A: 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {border:1px solid #000000; width:620px; position:relative;}
.style2 {border:1px solid #000000; width:300px; float:left;}

.col1 {
width:300px;
float:left;
}
.col2 {
margin-left:300px;
}
-->
</style>
</head>

<body>
<div class="style1">
<div class="col1">
    <div class="style2">box 1<br>
      <br>
      <br>
    </div>
    <div class="style2">box 3<br>
      <br>
    </div>
</div>
<div class="col2">
    <div class="style2">box 2<br>
      <br>
      <br>
      <br>
  </div>
    <div class="style2">box 4<br>
      <br>
      <br>
    </div>
</div>
</div>
</body>
</html>
serge_bg
wowza, i think you've cracked it. I'll try this on my site and report back.
Jim
spoke too soon, its an array, the code saying <div="style1"></div> is called each time for every box (using dynamic content), so i can't have new divs inbetween the calling of this.
Jim
ok, I will suggest another one.
serge_bg
A: 

I am going to assume these divs are being dynamically created.

Your current problem is you can't get the floated items any lower than the tallest of the divs in that row. I can't think offhand of a way to do what you want using css. However if you are creating these divs by looping over an array, it is solveable.

I would probably create two side by side divs and in the loop check for if the loop count is even. If it is append the dynamic div to the right column instead of the left. This will look odd, if the height of the divs ends up being greater on one side, but this could possibly be fixed by keeping track of div heights and assigning the new div to the shorter column, instead of by alternating even and odd.

Philip T.
i'd love to add an even/odd part to the loop, but at present i do not know how to do this. How's about i show you the php code for this loop, and someone could try to give me the option of an even/odd part to this?
Jim
A: 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {border:1px solid #000000; width:620px; position:relative;
}
.style2 {border:1px solid #000000; width:49%; float:left;}
.style3 {border:1px solid #f00; width:49%;margin-top:-20px;float:left;}
-->
</style>
</head>

<body>
<div class="style1">

    <div class="style2">box 1<br>
      <br>
      <br>
    </div>
    <div class="style2">box 2<br>
      <br>
      <br>
      <br>
  </div>

    <div class="style3">box 3<br>
      <br>
    </div>
    <div class="style2">box 4<br>
      <br>
      <br>
    </div>
    <div style="clear:both"></div>
</div>
</body>
</html>

try this one

coderex
it's an array, therefore i cannot modify willy nilly inside of the loop, or can i create style 3 in the loop, its all dynamic at present.
Jim
how may iterations in ur loop? This four only ? or set of 4 divs
coderex
one div is being repeated for as many as meet the criteria, and then repeated.
Jim
A: 

Another one. Probably, you can mark odd and even boxes:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {border:1px solid #000000; width:620px; position:relative;}
.style2 {border:1px solid #000000; width:300px; }


.a {float:left;}
.b {float:right;}
-->
</style>
</head>

<body>
<div class="style1">
    <div class="style2 a">box 1<br>
      <br>
      <br>
    </div>
    <div class="style2 b">box 2<br>
      <br>
      <br>
      <br>
    </div>
    <div class="style2 a">box 3<br>
      <br>
    </div>
    <div class="style2 b">box 4<br>
      <br>
      <br>
    </div>
</div>
</body>
</html>
serge_bg
this will work only when the php script allows for an odd/even loop. I'm hoping someone will help me with this as i've now posted the script and where the loop is in this on this very page.
Jim
A: 

*FOLLOW ON TO CSS ISSUE, PHP CODE *

here is my code: http://www.pcgage.net/code.zip (sorry, pasting the code caused it to really mess up, even using the code container).

Scroll to line: 160 (to 174) - this is the loop in question. If i can simply have an odd/even, that would work perfectly. For now, who ever does this, just use the current code in the div for the new odd part, i can edit this to use a different style later.

Thanks for the help, it's been fantastic so far.

Jim