Css Layout
Css Layout
Css Layout
Manesh Jhawar
According to W3Schools,
“The position property specifies the type of positioning method used for an
element”
In other words, the position property can help you change the location of an
element in the document.
The different type of position properties are:
1. Static
2. Relative
3. Absolute
4. Fixed
5. Sticky
position: static
https://codepen.io/manjhawar96/pen/aYXojV
Coordinate Systems
Before we go on, we need to describe the coordinate system(s) used by a web
page. (0,0)
Uncomment properties in CSS to see how we can use offset properties to move
elements around.
3. The position: relative property also create its own coordinate system for
its child element. Now the child elements will measure it’s offset properties from
the box rather than the document (see slide 6).
Check this example where we see how relative positioning align their own child
elements:
https://codepen.io/manjhawar96/pen/pLGoBV?editors=1100
We can see that the red box calculates 40px from left side of the green-box rather
than the left end of the document.
position: absolute
1. An element with the property position: absolute is called an absolutely
positioned element, and it is removed from the normal ordering of the
elements on the document page. This means you can put it anywhere, and it
won’t affect or be affected by any other element in the flow.
2. Absolute elements are positioned relative to the nearest positioned* ancestor
element. If there are no positioned ancestors, the element positions itself
according to the outermost container of the document or, in many cases, the
document itself.
*Meaning the ancestor can not have the property position: static.
Check how position absolute works:
https://codepen.io/manjhawar96/pen/bvzNgv
https://codepen.io/manjhawar96/pen/wmNBLQ
position: sticky
https://codepen.io/manjhawar96/pen/xWMGxg
Putting it all together
Here’s an example of a using the position properties to center an image between
two containers:
https://codepen.io/manjhawar96/pen/ZxwGLW