margin
and padding
properties.Set the margin in an element
An element has four sides: right, left, top and bottom. Themargin
is the distance from each side to the neighboring element (or the borders of the document). See also the diagram in lesson 9 for an illustration.As the first example, we will look at how you define margins for the document itself i.e. for the element
<body>
. The illustration below shows how we want the margins in our pages to be.The CSS code for this would look as follow:
Or you could choose a more elegant compilation:
show example
You can set the margins in the same way on almost every element. For example, we can choose to define margins for all of our text paragraphs marked with
<p>
:show example
Set padding in an element
Padding can also be understood as "filling". This makes sense as padding does not affect the distance of the element to other elements but only defines the inner distance between the border and the content of the element.The usage of padding can be illustrated by looking at a simple example where all headlines have background colors:
show example
By defining padding for the headlines, you change how much filling there will be around the text in each headline:
show example