Understanding CSS position property | CSS interview Question.
CSS positioning is an important aspect of web development because it enables developers to control the position and appearance of elements on a page. In this article, we'll look at the various CSS positioning methods and how to use them to create visually appealing and responsive layouts for your webpages.
The different positioning methods in CSS are:
- Static positioning
- Relative positioning
- Fixed positioning
- Absolute positioning
- Sticky positioning
Static positioning
For the HTML markup part we will need the following : This is the default positioning method. Elements with static positioning are not affected by the top, bottom, left, and right properties. They are positioned according to the normal flow of the document.
Relative positioning
Elements with relative positioning are positioned in the document relative to their normal position. The element's position can be adjusted using the top, bottom, left, and right properties.
To use relative positioning, set the position property to "relative" and specify the element's position using the top, bottom, left, and right properties. As an example:
This would position the element with the class "relative-element" in the document 100 pixels down and 200 pixels to the right of its normal position.
Relative positioning is useful for changing an element's position without removing it from the normal document flow. This means that the element will still take up space on the page and will affect the position of other elements.
It should be noted that the top, bottom, left, and right properties are relative to the element's normal position in the document, not to the parent element or the first containing block. This means that rather than being moved to an absolute position on the page, the element will be moved in relation to its original position.
Fixed positioning
Elements with fixed positioning are positioned relative to the browser window and stay in place even when the page is scrolled.
To use fixed positioning, set the position property to "fixed" and specify the element's position using the top, bottom, left, and right properties. As an example:
The "fixed-element" div would be positioned 100 pixels from the top and 200 pixels from the left of the browser window. Even if the page is scrolled, the element will remain in this position.
Fixed positioning can be used if you want to place an element fixed in a place of viewport, like a header or footer that always appears at the top or bottom viewport. However, that fixed positioning is removed from the normal document flow, which means it takes up no space and has no effect on the position of other elements. This can make creating responsive layouts with fixed positioning a bit challenging.
Absolute positioning
This can a be bit confusing one, but hang in there. Absolute positioned elements are positioned relative to the nearest positioned ancestor (if any) or the initial containing block (if no ancestor is positioned). If no positioned ancestor exists, the element is positioned relative to the first containing block.
Ex: If a absolute div is placed in a relative / any other positioned div, the position properties will be set WRT the div in which our absolute div is placed.
To use absolute positioning, set the position property to "absolute" and specify the element's position using the top, bottom, left, and right properties. As an example:
In the above example we can see, the blue box is placed absolute to its parent which is the document in this case and the yellow box is placed absolte to its relatively positioned parent which is the orange box.
If we remove the position "relative" from the orange box, the yellow box will be placed according to its nearest positioned parent i.e the document.
It's important to note that absolute positioning is removed from the normal document flow, which means that it does not take up any space and does not affect the position of other elements. This can be useful for creating overlays or for positioning elements precisely on a page.
However, absolute positioning can also make it difficult to create responsive layouts, as the element's position is fixed and does not adjust when the page is resized. In these cases, it may be necessary to use a combination of absolute and relative positioning to create a responsive layout.
Sticky positioning
The "sticky" position value allows an element to be "fixed" within an area when its parent is scrolled, but unlike fixed elements, sticky elements are still part of the normal document flow. This can be useful for creating elements that behave like a fixed header or footer but are not removed from the document flow.
To use the "sticky" position value, first set the position property to "sticky," then use the top, bottom, left, or right properties to position the element within its parent element. For instance:
When the parent element is scrolled, the element with the class "sticky-element" is fixed to the top of the parent.
It is important to note that the "sticky" position value is not supported by all browsers, so you may need to use a polyfill or fallback to a different positioning method in older browsers.
Aside from the position property, there are several other properties that can be used to fine-tune an element's positioning. The z-index property, which determines element stacking order, and the float property, which allows elements to be floated to the left or right of the page. We will see more about this is the coming articles.