Performance: Position relative
Position relative affects layout stability and paint performance by creating a new positioning context without removing the element from the normal document flow.
Jump into concepts and practice - no test required
div {
position: relative;
top: 10px;
left: 10px;
}div {
position: absolute;
top: 10px;
left: 10px;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| position: absolute with offset | Removes element from flow, affects siblings | Triggers reflow for siblings | High paint cost due to layout changes | [X] Bad |
| position: relative with offset | Element stays in flow, no sibling impact | No reflow triggered | Paint and composite only | [OK] Good |
position: relative; do to an HTML element?top: 10px; moves the element 10px down relative to its normal position.top for relative movement.div {
position: relative;
left: 20px;
top: 10px;
background-color: lightblue;
width: 100px;
height: 50px;
}
What will be the visual position of the div compared to its normal spot?p element 15px down, but it doesn't work as expected:
p {
position: relative;
bottom: 15px;
}
What is the problem?bottom: 15px; moves the element up by 15px relative to its normal position.bottom moves up instead.bottom: 15px; moves the element up, not down. -> Option Aposition: relative; on both keeps layout space but allows moving the second box over the first.left: 30px; and top: 20px; moves the second box right and down overlapping the first.