views:

512

answers:

3

Hi!

I am trying to position some text relative to the prior element. So i have 4 boxes and 1 container, the first and second element works, but then the problem starts. It is supposed to be 2 rows with 2 boxes on each row.

Box1 | Box2 Box3 | Box4

But the Box3 isn't showing as it is supposed to.

http://homeweb.mah.se/~M09K0291/test.htm

In Chrome i get the text from Box 3 below the actual box and in IE the third box is behind the fourth box.

Any ideas?

+1  A: 
div.c {
clear:left;
position:relative;
top:-100px;
}

div.d { top:-200px; }
meder
But if i clear the 3rd box, the 2nd row of boxes jumps one step down, creating a gap between the first set of boxes and the second set of boxes.
Doh - I thought the gap was intended. Looks like thedz has provided what you need, I'd agree with his answer.
meder
modified since you want a position:relative solution.
meder
+1  A: 

Since you're defining explicit height and width for the boxes anyways, it's probably more clear and robust to just use absolute positioning with position: relative set on .page1.

For example:

 .page1 { position: relative; }
 .a, .b, .c, .d { position: absolute; }
 .a { top: 0; left: 0; }
 .b { top: 0; left: 100px; }
 .c { top: 100px; left: 0; }
 .d { top: 100px; left: 100px;}

See: http://jquery.nodnod.net/cases/597

EDIT:

If you don't want to use absolute positioning, or want to avoid top and left, you can also use floats and clears:

.a, .b, .c, .d { float: left; }
.a, .c { clear: left; }

You'll also need to apply float clearing for .page1 via something like clearfix.

See: http://jquery.nodnod.net/cases/598

thedz
I wanted to solve this without top and left attributes, but every time i tried removing the top and left, i got the boxes below each other.
Why do you want to solve this without top and left attributes? In any case, if you don't want to use relative or absolute positioning, put "float: left" on all the boxes, and set "clear: left" on box A and box D.
thedz
Well i am trying to understand how position:relative works, so i don't want to mix it with position:absolue.Is there a way to make the boxes appear with just the relative attribute?
See: http://jquery.nodnod.net/cases/598 for those boxes positioned with 'relative'. However, please keep in mind that for something like this, using position: relative is pretty suboptimal.
thedz
I don't that that it worked, or i might have applied it incorrectly, i uploaded the page again, but now everything is on the same line.http://homeweb.mah.se/~M09K0291/test.htm
The float solution won't work in ie6/ie7. d floats up to the first line because there is room.
Emily
Oops, you're right Emily. .page1's width would need to be set to something less than 300px. In any case, I still prefer using absolute positioning in this case.
thedz
A: 

Position relative positions the element relative to where it would normally be. The element is not taken out of the flow of the document so if you move one it will affect the positioning of the elements after it.

Position absolute positions the element relative to the containing block. The elements are taken out of the flow of the document so moving one does not affect anything else on the page.

Example using relative positoning

If you are using position relative, elements are moved with top and left.

div.fyrkant
{
    height: 100px;
    width: 100px;
    position:relative;
}
div.a
{
    background-color: #b5e274;
}
div.b
{
    background-color: #e2d974;
    left: 100px;
    top:-100px;
}
div.c
{
    background-color: #e2b074;
    top: -100px;
}
div.d
{
    background-color: #e27e74;
    top: -200px;
    left: 100px;
}

If you don't want use top and left, you should investigate using floats and clears for this problem.

Example using floats

The best cross-browser solution is going to involve adding markup to your html. Since what you want is two rows of blocks, surround each row with a div.

<div class="page1">
    <div class="row">
     <div class="a fyrkant">test a
     </div>
     <div class="b fyrkant">test b
     </div>
    </div>
    <div class="row">
     <div class="c fyrkant">test c
     </div>
     <div class="d fyrkant">test d
     </div>
    </div>
</div>

Then add to your css

div.fyrkant
{
    height: 100px;
    width: 100px;
}
.row {overflow:hidden;zoom:1;}
.a, .b, .c, .d { float: left; }

The overflow:hidden clears the floats so the .row contains the blocks. If you do not have that the rows have no height and the rows will be on the same line. The overflow:hidden does not work in ie6 but zoom:1 will (invokes haslayout)

Emily
But its sooo strange, i just added floats to Box 1 and 3 and now it works, in Internet Explorer, but not in Google Chrome.http://homeweb.mah.se/~M09K0291/test.htm
Mixing floats and relative position is going to give you nothing but trouble. As you can see, that combination is inconsistantly rendered across browsers. My first example above uses only relative positioning. The second uses only floats. Are you using firebug? http://getfirebug.com/ It is the best way to develop/debug html and css.
Emily