Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute and fixed positioning together

As you can see in this page: http://pitchfork.com/ , there are some audio elements on the right side. I've inspected them and they seem to have absolute positioning. But if you scroll down, you'll see that they are fixed.

How can achieve this behavior? Can be an element Absolute and Fixed positioned?

like image 513
Manolo Avatar asked Jan 17 '14 16:01

Manolo


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.

How absolute relative static and fixed positions are different from each other?

Static - this is the default value, all elements are in order as they appear in the document. Relative - the element is positioned relative to its normal position. Absolute - the element is positioned absolutely to its first positioned parent. Fixed - the element is positioned related to the browser window.

When would you want to use absolute positioning?

When you need something to be positioned in an very specific spot you would use absolute positioning. For instance you may want to have an image with an overlapping caption that always sits at the top of the picture (say 20px from the top).

Does margin auto work with position absolute?

just use margin: auto; . with absolute position, all other style elements(regarding position/etc) are ignored.


2 Answers

This is the only way I've found: like @DreamTek said:

<div id="relative-layer">
    <div id="fixed-layer">
    </div>
</div>

and in the styles file:

#relative-layer {
    position:relative;
}

#fixed-layer {
    position: fixed;
    margin-top: 10px;
    margin-left: 10px;
}

because using top and right rules positions the layer relative to the window, but if using margin-top and margin-left it is positioned relative to the parent layer.

JSFIDDLE: http://jsfiddle.net/9HQ4b/1/

like image 81
Manolo Avatar answered Jan 05 '23 06:01

Manolo


Create a fixed scrolling sidebar with no JavaScript and a few lines of CSS.

The fixed div in the fiddle below appears to be positioned relative to the container but this is just an illusion.

It can be achieved using percentage widths or by using fixed widths and the setting a negative margin relative to the container width.

FLUID WIDTH

.wrap {
  background: #ccc;
  width: 90%;
  height: 1000px;
}

.fixed {
  position: fixed;
  top: 10px;
  right: 0;
  background: #333;
  height: 100px;
  width: 10%;
}
<div class="wrap">WRAP</div>
<div class="fixed">FIXED</div>

FIXED WIDTH

.wrap {
  background: #ccc;
  width: 200px;
  height: 1000px;
  margin: 0 auto;
}

.fixed {
  position: fixed;
  top: 20px;
  right: 50%;
  background: #333;
  height: 100px;
  width: 50px;
  margin-right: -160px;
}
<div class="wrap">WRAP</div>
<div class="fixed">FIXED</div>

A note about CSS positioning.

FIXED

Element is always positioned relative to the screen.

ABSOLUTE

Element is positioned relative to the nearest parent container with a position attribute.

like image 41
DreamTeK Avatar answered Jan 05 '23 07:01

DreamTeK