Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Negative Margin To Remove Parent's Padding

Is it a good practise to use negative margins to remove padding of wrapper element?

For example, which of the following code pieces is better to use?

<div style="padding: 5px;">
 Shortened width string
 <div style="margin: 0 -5px;">Full width string</div>
 Shortened width string
</div>

or

<div>
 <div style="padding: 5px;">Shortened width string</div>
 <div>Full width string</div>
 <div style="padding: 5px;">Shortened width string</div>
</div>
like image 662
Denis Bobrovnikov Avatar asked Nov 05 '22 06:11

Denis Bobrovnikov


1 Answers

Why not just declare padding:5px 0; so you don't have horizontal padding? Though I would argue that it's perfectly fine to use negative margins, that's what they're made for but if you can avoid them in the first place, do so.

like image 145
meder omuraliev Avatar answered Nov 11 '22 04:11

meder omuraliev