CSS relative vs absolute position

user3810317

I'm trying to understand the difference between relative and absolute positions in CSS. I've read the explanations and definitions of both absolute and relative, yet I still find a particular example rather strange. Can someone explain why in the following example : Here's the HTML file :

body {
  display: block;
}

.d1 {
  margin-top: 100px;
  position: relative;
  width: 100px;
  height: 100px;
  background: #815BFF;
}

.d2 {
  position: absolute;
  margin-left: 100px;
  width: 100px;
  height: 100px;
  background: #815BFF;
}

.d3 {
  position: absolute;
  margin-top: 100px;
  margin-left: 200px;
  width: 100px;
  height: 100px;
  background: #815BFF;
}
<body>
  <div class="d1">div 1</div>
  <div class="d2">div 2</div>
  <div class="d3">div 3</div>
</body>

I've posted the example on http://codepen.io/l7uci/pen/JWNrRj.

If I change the position of any div from absolute to relative, why does the div itself not change, but all the divs that come after it take it as a reference and change according to it ? I was expecting the other divs to still be placed relative to the body, as in Difference between relative and absolute .

Alohci

If you use position:absolute but don't set top, left, bottom or right, the element takes the position it would have had in normal flow, even though it is not itself in normal flow, so doesn't affect the position of subsequent elements.

So if you change an element without top, left, bottom or right from absolute to relative it doesn't move, this is it still takes it's place in normal flow, but it is now in normal flow, so subsequent elements will move to take account of its size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS: absolute position and relative position

From Dev

Relative vs Absolute in CSS

From Dev

When to use position absolute vs position relative when vertically aligning with css

From Dev

When to use position absolute vs position relative when vertically aligning with css

From Dev

Position relative element in front of absolute element CSS

From Dev

css position an absolute div in relative with a slideable menu

From Dev

HTML CSS Div overlapping absolute & relative position

From Dev

CSS position:absolute not covering position:relative text and borders

From Dev

CSS position:absolute not covering position:relative text and borders

From Dev

CSS: Relative position in absolute position in a relatively positioned div

From Java

Position absolute but relative to parent

From Dev

Absolute position relative to an image

From Dev

CSS Absolute Position Div vs P

From Dev

CSS dropdown menu with submenu absolute/relative position to other element

From Dev

How to use CSS position(relative, absolute) with percentage (height, width) dimension?

From Dev

CSS position absolute/relative change outer div alignment

From Dev

relative/absolute position css (convert area element rect coordinates)

From Dev

How to position a label using css relative to an absolute positioned input box

From Dev

Relative vs absolute address

From Dev

Position absolute and relative on one element?

From Dev

How to use absolute position as relative?

From Dev

Absolute within position relative div?

From Dev

Position absolute height of relative parent

From Dev

Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

From Dev

css - parent's position is absolute and child's position is relative and vice versa

From Dev

Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

From Dev

Difference and impact of position:relative, position:absolute, and float

From Dev

Things disappearing with position:absolute but not position:relative?

From Dev

position absolute within position relative not working correctly

Related Related

  1. 1

    CSS: absolute position and relative position

  2. 2

    Relative vs Absolute in CSS

  3. 3

    When to use position absolute vs position relative when vertically aligning with css

  4. 4

    When to use position absolute vs position relative when vertically aligning with css

  5. 5

    Position relative element in front of absolute element CSS

  6. 6

    css position an absolute div in relative with a slideable menu

  7. 7

    HTML CSS Div overlapping absolute & relative position

  8. 8

    CSS position:absolute not covering position:relative text and borders

  9. 9

    CSS position:absolute not covering position:relative text and borders

  10. 10

    CSS: Relative position in absolute position in a relatively positioned div

  11. 11

    Position absolute but relative to parent

  12. 12

    Absolute position relative to an image

  13. 13

    CSS Absolute Position Div vs P

  14. 14

    CSS dropdown menu with submenu absolute/relative position to other element

  15. 15

    How to use CSS position(relative, absolute) with percentage (height, width) dimension?

  16. 16

    CSS position absolute/relative change outer div alignment

  17. 17

    relative/absolute position css (convert area element rect coordinates)

  18. 18

    How to position a label using css relative to an absolute positioned input box

  19. 19

    Relative vs absolute address

  20. 20

    Position absolute and relative on one element?

  21. 21

    How to use absolute position as relative?

  22. 22

    Absolute within position relative div?

  23. 23

    Position absolute height of relative parent

  24. 24

    Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

  25. 25

    css - parent's position is absolute and child's position is relative and vice versa

  26. 26

    Positioning two blocks in absolute position for desktop and relative position for mobile using CSS or Jquery

  27. 27

    Difference and impact of position:relative, position:absolute, and float

  28. 28

    Things disappearing with position:absolute but not position:relative?

  29. 29

    position absolute within position relative not working correctly

HotTag

Archive