Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div always top using css?

Tags:

css

Like alert bar of stackoverflow here.

It should not be position:fixed,because it's not supported by all browsers.

Absolutely,neither will position:absolute do.

like image 635
cssnewbie Avatar asked Jan 26 '10 03:01

cssnewbie


2 Answers

You could always use EMCAscript or one of its forms (JScript, JavaScript) to calculate the position of the viewport and set the position equal to that.

function positionView()
{
  return (window.pageYOffset) ?
    window.pageYOffset :
    (document.documentElement.scrollTop) ?
      document.documentElement.scrollTop :
      document.body.scrollTop;
}

function setPosition(object)
{
  object.style.top = positionView() + "px";
}

Then just pass in the DIV object you want to use, with document.getElementById.

I'd use position: fixed;. Many people still use IE6, though, which does not support it.

like image 179
Patrick Niedzielski Avatar answered Sep 23 '22 13:09

Patrick Niedzielski


Put the div under the body tag, give it position absolute, top:0, left:0 And if you want it to push the content, just put it there without the CSS I gave you.

like image 41
Itay Moav -Malimovka Avatar answered Sep 20 '22 13:09

Itay Moav -Malimovka