move an element into its own layer and fix its position to the viewport. : position fixed « Layout « HTML / CSS

move an element into its own layer and fix its position to the viewport.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style type="text/css" media="Screen">
* .grandContainer {
  position: relative;
  z-index: 1;
  width: 800px;
  height: 800px;
  background: red;

#in-place {
  position: fixed;
  z-index: 1; width 600px;
  height: 600px;
  background: yellow;

#shrinkwrapped {
  position: fixed;
  z-index: 0;
  width: auto;
  left: 0;
  bottom: 0;
  margin: 0;
  background: blue;

#sized {
  position: fixed;
  z-index: auto;
  width: 170px;
  height: 115px;
  bottom: 0;
  left: 270px;
  margin: 0;
  background: gold;

#stretched {
  position: fixed;
  z-index: -1;
  height: auto;
  right: 0;
  top: 0;
  bottom: 0;
  margin: 0;
  background: pink;
<div class="grandContainer"><h2>Positioned Grandparent</h2> 
  <div class="parent">
    <h2>Non-positioned Parent</h2> 
    <span id="in-place" class="box">In-place Absolute</span> 
    <span id="sized" class="box">Sized Absolute</span> 
    <p id="stretched" class="box">Stretched Absolute</p> 
    <p id="shrinkwrapped" class="box">Shrinkwrapped Absolute</p>


Related examples in the same category

1.Elements with a fixed position are always positioned relative to the browser's viewport, not in a document's structure.
2.position:fixed positions an element at an offset from the viewport.
3.Elements with a fixed position stay in place, even when a document is scrolled.
4.Using Fixed Positioning
5.Fixed position and static position
6.position fixed: top left
7.watermark with position fixed
8.Fixed top, footer and left bar
9.Position:fixed header
10.Absolute and fixed positioning use left, right, top, bottom, and z-index to control the alignment of the absolute box.
11.Fixed block with width and height to bottom and top
12.fixed block to top and left
13.Fixed block to top and right
14.Fixed block to bottom and left
15.Fixed block to bottom and right
16.position-fixed offset: left top