CSS Layout - The Position Property - W3Schools

CSS The position Property ❮ Previous Next ❯

CSS Positioning

CSS positioning is about controlling the placement of elements within a web page.

With CSS positioning, you can override the normal document flow.

The CSS position Property

The position property specifies the positioning type for an element.

This property can have one of the following values:

  • static - This is default
  • relative
  • fixed
  • absolute
  • sticky

Elements are then positioned to their final location with the top, bottom, left, and right properties.

CSS position: static

All HTML elements are positioned static by default.

Static positioned elements are not affected by the top, bottom, left, and right properties.

An element with position: static; is always positioned according to the normal flow of the page:

This <div> element has position: static;

Here is the CSS that is used:

Example

div.static { position: static; border: 3px solid #73AD21;} Try it Yourself »

CSS position: relative

An element with position: relative; is positioned relative to its normal position in the document flow.

Setting the top, right, bottom, and left properties will cause the element to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

This <div> has position: relative, and is skewed 30 px to the right of its normal position

Here is the CSS that is used:

Example

div.relative { position: relative; left: 30px; border: 3px solid #73AD21;} Try it Yourself »

CSS position: fixed

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used set the final location of the element.

A fixed element does not leave a gap in the page where it would normally have been located.

This <div> element has position: fixed;

Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:

Example

div.fixed { position: fixed; bottom: 0; right: 0; width: 300px; border: 3px solid #73AD21;} Try it Yourself »

CSS position: absolute

An element with position: absolute; is positioned relative to the nearest positioned ancestor (with position other than static).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Note: Absolute positioned elements are removed from the normal document flow, and can overlap other elements.

Here is a simple example:

This <div> element has position: relative; This <div> element has position: absolute;

Here is the CSS that is used:

Example

div.relative { position: relative; width: 400px; height: 200px; border: 3px solid green;} div.absolute { position: absolute; top: 80px; right: 0; width: 200px; height: 100px; border: 3px solid red;} Try it Yourself »

CSS position: sticky

An element with position: sticky; toggles between a relative and fixed position, depending on the scroll position.

A sticky element is positioned relative until a certain scroll position is reached - then it "sticks" in that place (like position:fixed).

Note: You must specify at least one of the top, right, bottom or left properties, for sticky positioning to work.

In this example, when the sticky element reach the top of the page (top: 0), it sticks to this position:

Example

div.sticky { position: sticky; top: 0; background-color: green; border: 2px solid #4CAF50;} Try it Yourself »

Positioning Text On an Image

How to position text on an image:

Example

Cinque Terre Bottom Left Top Left Top Right Bottom Right Centered

Try it Yourself:

Top Left » Top Right » Bottom Left » Bottom Right » Centered »

All CSS Positioning Properties

Property Description
bottom Sets the bottom margin edge for a positioned box
clip Clips an absolutely positioned element
left Sets the left margin edge for a positioned box
position Specifies the type of positioning for an element
right Sets the right margin edge for a positioned box
top Sets the top margin edge for a positioned box
❮ Previous Next ❯ +1 Sign in to track progress

Tag » How To Move In Top