Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does margin-right not work, but margin-left, margin-top and margin-bottom do?

Tags:

css

margin

When I set margin-right: 50px; I do not see any effect, yet when I replace the margin-right: 50px; with margin-left: 50px; or margin-top: 50px; I do see an effect. Here is the code with margin-right...

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Max Pietsch homepage</title>
        <style type="text/css">
        .me {
            margin-right: 20px;
        }
        #pic_of_me {
            width: 200px;
        }
    </style>
</head>
<body>
    <div class="me">
        <img id="pic_of_me" src="me.jpg" alt="A picture of me">
    </div>
</body>

like image 642
Max Avatar asked Sep 16 '15 02:09

Max


1 Answers

Html elements are per default always alligned at the top left corner of their parent element.

Your .me is thus placed in the top left corner of the body element.

If you add a margin-top or margin-left your .me "pushes" itself away from this corner (this is why you see it moving) .

If you add a margin-right or margin-bottom other elements on the right/below your element would be "pushed" away.

As you don't have any elements on the right/below your element you can't see this effect.

Try it out!

like image 120
Rico Ocepek Avatar answered Nov 17 '22 16:11

Rico Ocepek