The difference between relative and absolute

I am reading this article about position , and I don’t understand why in this example a relatively positioned div affects BODY, but an absolutely positioned block ignores it?
Shouldn't they behave the same when they are located inside another element?

CSS:

body {
     display: block;
     margin: 8px;
}

#box_1 { 
     position: relative;
     width: 200px;
     height: 200px;
     background: #ee3e64;
}
#box_2 { 
     position: absolute;
     top: 0;
     left: 100px;
     width: 200px;
     height: 200px;
     background: #44accf;
}
+5
source share
5 answers

Basically, you have four position states that look like this:

  • static (default)
  • regarding
  • fixed
  • absolute

, "" (left:15px 15px), ( ) (left: 15px) , 15 .

-.

+4

absolute . , CSS. relative - CSS

0

: : .

html . - , body, . .

'div2' div1.

<div id='div1' style="position: relative; left: 100px;top: 10px;"> 
<h1>This is the first position element</h1>
<div id='div2' style="  position: absolute;left: 100px;top: 150px;">
<h2>This is a heading with an absolute position</h2>
</div>
</div>

"div2" body, .

  <div id='div1'> 
    <h1>This is the first position element</h1>
    <div id='div2' style="  position: absolute;left: 100px;top: 150px;">
    <h2>This is a heading with an absolute position</h2>
    </div>
    </div>

html  position.Below - .

10 10 .

<div id='div2' style="  position: relative;left: 10px;top: 10px;">
<h2>This is a heading with an absolute position</h2>
</div>
0

:

, /// , , . ( , , ).

, , , .

-1

, , , . .

: . , .. , div :

margin-left:10px;

, 10px; , : : ;

margin-left:10px;

, . div : : ;

margin-left:10px;

10 + 10 = 20px margin-left. 10px div 10 px div id. :

#div1{
float:left;
margin-left:10px;
} 
#div2{
float:left;
margin-left:10px;
}
-1

All Articles