position — CSS Technical Interview Questions & Answers
static, relative, absolute, fixed and sticky.
position
- static, relative, absolute, fixed and sticky.
- position is a CSS property: it changes how a selected element looks or sits. You write it inside a rule: .card { position: …; }
- Change one value, refresh, say what moved: colour, space, size, or stacking. That is how you learn properties — not by memorising the spec.
- Trap: putting it on the wrong selector (parent vs child). Trap: px only on a phone page — learn rem/%/flex too when it is layout.
- Box model reminder: width/height, padding, border, margin are different. If position is about space, draw the box.
Technical Interview Tip: position: one rule on .card, then say what changed.
Code Examples for position
position — working example
.card {
position: relative; /* containing block for absolute children */
}
.badge {
position: absolute;
top: 0.5rem;
right: 0.5rem;
z-index: 2;
}
.header {
position: sticky;
top: 0;
z-index: 10;
}
.toast {
position: fixed;
right: 1rem;
bottom: 1rem;
}position — absolute uses the nearest positioned ancestor; fixed uses the viewport; sticky toggles within flow.
How to Explain position to an Interviewer
What does position do?
A CSS property that changes look/layout Write it in a rule and refresh