Connecting Tech Pros Worldwide Forums | Help | Site Map

Locking an element (div) to the top or bottom of the browser window.

Member
 
Join Date: Apr 2008
Posts: 45
#1: Sep 3 '09
Gmail does this when it says "Sending.." or "working.." even when
you scroll down the page the little box stays top and center.
Perfectly smooth.

www . comodo . com does this, their logo in the bottom right.

How is this done?

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,670
#2: Sep 3 '09

re: Locking an element (div) to the top or bottom of the browser window.


position: fixed;

or some Javascript if IE
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#3: Sep 3 '09

re: Locking an element (div) to the top or bottom of the browser window.


position:fixed works in IE 7 and 8 now :)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,670
#4: Sep 3 '09

re: Locking an element (div) to the top or bottom of the browser window.


ah, so they finally did it? ;)
Member
 
Join Date: Apr 2008
Posts: 45
#5: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


This is my css/html. Works perfectly in FF. In IE the z-index is ignored and when I add things to the UL it pushes things down.

Expand|Select|Wrap|Line Numbers
  1. <div id="added-to-cart" style="height: auto; position:fixed; top:0px; left:0px; width: 100%; display:none; z-index: 250"><ul id="cart-added-list" style="z-index: 250"></ul></div>
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#6: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


Could you please elaborate on "pushes things down"?
Please note that the z-index will only work for elements that have an position:absolute (or I position:relative too).....
Member
 
Join Date: Apr 2008
Posts: 45
#7: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


In firefox the ul expands the div that its in. it just overlays whats underneath it.
In IE rather than lay on top of content it disobeys the z-index and moves the content down as items are added to it.
Member
 
Join Date: Apr 2008
Posts: 45
#8: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


Hmmm in FF position fixed work with z-indicies.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#9: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


Ok I think I experienced something similar once...where elements with a style of position:relative were appearing on top of elements with a style of position:fixed or position:absolute.

To fix this I had to move the position:fixed element under the position:relative element in the HTML.

For example, this wouldn't work:
Expand|Select|Wrap|Line Numbers
  1. <div style="position:fixed"></div>
  2. <div style="position:relative"></div>
  3.  
But this would:
Expand|Select|Wrap|Line Numbers
  1. <div style="position:relative"></div>
  2. <div style="position:fixed"></div>
  3.  
You can move the position:fixed element to the appropriate place on the page using CSS styles....
Member
 
Join Date: Apr 2008
Posts: 45
#10: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


More markup than I wanted, but it works :) Thanks for your help!

Expand|Select|Wrap|Line Numbers
  1. <div style="position:absolute; left:0px; top:0px; z-index: 200"><div id="added-to-cart" style="height: auto; position:fixed; top:0px; left:0px; width: 100%; display:none; z-index: 250"><ul id="cart-added-list" style="z-index: 250"></ul></div></div>
Member
 
Join Date: Apr 2008
Posts: 45
#11: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


I was mistaken. I should have known. This loses the fixed position part of it that I needed. so it behaves as it should, but it stays at the top of the page rather than the top of the browser window.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#12: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


You shouldn't wrap the position:fixed div in a position:absolute div...this is probably why you're having problems......
Member
 
Join Date: Apr 2008
Posts: 45
#13: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


makes sense. But fixed stll doesn't do what I think ti should in IE. Instead of putting it at (0,0) its putting it on the page where it is in the HTML.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#14: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


I just tested it and it's positioned at 0,0 for me in IE (8):
Expand|Select|Wrap|Line Numbers
  1. <div id="added-to-cart" style="height: auto; position:fixed; top:0px; left:0px; width: 100%; z-index: 250">
  2.   <ul id="cart-added-list">
  3.     <li>abc</li>
  4.   </ul>
  5. </div>
Member
 
Join Date: Apr 2008
Posts: 45
#15: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


Even when I paste your code and take mine out... it still shows up inline rather than 0,0

Could doc type affect this?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#16: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


What?
I don't know about the doctype...maybe.
But I think it's more likely to be some other style that you've applied on the page?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#17: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


This is the basic layout of what my page was when I was testing:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.   <head>
  4.     <title></title>
  5.   </head>
  6.   <body>
  7.     <!--............tons of my stuff ...........-->
  8.     <div id="added-to-cart" style="height: auto; position:fixed; top:0px; left:0px; width: 100%; z-index: 250">
  9.       <ul id="cart-added-list">
  10.         <li>abc</li>
  11.       </ul>
  12.     </div>
  13.   </body>
  14. </html>
Member
 
Join Date: Apr 2008
Posts: 45
#18: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


You're most likely right... I'll experiement

my code def works if I make a new document. hmmm
Member
 
Join Date: Apr 2008
Posts: 45
#19: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


I don't knwo what it was exactly, but I strted running my document through a validator and fixing the errors, and it started working after I fixed the first few! Thanks ! :)
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,168
#20: Sep 4 '09

re: Locking an element (div) to the top or bottom of the browser window.


Haha :)
Yeah fixing your code so that it's valid is tedious, and can sometimes be very annoying and frustrating; but it usually does the trick.
Reply