why use negative margins?

前端 未结 4 976
天命终不由人
天命终不由人 2020-12-18 15:35

Just looking at some CSS here, and I noticed:

.head{position:relative;overflow:hidden;margin:-30px 0 0 -25px;width:820px;padding:20px 25px 0 25px;background:         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 16:26

    Actually i think there is an use case where negative margins are the only right thing to do:

    You want an part of a box to extend over the whole parent, even over the padding. So instead of removing the padding of the parent element an apply it to all children exept for your special case, you give your special case a negative margin. Also no haggling with positioning. Works great and is very readable.

    Example: http://codepen.io/anon/pen/DpHvu

提交回复
热议问题