I have a question the way the code was written in this lesson

Tell us what’s happening:
In the lesson Basic CSS: Adjust the Margin of an Element.

The question is on the red highlighted section of the codes h5 to div.

I am wondering why the text “margin” is injected into the yellow box(div-/div)?

Since we want the text “margin” to be inside the yellow box. Why its not instead written within the element

(the yellow box)?

Is there a reason?

Many thanks

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: crimson;
  color: #fff;
  padding: 20px;
  margin: 20px;
}

.blue-box {
  background-color: blue;
  color: #fff;
  padding: 20px;
  margin: 10px;
}
</style>

***<h5 class="injected-text">margin</h5>***

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Adjust the Margin of an Element

Link to the challenge:
https://www.freecodecamp.org/learn/responsive-web-design/basic-css/adjust-the-margin-of-an-element[quote=“Hisoka, post:1, topic:380316, full:true”]

Hi,

I don’t think there’s any particular reason other than showing what margin attribute can do. In and ideal situation, you would write it inside the yellow box, but I think here is just another example of what it could do.

Hope this helps.

hmmmm. do you think so? I would like to hear what others would say about it. Because as you said ideal situation i would write the code for margin text inside the div.

Maybe its another example as you said.

but i am wondering maybe it was done like that because it would not effect the appearance.

I changed the codes and placed the margin text inside the yellow box. I just needed to add a selector style for the margin text to be aligned center and adjust its margin to be closer to the upper border. Then played around with the red and blue box margin and padding size. I didnt see any problem.

Logically the code for the margin should be in div (yellow box). As you and i have noticed maybe it was just to show that you can play with text as well.

Not sure.

By not putting the h5 inside the yellow div, that text is not part of the contents of the div. The point of this exercise is to show the effects of margin on an element, so the blue and red divs fill 100% of the yellow div. Putting additional text in it would throw off the effect.

1 Like

It is just an easier way to achieve the desired layout.

The negative margin makes sure it does not interact with the elements inside the box (so they don’t push on the element but on the box). It’s also compensating for the default margin on the body (pulling the box up) but whether or not that was the intention or just a side effect I can’t say.

Here is the same layout with the element inside the box (the negative margin is pulling the box up a bit more than just removing the margin on the body).

<style>
  body {
    margin-top: 0 !important;
  }

  .injected-text {
    margin: 0;
    position: absolute;
    top: 5px;
    left: 50%;
    transform: translateX(-50%);
  }

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

  .yellow-box {
    background-color: yellow;
    padding: 10px;
  }

  .red-box {
    background-color: crimson;
    color: #fff;
    padding: 20px;
    margin: 20px;
  }

  .blue-box {
    background-color: blue;
    color: #fff;
    padding: 20px;
    margin: 10px;
  }
</style>

<div class="box yellow-box">
  <h5 class="injected-text">margin</h5>
  <h5 class="box red-box">padding</h5>
  <h5 class="box blue-box">padding</h5>
</div>
1 Like

Yep. you are right. When i placed margin text inside the box it shifted the red and blue box.

But if i do it this way as shown below? When i play with the pixels of red and blue i don’t see any damage done.

let us say in future, in higher level codes. what would be better? For the margin to be inside the box or outside? Or it doesnt matter unless you get the effect you want? Will it effect the speed?

Changed codes


<style>
  .***injected-text {***
***    margin: -11px;***
***    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: crimson;
  color: #fff;
  padding: 20px;
  margin: 20px;
}

.blue-box {
  background-color: blue;
  color: #fff;
  padding: 20px;
  margin: 10px;
}
</style>



<div class="box yellow-box">
***<h5 class="injected-text">margin</h5>***
<h5 class="box red-box">padding</h5>
<h5 class="box blue-box">padding</h5>
</div>

Well, you now your red box and blue box are h5 elements instead of divs, which is probably not what you would want in the real world.

This isn’t a situation that translates well to real use cases.

Speed really isn’t the issue with HTML. It’s about getting it to look and behave the way you want it to in every scenario, in a way that doesn’t require fussing every time you update something.

1 Like

Some time is happen because it get auto in setting. When mention Margins then you have to check its lengths, percentages, or the keyword .

Hi

Thanks. Sorry for the late reply. I lost internet connection for 2 days. it was like walking in the desert without water :sweat_smile:.

I was going through your code. Cool commands you included. i had to check them out. Which made me learn new stuff. Many thanks :smiley:

Few Querstions:
Why you place top-margin: 0 important! for the body?

Can you please explain the styling for the injected-text that you used.
.injected-text {
margin: 0;
position: absolute;
top: 5px;
left: 50%;
transform: translateX(-50%);
}

What you think about the code below? Its is a simple code i made . Which ArielLeslie replied to Link to the Post

The codes:

<style>

  .injected-text {

    margin: -11px;

    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: crimson;

  color: #fff;

  padding: 20px;

  margin: 20px;

}

.blue-box {

  background-color: blue;

  color: #fff;

  padding: 20px;

  margin: 10px;

}

</style>

<div class="box yellow-box">

<h5 class="injected-text">margin</h5>

<h5 class="box red-box">padding</h5>

<h5 class="box blue-box">padding</h5>

</div>

  1. Because the original code pulls the box up, negating the top margin on the body. So instead I just removed it as I’m not pulling the box up.

  2. Some code comments.

.injected-text {

/* remove default margin */
  margin: 0;

/* remove the element from document flow so it does not interact
 * with other elements and can be positioned using offsets */
  position: absolute;
  
/* position it 5px from the top to have the same position as in the original code */
  top: 5px;
  
/* center the element horizontally */
  /* left of element box positioned at 50%, i.e. center of parent */
  left: 50%;
  /* pull the element back by 50% of its own width to center the element box */
  transform: translateX(-50%);
}

Here is a pen to help visualize the centering
https://codepen.io/lasjorg/pen/bGVYbwQ

  1. With your code, the element’s margins are still interacting. It really depends on the desired layout and how flexible it has to be. Personally, if I had to add a label to a box and it wasn’t meant to interact with other elements inside the box, I would use absolute positioning to take the element out of document flow.
1 Like

Thanks for the tones of information. I will have to read over it couple of times to get to understand how the codes works precisely.

therefore its like a formula on centering elements.

Thanks a mill :smiley: :smiley: :smiley: