CSS Position
1. Introduction to CSS Position
The position property specifies how an element is positioned in a webpage. It works together with the top, right, bottom, and left properties.
Position Values
- static
- relative
- absolute
- fixed
- sticky
.box{
position:relative;
}
2. position: static
static is the default position value. Elements are displayed in the normal document flow.
.box{
position:static;
}
| Feature | Description |
|---|---|
| Default Position | Yes |
| Moves with top/left | No |
| Normal Document Flow | Yes |
div{
position:static;
}
3. position: relative
A relative element remains in the normal document flow but can be moved from its original position using top, right, bottom, and left.
.box{
position:relative;
top:20px;
left:30px;
}
| Property | Purpose |
|---|---|
| top | Moves element downward. |
| left | Moves element to the right. |
| right | Moves element to the left. |
| bottom | Moves element upward. |
4. position: absolute
An absolute element is removed from the normal document flow and positioned relative to its nearest positioned ancestor.
.parent{
position:relative;
}
.child{
position:absolute;
top:20px;
right:20px;
}
| Feature | Description |
|---|---|
| Removed from Flow | ✔ Yes |
| Uses top/right/bottom/left | ✔ Yes |
| Positioned Relative to Parent | ✔ Yes |
5. position: fixed
A fixed element remains in the same position even when the webpage is scrolled. It is positioned relative to the browser window.
.header{
position:fixed;
top:0;
width:100%;
}
| Common Use | Example |
|---|---|
| Sticky Header | Navigation Bar |
| Floating Button | Back to Top Button |
| Chat Widget | Bottom-right Corner |
.chat{
position:fixed;
bottom:20px;
right:20px;
}
6. position: sticky
A sticky element behaves like a relative element until it reaches a specified scroll position. After that, it sticks to the viewport until its parent container is out of view.
.header{
position:sticky;
top:0;
}
| Feature | Description |
|---|---|
| Normal Flow | Yes (before sticking) |
| Sticks While Scrolling | Yes |
| Requires top/right/bottom/left | Yes |
nav{
position:sticky;
top:10px;
}
7. z-index Property
The z-index property controls the stacking order of positioned elements. An element with a higher z-index appears in front of one with a lower value.
.box1{
position:absolute;
z-index:1;
}
.box2{
position:absolute;
z-index:5;
}
| z-index | Result |
|---|---|
| 1 | Appears behind higher values. |
| 10 | Appears above lower values. |
| -1 | Moves behind other elements. |
8. Top, Right, Bottom & Left Properties
The top, right, bottom, and left properties specify the position of an element when using relative, absolute, fixed, or sticky positioning.
.box{
position:absolute;
top:20px;
left:30px;
}
| Property | Moves Element |
|---|---|
| top | Down from the top edge. |
| right | Left from the right edge. |
| bottom | Up from the bottom edge. |
| left | Right from the left edge. |
.notice{
position:fixed;
bottom:15px;
right:15px;
}
static).
9. Positioning Examples
Relative Position
.box{
position:relative;
left:40px;
}
Absolute Position
.icon{
position:absolute;
top:5px;
right:5px;
}
Fixed Position
.footer{
position:fixed;
bottom:0;
width:100%;
}
10. Practical Position Examples
Example 1: Sticky Navigation Bar
nav{
position:sticky;
top:0;
}
Example 2: Floating Chat Button
.chat{
position:fixed;
bottom:20px;
right:20px;
}
Example 3: Notification Badge
.badge{
position:absolute;
top:-5px;
right:-5px;
}
Example 4: Layering Elements
.popup{
position:fixed;
z-index:999;
}
11. CSS Position Best Practices
Proper use of CSS positioning helps create clean, responsive, and user-friendly webpage layouts.
- Use
position: relative;as the parent for absolutely positioned elements. - Use
position: absolute;for badges, icons, and tooltips. - Use
position: fixed;for floating buttons and fixed navigation bars. - Use
position: sticky;for headers and side menus. - Use
z-indexcarefully to manage overlapping elements. - Avoid unnecessary absolute positioning.
- Test layouts on desktop and mobile devices.
- Keep positioning simple for better maintenance.
12. Common Position Mistakes
| Mistake | Correct Practice |
|---|---|
| Using absolute positioning without a relative parent. | Set the parent element to position: relative;. |
| Overlapping elements accidentally. | Use z-index properly. |
| Using fixed elements everywhere. | Use fixed positioning only when necessary. |
| Ignoring responsive layouts. | Test positioning on different screen sizes. |
| Using too many position values. | Keep layouts simple and organized. |
❌ Incorrect
.child{
position:absolute;
}
✔ Better
.parent{
position:relative;
}
.child{
position:absolute;
top:10px;
right:10px;
}
13. Browser Support
CSS positioning properties are supported by all modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Position Interview Questions
- What is the purpose of the CSS
positionproperty? - What is the default value of the
positionproperty? - What is the difference between
relativeandabsolutepositioning? - When should you use
position: fixed;? - What is
position: sticky;? - What is the purpose of the
z-indexproperty? - Which properties are used to move positioned elements?
- Why should a parent element use
position: relative;? - What is the difference between
fixedandsticky? - Give three practical uses of CSS positioning.
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Position.
- ✔ position: static.
- ✔ position: relative.
- ✔ position: absolute.
- ✔ position: fixed.
- ✔ position: sticky.
- ✔ z-index.
- ✔ top, right, bottom and left properties.
- ✔ Positioning Examples.
- ✔ Practical Position Examples.
- ✔ Best Practices and Common Mistakes.
Quick Quiz
Which CSS position value keeps an element fixed at the top of the page while scrolling after it reaches a specified position?