Lecture

Positioning

The position property determines how an HTML element is positioned in a web page.


Static Positioning

static is the default positioning for all elements.

If you don't specify a position, the elements are automatically placed in their original order.

Example of using the static property
.box { position: static; }

Relative Positioning

relative moves an element relative to its original position.

For instance, the CSS box class below moves the element with the class 20px to the left and 10px up.

Example of using the relative property
.box { position: relative; left: 20px; top: 10px; }

Absolute Positioning

absolute positions an element relative to its nearest positioned ancestor that isn't static.

If no such ancestor exists, it will be positioned relative to the body of the web page.

Example of using the absolute property
.box { position: absolute; top: 50px; right: 30px; }

Fixed Positioning

fixed keeps an element fixed in place relative to the browser window.

This is commonly used for floating buttons or navigation bars that remain visible as you scroll through a page.

Example of using the fixed property
.box { position: fixed; bottom: 10px; left: 10px; }

Sticky Positioning

sticky allows an element to shift relative to the scroll position until it reaches a specified point, then it becomes fixed in place.

This technique is often used to keep a table header or sidebar content in view.

CSS
.box { position: sticky; top: 0; }

Follow the highlighted portions in the code to practice.

Mission
0 / 1

Which of the following describes the feature of position: fixed?

The element moves relative to its original position.

The position of the element is determined relative to the nearest positioned (non-static) parent element.

The element holds a fixed position relative to the browser window, not changing even when scrolling.

The element gets fixed when it reaches a certain point.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

HTML
CSS
Loading...