views:

33

answers:

3

I can't seem to float DIV's correctly. What i am trying to achieve is being able to position 4 DIV's next to each other. Each aligned to the top at 0px, and spaced apart by 30 or so pixels, but i cannot use absolute positioning. The size of the DIV's are adjusted according to screen size. So, i need to position the DIV's relatively. When i try to float the DIV's they just sit atop each other.

Here is what i have thus far.

<head>
<style type="text/css">
.b1{
 position:relative;
 margin-left:50px;
 float:left;
 width:1000px;
 height:200px;
 background-color:#000;
}
.b4{
 position:relative;
 top:0px;
 left:30px;
 float:right;
 width:1000px;
 height:200px;
 background-color:#000;
}
</style>
</head>

<body>
<div class="b1"></div><div class="b1"></div><div class="b1"></div><div class="b4"></div>
</body>
A: 

Floated the elements will stack when their container isn't wide enough to align them horizontally. Put the four divs inside a container that's wide enough for all of them to fit (presumably 4000px + whatever margin and padding you want on each). In your question, you mention 30px, but your example uses a 50px left margin. Here's an example with a 50px left margin. http://jsfiddle.net/brianflanagan/9ZUw5/

Brian Flanagan
A: 

The problem is your viewport. When you float a series of elements, they will wrap to the next line if the viewport is not wide enough to display them on one line. To see what I mean, decrease the width of .b1 to 100.

You can add a container div if you really want this that can scroll horizontally.

<div class="container"> <div class="b1"></div><div class="b1"></div><div class="b1"></div><div class="b4"></div> </div>

With the parent container style of:

overflow: scroll;
Mike
A: 

Add a wrapper and make the wrapper large enough to fit your DIVs:

<div class="wrapper">
   <div class="b1"></div><div class="b1"></div><div class="b1"></div><div class="b4"</div>
</div>


.wrapper {
    width:3500px;
}
Diodeus