65

That guilty feeling i get when I use negative margins

Comments
  • 0
    Yes, I agree with you.
  • 1
    We've all been there.
  • 2
    Sometimes . You just have to
  • 6
    I feel the same anytime I use !important
  • 0
    The only time I use negative margin is center aligning an absolute element.

    {
    position:absolute;
    width:400px;
    left:50%;
    margin-left:-200px;
    }

    Although I hear there is a better way of doing it.
  • 0
    A second case for negative margin or the use of !important is when editing someone else's code. God does it drive me mad.
  • 0
    @uziiuzair
    Setting a element to center

    .containElement {
    Width:500px;
    Height:500px;
    Position relative;
    }
    .centeredele {
    Margin:auto;
    Left:auto;
    Right:auto;
    Top:auto;
    Bottom:auto;
    }

    The centerele must have width or height defined to center if you just want vertical don't use left or right if you want horizontal only don't use top bottom

    There is obviously flex now as well..

    You should never use, !important

    But in a rush and your working on someone code... It understandable. But literally no other reason
  • 0
    @FitzSuperUser that's one way of doing it, however I believe it will not work in the case of an overlay element say a pop up or a login box.

    I should've clarified the use of my code too hahaha
  • 0
    @uziiuzair no you can you just define Percentages
  • 1
    @FitzSuperUser ah yes, sorry my bad. I had something else in mind and then read your code again to figure it out!
  • 3
    Yes you should, I turn into the hulk when I see that. Just do left 50% and transform: translatex(-50%) Or better yet: flexbox ftw
  • 1
    @uziiuzair I should say the element inside needs position absolute
Add Comment