Absolute Positioning

Would someone please explain to me how absolute positioning works?

I have the following dropdown submenu…


<ul id="nav">
	<li class="current"><a href="#">News</a>
		<ul>
			<li><a href="#">Breaking</a></li>
			<li><a href="#">Local</a></li>
			<li><a href="#">U.S.</a></li>
			<li><a href="#">World</a></li>
		</ul>
	</li>
</ul>

I am unclear what the various positioning labels mean and how they work.


#nav li ul{
	position: absolute;
	width: 106px;
	left: 0;
	top: 100%;
}

1.) What does top: 100% mean and do?

2.) What does top: 0% mean and do?

3.) What does top: 0 mean and do?

These three all seem to do the same thing, which is properly display the dropdown submenu. :-/

4.) What does bottom: 100% mean and do?

5.) What does bottom: 0% mean and do?

6.) What does bottom: 0 mean and do?

These seem to do different things.

Thanks,

Debbie

For starters, elements with Absolute Positioning are completely removed from the normal page flow. Other elements are completely blind to the fact that the AP element is there. The AP element will hide content of the in flow element when it lays on top of it. That also depends on whether or not other elements have positioning applied to them as that will have an effect on [URL=“http://reference.sitepoint.com/css/stacking”]Stacking Contexts.

In the case of a dropdown menu we want the AP element to lay on top of other elements. But if you are using absolute positioning on something else like a logo then you need to protect other elements from being hidden by it. Usually a margin or padding will do the trick to push something away from the AP element. Generally speaking though, the only time you want to use absolute positioning is when something NEEDS to be removed from the flow so it does not push around on other in flow elements. That is why it is used for dropdowns, if we did not use AP on the dropdown it would push the rest of the page down.

1.) What does top: 100% mean and do?
2.) What does top: 0% mean and do?
3.) What does top: 0 mean and do?

These three all seem to do the same thing, which is properly display the dropdown submenu. :-/
top:100% sets the top of the AP box at the very bottom of the containing block.

top:0 and top:0% are the same thing “zero”, and that sets the top of the AP box at the top of the containing block.

4.) What does bottom: 100% mean and do?
5.) What does bottom: 0% mean and do?
6.) What does bottom: 0 mean and do?

These seem to do different things.
bottom:100% behaves the same as top:0; you would think it might give the opposite behavior of top:100% but that is not the case. It sets the top of the AP box at the top of the containing block.

bottom:0 and bottom:0% are the same thing “zero”, and that sets the bottom of the AP box at the bottom of the containing block.

Here is an example of all of those conditions. The Parent divs are set with fluid heights and they are the “containing block” since position:relative; has been applied to them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>AP Elements</title>

<style type="text/css">
body {
    margin:0;
    padding:0;
    font: 100%/1.3 arial,helvetica,sans-serif;
    text-align:center;
}
h1,h2 {font-size:1.5em;margin:0}
h2{font-size:1.2em}
p {margin:1em;}

.wrap {
    position:relative; /*establish containing block for AP children*/
    width:400px;
    display:inline-block;
    vertical-align:top;
    margin:20px 20px 100px;
    background:#CDF;
}
.ap {
    position:absolute;
    padding:20px;
    right:0;
    top:0;
    background:lime;
    display:none;/*hide .ap div*/
}
    .top-100 {
        top:100%;/*override top:0*/
    }
    .bot-0 {
        top:auto;/*null out top:0*/
        bottom:0;
    }
    .bottom-100 {
        top:auto;/*null out top:0*/
        bottom:100%;
    }
.wrap:hover .ap {display:block} /*reveal .ap div on :hover*/
</style>

</head>
<body>
<h1>Hover on Blue Parent Divs to show AP Child Divs</h1>
<div class="wrap">
    <h2>AP Child has top:0</h2>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <div class="ap">
        <p>AP with top:0</p>
    </div>
</div>

<div class="wrap">
    <h2>AP Child has top:100%</h2>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <div class="ap top-100">
        <p>AP with top:100%</p>
    </div>
</div>

<br>

<div class="wrap">
    <h2>AP Child has bottom:0</h2>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <div class="ap bot-0">
        <p>AP with bottom:0</p>
    </div>
</div>

<div class="wrap">
    <h2>AP Child has bottom:100%</h2>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <div class="ap bot-100%">
        <p>AP with bottom:100%</p>
    </div>
</div>

</body>
</html>

one thing to add I would avoid using absolute positioning unless it’s absolutely necessary. It can cause a lot of headaches if not used correctly.

I need to make a correction with that comment about bottom:100%; It does behave opposite of top:100%

I had a typo in the class name I was using in both the CSS and HTML.

My correction would read as this -

bottom:100% behaves opposite top:100%; It sets the bottom of the AP box at the top of the containing block.


.ap {
    position:absolute;
    padding:20px;
    right:0;
    top:0;
    background:lime;
    display:none;/*hide .ap div*/
}
    [COLOR=Blue].bot-100[/COLOR] {
        top:auto;/*null out top:0*/
        bottom:100%;
    }
<div class="wrap">
    <h2>AP Child has bottom:100%</h2>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <p>Parent with fluid height: Parent with fluid height</p>
    <div class="ap [COLOR=Blue]bot-100[/COLOR]">
        <p>AP with bottom:100%</p>
    </div>
</div>

Yes, I knew that, but thanks for the refresher.

top:100% sets the top of the AP box at the very bottom of the containing block.

Not the most intuitive.

top:0 and top:0% are the same thing “zero”, and that sets the top of the AP box at the top of the containing block.

That makes sense.

bottom:100% behaves the same as top:0; you would think it might give the opposite behavior of top:100% but that is not the case. It sets the top of the AP box at the top of the containing block.

Who dreams up these conventions?! :rolleyes:

bottom:0 and bottom:0% are the same thing “zero”, and that sets the bottom of the AP box at the bottom of the containing block.

That makes sense, although you might think it sets the Top of the AP box at the bottom of the containing block as well…

(*NOTE: One reason I was getting confused is that often I make a change in my code but my browser mysteriously caches the old page again and again and so when I load the changed code I see the effects of the old code and that throws me off. That was happening last night when I made this post.)

Here is an example of all of those conditions.

That is an AWESOME example!!!

(My hero!!) :cheer:

Thanks Rayzur!!!

Debbie

make sure you are holding F5 to clear your cache. :slight_smile:

Sure it is, the offset values get their coordinates from the containing block’s dimensions. In that case it’s setting the top value according to the containing block’s calculated height. By using top:100% we are assured that the AP box will always find the containing block’s height, even when that height is dynamic.

[QUOTE]bottom:100% behaves the same as top:0; you would think it might give the opposite behavior of top:100% but that is not the case. It sets the top of the AP box at the top of the containing block.
Who dreams up these conventions?! :rolleyes:[/QUOTE]You seem to have overlooked the correction in my last post. :wink:

Here is the example with the corrections posted above.
http://www.css-lab.com/misc-test/ap-top-bot.html

EDIT:

I changed the width of the containing block’s to 40%.
Reduce your viewport width and that will cause their heights to increase. That shows you that top:100% is intuitive, it finds the parent height even when it is dynamic.

I do shift + refresh.

I disagree. :disagree:

Top 0: Top of AP box at Top of Containing Block. (Makes sense.)

Top 0%: Top of AP box at Top of Containing Block’s height. (I guess I can buy that.)

Top 100%: Top of AP box at Bottom of Containing Block’s height. (I guess I can buy that.)

Bottom 0: Bottom of AP box at Bottom of Containing Block. (Makes sense.)

Here is the inconsistency…

Bottom 100%: Top of AP box at Top of Containing Block. (Makes NO sense!!)

[b]For symmetry, it should be…

Bottom of AP box at Top of Containing Block (i.e. opposite end - 100% - of the Bottom)[/b] (Bottom 100% should logically create a Drop-UP box…)

Dumb dumb dumb.

Sloppy convention, but whatever!

You seem to have overlooked the correction in my last post. :wink:

No, I just think the convention CSS2 uses is illogical.

Debbie

Here is the inconsistency…

Bottom 100%: Top of AP box at Top of Containing Block. (Makes NO sense!!)

Dumb dumb dumb.

Sloppy convention, but whatever!

How many times do I need to tell you to read my corrections in post#4? :frowning:

Yes, if there should be symmetry you’re right. But the whole point is that there is no symmetry. If top and bottom were truly symmetric, it wouldn’t make a lot of sense to have both, would it?

I always view it like this:

bottom: n = move the element n from the bottom of its parent
top: n = move the element n from the top of its parent

Same goes for left and right.

I do Tools>Clear Recent History in FireFox but even that doesn’t always work…

Debbie

To nitty gritty for me. The browser tells me my logic. If it works in all it’s logical.

You keep changing things…

I looked at your changes - and as they stood - responded accordingly.

(Said constructively)

It is better to put changes in a new post, because people aren’t going to keep re-reading every post in a thread again and again to look for changes,

I saw your 1st set of changes, but didn’t realize you made a 2nd round of changes.

As your 1st set of corrections stood, I thought things were counter-intuitive.

Now that you have made more changes, things behave as I would expect.

Debbie

Funny what an errant percent (%) symbol can do to your CSS, huh? :lol:

I carefully read over your updated updated code and compared it to the code I had and finally saw that extra (%) that you took out.

Whew! Glad we got that straightened out. (For once, things behave the way you’d expect!!)

Thanks Rayzur.

Debbie

That’s wrong.

See Rayzur’s 2nd update.

With the right code, things behave exactly as I would have expected, and there is complete symmetry.

Debbie

top: 0 and bottom: 100% are the same, but top: 100% and bottom: 0 are not the same
So no, there is no symmetry.

No they are not the same.

See Rayzor’s updated code and URL above.

This is one time where there is symmetry.

Debbie

Ah yes I got confused. My bad :blush:

I think you’re gonna owe me dinner after this or something… :wink:

In Post #2 you had this style…

[B].bottom-100[/B] {
    top:auto;/*null out top:0*/
    bottom:100%;
}

and you had this in your HTML…

&lt;div class="ap [B]bot-100%[/B]"&gt;

Then in Post #4 you had this style…

[B].bot-100[/B] {
    top:auto;/*null out top:0*/
    bottom:100%;
}

and I recall your HTML changing from…

<div class=“ap bot-100%”>

to

<div class=“ap bot-100”>

but it doesn’t really matter. :stuck_out_tongue:

Debbie