I am confused about the position of the red div (class=“marker one”). It has a margin: 10px auto, so there should be some space between the red and the black div (class=“container”) on the top, correct? But it is touching.
Can someone please explain? I tried searching online and AI help, but I’m not any wiser…
What margin in CSS does is add invisible spacing around the element it’s applied to, however this does not affect anything in the container it’s sitting in. What you’re thinking of is padding in the container. Add padding: 10px; to your .container element to see the space appear.
The thing is, the space around the red marker is there, but you can’t see it. It just spills out invisibly over the top of the .container.
Margins create space outside the element’s border, pushing other elements away. The margins push the .marker elements away from each other, but not away from the container’s edges.
I didn’t quite understand you there, but it seems to me the marker one does start inside container.
It may not appear like it, but it’s all to do with the overflow property of the container. Try setting the overflow to auto on your container styling and see your results.
What you see in shaded brown in its margin. See how it is equal above and below? That is the margin: 10px part of your marker one. See the long equal sides ? That is the auto margin part.And see how it spills over? That is because for your container, overflow is set to visible, which is the default. If you change it to auto you will get your desired result.
Are you following me? Sorry for the dense explanation.
I just thought that if I have a div inside another div and use margin-top: 10px on the inner, the inner one will “start” 10px from the top of the outer one.
I am looking on the overflow property and it is indeed solution, thanks! Not sure I understand what is “overflowing” in this case, but I will meditate on that a bit more
What is overflowing? Well, in this case, marker one. Think of it, its margin is overflowing out of the top of the outer one, right?
We’re simply changing the setting of the container’s overflow, which means we’re defining how anything that spills out of the container behaves. Make sense? I hope so.
Thanks. I think my confusion is my presumption, that the margin of the inner div will be calculated from the top of the outer div. And it isn’t. It goes above the top of the outer one.