...

View Full Version : How to avoid Negative values for top parameter in CSS



deepna
09-13-2011, 05:48 AM
Hi,

I was trying divs in CSS today and stuck with the below problem of using negative values for "top" attribute to make them look how I want them to be.

The code is as below
============================================
<html>
<head>
<style type="text/css">

body{ background-color: white; height: 600px;}

h3 {color: white;}

.Header{
position: relative;
top: 0px;
left: 150px;
border-style: ridge;
width: 970px;
height: 120px;
}

.Block1{
position: relative;
top: 0px;
left: 150px;
border-style: ridge;
width: 970px;
height: 120px;
}

.Block2{
position: relative;
top: 0px;
left: 150px;
border-style: ridge;
width: 550px;
height: 600px;
}

.Block3{
position: relative;
top: -600px;
left: 700px;
border-style: ridge;
width: 420px;
height: 120px;
}

.Block4{
position: relative;
top: -600px;
left: 700px;
border-style: ridge;
width: 420px;
height: 480px;
}

.TrailerBlock{
position: relative;
top: -600px;
left: 150px;
border-style: ridge;
width: 970px;
height: 120px;
}

</style>
</head>

<body class="HtmlBody">

<div class='Header'>



</div>
<div class='Block1'>

</div>
<div class='Block2'>



</div>
<div class='Block3'>



</div>
<div class='Block4'>



</div>
<div class='TrailerBlock'>


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

If we open this html in IE, we can see well formatted div blocks but to achieve this I had to tweak the "top" parameters with negative values of pixels, can I do this without using negative values ?

Thanks,
Deepna

vikram1vicky
09-13-2011, 09:12 AM
Sorry I dint understand, what you want to achieve and why you are using negative value???

Can you share mock up of design, how it should look like??

Please add DOCTYPE at starting of HTML code. Following is the example:


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

deepna
09-13-2011, 03:40 PM
Hi,

If you see the below div blocks, the top is set as -600px, is this the right way of defining div positions using negative pixel values and relative mode ?

.Block3{
position: relative;
top: -600px;
left: 700px;
border-style: ridge;
width: 420px;
height: 120px;
}

.Block4{
position: relative;
top: -600px;
left: 700px;
border-style: ridge;
width: 420px;
height: 480px;
}

Thanks,
Deepna

resdog
09-13-2011, 05:21 PM
when using position:relative, you are positioning the element based on it's position in the layout. So the top: 0 and left: 0 would keep it in the exact same position as it was. If you want to move it up, you have to use a negative value in the "top" and to move it further left, you have to use a negative value in the "left" field.

Apostropartheid
09-13-2011, 05:26 PM
when using position:relative, you are positioning the element based on it's position in the layout. So the top: 0 and left: 0 would keep it in the exact same position as it was. If you want to move it up, you have to use a negative value in the "top" and to move it further left, you have to use a negative value in the "left" field.

Half true.

The right and bottom properties have the same effect as negative left and top values respectively.

Rowsdower!
09-13-2011, 05:43 PM
Sorry, but why is it that relative positioning with top/bottom/left/right settings is needed at all for this layout? The only thing you need is a float or two and you can leave everything with static positioning (as it is by default). In my experience using any top/bottom/left/right setting with position relative is a recipe for disaster.

Give this a look and see what you think:

<!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" xml:lang="en" lang="en">
<head>
<title>Layout Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">

*{margin:0;padding:0;}

body{ background-color: white; height: 600px;width:976px;margin:0 auto;}

h3 {color: white;}

.header{
border-style: ridge;
width: 970px;
height: 120px;
}

.block1{
border-style: ridge;
width: 970px;
height: 120px;
}

.block2{
border-style: ridge;
width: 544px;
height: 606px;
float:left;
}

.block3{
border-style: ridge;
width: 420px;
height: 120px;
margin-left:550px;
}

.block4{
border-style: ridge;
width: 420px;
height: 480px;
margin-left:550px;
}

.trailer_block{
border-style: ridge;
width: 970px;
height: 120px;
clear:left;
}



</style>
</head>

<body class="html_body">

<div class='header'>



</div>
<div class='block1'>

</div>
<div class='block2'>



</div>
<div class='block3'>



</div>
<div class='block4'>



</div>
<div class='trailer_block'>


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

deepna
09-13-2011, 06:19 PM
Hi Rowsdower - when i look at your html in IE (Version 7.0), I can see that the block 3 and 4 are out of sync, they are not at the same level as block 2, how can we bring these blocks up to the same level as block 2?

If you see my html which i had posted earlier, the way these blocks are set is different that what comes out of your layout.

Thanks,
Deepna

alykins
09-13-2011, 06:23 PM
I am confused what you are going for as far as layout (and Rows may be as well)... this is essentially 6 blocks placed on a page... can you do a real quick paint jpg of six blocks and label what block is what and then we can help you get them in the right spot the right way ;)

Rowsdower!
09-13-2011, 06:50 PM
Hi Rowsdower - when i look at your html in IE (Version 7.0), I can see that the block 3 and 4 are out of sync, they are not at the same level as block 2, how can we bring these blocks up to the same level as block 2?

If you see my html which i had posted earlier, the way these blocks are set is different that what comes out of your layout.

Thanks,
Deepna

That's just a problem with the IE box model handling borders differently than Firefox (which is what I used when I wrote up the example). You're going to have issues like that to deal with if you truly want borders on every block of the page.

In the meantime try this instead:

<!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" xml:lang="en" lang="en">
<head>
<title>Layout Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">

*{margin:0;padding:0;border:0;}

body{ background-color: white; height: 600px;width:976px;margin:0 auto;}

h3 {color: white;}

.header{
border-style: ridge;
width: 970px;
height: 120px;
background-color:#ace;
}

.block1{
border-style: ridge;
width: 970px;
height: 120px;
background-color:#f00;
}

.block2{
border-style: ridge;
width: 550px;
height: 600px;
float:left;
background-color:#a4a4a4;
}

.block3{
border-style: ridge;
width: 420px;
height: 120px;
margin-left:550px;
background-color:#0f0;
}

.block4{
border-style: ridge;
width: 420px;
height: 480px;
margin-left:550px;
background-color:#00f;
}

.trailer_block{
border-style: ridge;
width: 970px;
height: 120px;
clear:left;
background-color:#055;
}

</style>
</head>
<body class="html_body">
<div class='header'>
<h1>Header</h1>
</div>
<div class='block1'>
<h1>Block 1</h1>
</div>
<div class='block2'>
<h1>Block 2</h1>
</div>
<div class='block3'>
<h1>Block 3</h1>
</div>
<div class='block4'>
<h1>Block 4</h1>
</div>
<div class='trailer_block'>
<h1>Trailer Block</h1>
</div>
</body>
</html>

Same idea, but a different way of showing you where the blocks are since borders are a bit dodgy to work with...

deepna
09-13-2011, 07:41 PM
Hi Rowsdower - this is exactly what i was looking for, but I dont understand how the block 4 fit in below block 3 ? instead of block 4 fitting immediately after block 3 in the same level ?

I was using top attribute to bring the block 4 below block 3.

Alykins - the html that Rowsdower has created is the format i was looking for but I was using borders to differentiate div's instead of colors.

Thanks,
Deepna

Rowsdower!
09-13-2011, 08:05 PM
Hi Rowsdower - this is exactly what i was looking for, but I dont understand how the block 4 fit in below block 3 ? instead of block 4 fitting immediately after block 3 in the same level ?

I was using top attribute to bring the block 4 below block 3....

If we were to remove block 2 from the layout - or even just to give it display:none; for now - we would see that block 3 and block 4 are still stacked one entirely above the other. There is no overlap and they don't slide in side-by-side. This is the natural behavior of block-level elements (i.e. elements with display:block). Your use of posibiton:relative with top/bottom/left/right values was disturbing the natural course of the block element behavior by artificially rolling boundaries up above where they should have been.

You can break this layout again by doing something like this, which will show you again why postion:relative with top/bottom/left/right values is not usually a good idea:

<!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" xml:lang="en" lang="en">
<head>
<title>Layout Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">

*{margin:0;padding:0;border:0;}

body{ background-color: white; height: 600px;width:976px;margin:0 auto;}

h3 {color: white;}

.header{
border-style: ridge;
width: 970px;
height: 120px;
background-color:#ace;
}

.block1{
border-style: ridge;
width: 970px;
height: 120px;
background-color:#f00;
}

.block2{
border-style: ridge;
width: 550px;
height: 600px;
float:left;
background-color:#a4a4a4;
}

.block3{
border-style: ridge;
width: 420px;
height: 120px;
margin-left:550px;
background-color:#0f0;
position:relative;
top:100px;
}

.block4{
border-style: ridge;
width: 420px;
height: 480px;
margin-left:550px;
background-color:#00f;
position:relative;
}

.trailer_block{
border-style: ridge;
width: 970px;
height: 120px;
clear:left;
background-color:#055;
}

</style>
</head>
<body class="html_body">
<div class='header'>
<h1>Header</h1>
</div>
<div class='block1'>
<h1>Block 1</h1>
</div>
<div class='block2'>
<h1>Block 2</h1>
</div>
<div class='block3'>
<h1>Block 3</h1>
</div>
<div class='block4'>
<h1>Block 4</h1>
</div>
<div class='trailer_block'>
<h1>Trailer Block</h1>
</div>
</body>
</html>

Here suppose we gave block 3 a top position to "fix" something that we wanted to fix. But now block 4 will overlap block 3, but it's not block 4's "fault" that this happens. So when you try to "fix" block 4 you end up messing up the trailer block, and then when you try to fix that... well the whole thing becomes a death spiral.

Does that help to explain it?

deepna
09-13-2011, 08:20 PM
Thanks it helps, though I need to read more on CSS to get a grip of it:(, CSS is not my forte - any good links that you suggest reading on absolute and relative positioning in CSS ?

Thanks,
Deepna

Rowsdower!
09-13-2011, 09:04 PM
I do make use of position:absolute; (and even position:fixed; from time to time). But, to be perfectly honest, I can't think of any time that I use position:relative; for anything OTHER THAN:

1) making an element contain a child element that I want to use position:absolute; on
2) giving z-index to something that needs it

Those two things are pretty much the only reasons that will ever give an element position:relative;. And even when I do that, under no circumstances do I ever use top/bottom/left/right with position:relative;. It just doesn't happen when I code and I have never found a problem that couldn't be solved a better way.

So I don't think that you have much to study up on for position:relative;. In my opionion it is just a "helper" style that allows you to make use of other CSS features. As for position:absolute; - well...

I learned by reading and playing, basically. You can always take a look here:
http://www.w3schools.com/cssref/pr_class_position.asp

and dive into the examples to see what happens when you try one thing or another. Unforunately a lot of finer points to CSS are just learning through trial and error what works in which situations.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum