Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position absolute not working inside position fixed

<div id="main" style="position: fixed">
   <div id="inner" style="position: absolute">
   </div>
</div>

Both main and inner containers taking position: fixed. How to make inner container with position:absolute and main container with position:fixed ?

like image 277
Shesha Avatar asked Jun 13 '16 07:06

Shesha


People also ask

Does position absolute work with position fixed?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Why does position absolute not working?

If you are placing an element with absolute position, you need the base element to have a position value other than the default value. In your case if you change the position value of the parent div to 'relative' you can fix the issue.

How do you use position absolute and fixed?

Whereas the position and dimensions of an element with position:absolute are relative to its containing block, the position and dimensions of an element with position:fixed are always relative to the initial containing block. This is normally the viewport: the browser window or the paper's page box.

How do you fix an element inside a div?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.


1 Answers

You need to define top, right, bottom or left properties when using fixed, relative or absolute positioning on an element.

#main {
  background: #000;
  position: fixed;
  height: 300px;
  width: 200px;
  left: 20px;
  top: 10px;
}
#inner {
  background: #f00;
  position: absolute;
  height: 100px;
  width: 100px;
  left: 10px;
  top: 10px;
}
<div id="main">
   <div id="inner">
   </div>
</div>
like image 181
Mohammad Usman Avatar answered Oct 11 '22 20:10

Mohammad Usman