Why an Negative Margin makes grow larger an elemente?

Why an Negative Margin makes grow larger an elemente?
0.0 0

#1

Tell us what’s happening:

Hello guys! Does anybody know why an element grow larger when has a negative value in its margin. Why element father disappear?

Your code so far

<style>
  .injected-text {
    margin-bottom: -25px;
    text-align: center;
  }

  .box {
    border-style: solid;
    border-color: black;
    border-width: 5px;
    text-align: center;
  }

  .yellow-box {
    background-color: yellow;
    padding: 10px;
  }
  
  .red-box {
    background-color: red;
    padding: 20px;
    margin: -15px;
  }

  .green-box {
    background-color: green;
    padding: 20px;
    margin: 20px;
  }
</style>

<div class="box yellow-box">
  <h5 class="box red-box">padding</h5>
  <h5 class="box green-box">padding</h5>
</div>

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

Uhm…i’m not sure about the question, but if you’re speaking about the linked challenge the yellow element is overlapped not vanished ^^
Setting the negative value of the margin with that notation will increase the size of the element in every direction ( to write margin: -15px is the same of margin: -15px -15px -15px -15px;, or even margin-left:-15px; margin-top:-15px; margin-right:-15px; margin-bottom:-15px; )


#3

Oh! Thanks. I was talking about challenge indeed. overlapped not vanished, guess I understand.


#4

Everyone needs to understand the CSS Box Model.

Then you’ll see/know why a negative margin seems to make an element “grow.”