Macfael
KPCKevin Powell - Community
•Created by Macfael on 4/1/2024 in #front-end
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
3 replies