Ahmad Sakur

Frontend Engineer

Back

Oct 2022

CSS 101 : Position

CSS 101: Position

CSS Positioning is a fundamental concept in web development that allows you to control the layout and placement of elements on a web page. In this post, we'll dive into the various position values in CSS and how they affect the positioning of HTML elements.

The position Property

In CSS, the position property is used to specify the positioning method for an element. There are several values you can use with the position property:

1. Static

The default value for the position property is static. Elements with position: static are positioned according to the normal flow of the document. They are not affected by the top, right, bottom, or left properties.

2. Relative

When you set an element's position to relative, you can use the top, right, bottom, or left properties to adjust its position relative to its normal position in the document flow. Other elements on the page are not affected by the moved element.

3. Absolute

With position: absolute, an element is positioned relative to its nearest positioned ancestor, if any. If there is no positioned ancestor, it is positioned relative to the initial containing block. Absolute positioning removes the element from the normal document flow.

4. Fixed

Elements with position: fixed are positioned relative to the viewport. They do not move when the page is scrolled. Fixed elements are often used for things like navigation bars.

5. Sticky

position: sticky is a hybrid between position: relative and position: fixed. It is initially positioned according to the normal flow but becomes fixed when the element reaches a specified scroll position.

This is a temporary content made by AI, the content will be updated in the future.