Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
A second case for negative margin or the use of !important is when editing someone else's code. God does it drive me mad.
-
@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 -
@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 -
@FitzSuperUser ah yes, sorry my bad. I had something else in mind and then read your code again to figure it out!
-
NickdeK528yYes you should, I turn into the hulk when I see that. Just do left 50% and transform: translatex(-50%) Or better yet: flexbox ftw
Related Rants
That guilty feeling i get when I use negative margins
undefined
css