2. margin and padding #

Created Tuesday 16 June 2020

margin #

Margin is the space outside the element boundary.

padding #

This defines the space between the boundary and the contents of the element.

when to use padding/margin? #

Consider a web app made up of components, including many reusable ones.

The core code for reusable components shouldn’t bother the space outside them, so avoid using margin in them, use padding instead. See code (good):

<div style="padding: 16px;">
	...
<div>

On pages where components are integrated, i.e. reusablity of the page is assumed to be irrelevant, use margin for inter-component spaces. Example: A page has a 3 form fields one below the other. See code (good):

<form>
	<input />
	<input style="margin-top: 12px;" />
	<input style="margin-top: 12px;" />
	<input style="margin-top: 12px;" />
<form>

So its quite simple, don’t make life harder for the future/other developers, mind your own space.