Table of contents
Introduction
In CSS, positioning refers to the techniques used to control the placement and layout of elements on a web page. Several CSS properties are commonly used for positioning, including position
, top
, right
, bottom
, and left
.
position
: Theposition
property is used to define the positioning behavior of an element. It can take the following values:static
(default): The element follows the normal document flow and is not affected by thetop
,right
,bottom
, orleft
properties.relative
: The element is positioned relative to its normal position. It can be offset using thetop
,right
,bottom
, andleft
properties, but it still occupies its original space in the document flow.absolute
: The element is positioned relative to its nearest positioned ancestor ( provided its ancestor is relatively positioned ) (or the initial containing block if no ancestor is positioned). It is taken out of the normal document flow, and its position can be adjusted using thetop
,right
,bottom
, andleft
properties.fixed
: The element is positioned relative to the viewport, meaning it remains fixed in its position even if the page is scrolled. It is taken out of the normal document flow, and its position can be adjusted using thetop
,right
,bottom
, andleft
properties.
top
,right
,bottom
,left
: These properties are used in conjunction with theposition
property to specify the offset or position of an element. They define the distance between the element's edge and the respective edge of its containing element or the viewport.For example, with
position: relative
, you can usetop: 10px
to move the element down by 10 pixels from its normal position. Withposition: absolute
, the element's position can be adjusted withtop
,right
,bottom
, andleft
properties to position it relative to its nearest positioned ancestor.