Why margin right isn't working as expected?
I was experimenting with negative margins and came across something that I don't quite understand.
If I apply a negative left margin to an element, this causes it to move to the left of its container (div.inner1), but why doesn't the same thing happen with the right margin (div.inner2)?
However, I have found that if I also apply a left margin auto to the element that has a negative right margin, it seems to work as expected. (div.inner3)
If no fixed width is set on the child elements, the negative margins make them grow outwards and this seems to be the normal behavior (div.inner4 and div.inner5).
Can you help me understand why this happens with the negative right margin?
Of course, there are many other ways to position these elements offset from their container, but I was curious to better understand the negative margins and how they work.
Thank you very much for your help.
css
css
2 Replies
You might want to take a look at this article: https://www.quirksmode.org/blog/archives/2020/02/negative_margin.html
Towards the top it states:
It is possible to give margins a negative value. This allows you to draw the element closer to its top or left neighbour, or draw its right and bottom neighbour closer to it.Further on in the article it gives more explanations and examples.
Thank you, @Chris I'll take a loot at it.