tags:

views:

80

answers:

3

Hi,

i want to position 4 divs next to each other, at a certain position within the parent div. So the div structure is this (i'll call the top most div the maindiv):

<div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
</div>

now i want to set div with text '1' at the left in the maindiv

now i want to set div with text '2' at 50 px from the left side in the maindiv

now i want to set div with text '3' at 150 px from the left side in the maindiv

now i want to set div with text '4' at 200 px from the left side in the maindiv

I think :-) i have tried ALL possible css constructs, but it didn't work, sadly.

Anyone an idea?

+7  A: 

Give position: relative to the main div

position: absolute to the children and position them using left: xyz; top: xyz .

Pekka
please, please... tell me why this works (because it does!)it is in the 'relative', because when i delete that, the inner divs are placed at the top of the page. I've mad a pge with a table an d underneath this div with the innerdivs, and it's all fine. I can copy paste the div/innerdivs and they appear correct too. But when i delete the 'position:relative', the innerd vis (not the outer!!) are placed at the top of my page (Ie8 and FF3). That, dear sir, can not be figured out by me :). Can you explain why this behaviour exists? to be clear: the answer is correct, just want to understand why.
Michel
@Michel absolutely positioned elements are always positioned relatively to the *next parent element* with either `position: relative` or `position: absolute` (and I think, also `position: fixed`). Basic theory here: http://www.w3.org/TR/CSS2/visuren.html#absolute-positioning if the parent div is not `position: relative`, the absolute positioning is relative to the topmost element, the body. Thus, `left: 50px` will then actually mean 50 pixels from the document's left hand corner.
Pekka
Thanks very VERY much for your comment.
Michel
+1  A: 

Hope this would help you

                <div> 
                    <div style="float: left;">1</div> 
                    <div style="float: left; margin-left: 50px;">2</div> 
                    <div style="float: left; margin-left: 100px;">3</div> 
                    <div style="float: left;margin-left: 50px;">4</div> 
                </div>
KhanS
a bit, but not exactly.
Michel
+1  A: 

try this:

 div div
      {
          float:left;
          margin-left:50px;
      }

EDIT:as in: Fix for the parent:

div
{
    float:left;
    margin-left:-50px;
}
div div
{
    float:left;
    margin-left:50px;
}
Mark Schultheiss
NOTE: this moves the first div child also, so you could put a -50px margin on the parent if that is an issue.
Mark Schultheiss