CSS: Fix Element to Window (position:fixed)

By Xah Lee. Date: . Last updated: .

What is Position Fixed

position:fixed puts an HTML element into its own layer, and position it relative to the browser window.

The normal flow of elements will flow as if that element doesn't exist.

use the following CSS properties to set the offset position:

Example

You'll see a yellow box fixed to the top right of the window.

I AM FIXED HERE

Here is the code:

<div id="fixed65146">I AM FIXED HERE</div>
#fixed65146 {
position:fixed;
top:20px;
right:20px;
font-size:2rem;
color:red;
background-color:yellow;
border:solid thick red;
}

Overlapping Elements

When you have 2 elements that are both position:fixed, they may overlap. You can control which comes in front using CSS: z-index

CSS Layers