What are the types of Positioning in CSS?
What are the types of Positioning in CSS?
In CSS (Cascading Style Sheets), there are four primary types of positioning:
-
Static Positioning: This is the default positioning behavior, where elements are placed in the normal flow of the document. They are not affected by the
top
,right
,bottom
, orleft
properties or by other positioning properties. -
Relative Positioning: Elements with relative positioning are moved from their normal position in the document flow by using the
top
,right
,bottom
, orleft
properties. However, they still occupy space in the normal flow, and other elements are not affected by their new position. -
Absolute Positioning: Elements with absolute positioning are removed from the normal flow and positioned relative to their nearest positioned ancestor (an ancestor with a positioning property other than
static
). They do not affect the layout of other elements. -
Fixed Positioning: Elements with fixed positioning are also removed from the normal flow, but they are positioned relative to the viewport (the browser window). They remain in the same position even when the page is scrolled.
These positioning types allow you to precisely control the layout and placement of elements on a web page.
The position property in CSS tells about the method of positioning for an element or an HTML entity and the positioning of an element can be done using the top, right, bottom, and left properties. These specify the distance of an HTML element from the edge of the viewport.
There are five different types of position properties available in CSS:
- Fixed position: Any HTML element with position: fixed property will be positioned relative to the viewport. An element with fixed positioning allows it to remain in the same position even we scroll the page. We can set the position of the element using the top, right, bottom, and left.
- Static: This method of positioning is set by default. If we don’t mention the method of positioning for any element, the element has the position: static method by default. By defining Static, the top, right, bottom, and left will not have any control over the element. The element will be positioned with the normal flow of the page.
- Relative: An element with position: relative is positioned relatively with the other elements which are sitting on top of it. If we set its top, right, bottom, or left, other elements will not fill up the gap left by this element. An element with its position set to relative and when adjusted using top, bottom, left, and right will be positioned relative to its original position.
- Absolute: An element with position: absolute will be positioned with respect to its nearest Non-static ancestor. The positioning of this element does not depend upon its siblings or the elements which are at the same level.
- Sticky: Element with position: sticky and top:0 played a role between fixed & relative based on the position where it is placed. If the element is placed in the middle of the document then when the user scrolls the document, the sticky element start scrolling until it touched the top. When it touches the top, it will be fixed at the top place in spite of further scrolling. we can stick the element at the bottom, with the bottom property.
- 14 Forums
- 1,837 Topics
- 5,053 Posts
- 0 Online
- 1,079 Members