CSS Position property
CSS Position property
CSS positioning is a crucial aspect of web design that allows you to control the layout of
elements on a webpage. There are several positioning schemes in CSS, each serving different
purposes.
/* Relative positioning */
.relative {
position: relative;
top: 10px; /* Moves down 10px from its original position */
}
/* Absolute positioning */
.absolute {
position: absolute;
top: 50px; /* 50px from the top of the nearest positioned ancestor */
}
/* Fixed positioning */
.fixed {
position: fixed;
top: 0; /* Stays at the top of the viewport */
}
/* Sticky positioning */
.sticky {
position: sticky;
top: 0; /* Becomes fixed when it reaches the top of the viewport */
}
```
### Tips
- Always consider the stacking context when using z-index.
- Use relative positioning to create a reference point for absolutely positioned child elements.
- Test across different browsers to ensure consistent behavior.
These notes should provide a solid foundation for understanding CSS positioning! If you need
more detailed examples or explanations, feel free to ask.