Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.
SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
Ravi Vishwakarma
05-Jun-2024CSS provides various positioning schemes that allow you to control the layout and placement of elements on a web page. The primary positioning schemes are
relative,absolute,fixed, andsticky. Here’s an overview of each, including their characteristics and use cases:Relative Positioning
position: relative;positions an element relative to its normal position in the document flow.top,right,bottom, andleftproperties are used to adjust the element’s position relative to where it would have been placed normally.Example:
In this example, the element will be moved 10 pixels down and 20 pixels to the right from its original position.
Absolute Positioning
position: absolute;positions an element relative to its nearest positioned ancestor (i.e., an ancestor with a position other thanstatic), or if none exists, relative to the initial containing block (usually the viewport).top,right,bottom, andleftproperties specify the position.Example:
In this example, the
.absolute-elementis positioned 10 pixels down and 20 pixels to the right from the top-left corner of the.container.Fixed Positioning
position: fixed;positions an element relative to the viewport, meaning it stays in the same position even when the page is scrolled.top,right,bottom, andleftproperties specify the position.Example:
In this example, the
.fixed-elementwill be fixed to the top-right corner of the viewport and remain there even when the page is scrolled.Sticky Positioning
position: sticky;is a hybrid of relative and fixed positioning. An element withposition: sticky;toggles between relative and fixed positioning, depending on the user's scroll position.top,right,bottom, andleftproperties specify the sticky thresholds.Example:
In this example, the
.sticky-elementwill act like a relatively positioned element until the user scrolls past it, at which point it will stick to the top of the viewport.Use Cases
By understanding and combining these positioning schemes, you can create complex and responsive layouts that enhance the user experience on your web pages.