Positioning

Positioning

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.

  1. position: The position 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 the top, right, bottom, or left properties.

    • relative: The element is positioned relative to its normal position. It can be offset using the top, right, bottom, and left 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 the top, right, bottom, and left 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 the top, right, bottom, and left properties.

  2. top, right, bottom, left: These properties are used in conjunction with the position 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 use top: 10px to move the element down by 10 pixels from its normal position. With position: absolute, the element's position can be adjusted with top, right, bottom, and left properties to position it relative to its nearest positioned ancestor.