HTML elements are kept inside a box. This page discusses two of the important components of a box: padding and margin. Padding refers to the space outside the content but within the border. Margin refers to the outer-most space of the border but within the outer boundary of the box.
The following figure shows the scope of these two parameters (padding and margin). The figure also shows four sides for both padding and margin (top, right, bottom, and left).

Let us now see two examples that highlight the usage of these two properties. For both
of these examples, we use the same HTML and simply update the linked CSS file. The
HTML file (shown below) is fairly simple and consists of two paragraphs.
<!doctype html>
<html>
<head>
<title> Learning CSS: Box Borders </title>
<link type="text/css" rel="stylesheet" href="padding_margin.css">
</head>
<body>
<p id="id_jurassic">
Jurassic period was one of the three main periods of Mesozoic Era. Jurassic
period is further divided into three sub-periods: early Jurassic, middle
Jurassic, and late Jurassic. This period was a significant period for dinosaurs
and other reptiles. They became the most dominant and the most diverse vertebrae
during this period.
</p>
<p id="id_reptiles">
Reptiles ruled the earth during this period, Dinosaurs were the most
dominant race on the land. Notable among them were the Theropods -- the dinosaurs
that hunted on two legs and were mostly carnivores. Reptiles were also the most
dominant race in the ocean. Two such reptiles were Plesiosaurs and Ichthyosaurs.
And when it came to sky, the supreme flying creature was once again another
reptile: Pterosaurs.
</p>
</body>
</html>
The linked CSS file ("padding_margin.css") specifies different padding values for the above two paragraphs. Here it is.
#id_jurassic {
border-style: solid;
border-width: thin;
padding: 16px;
}
#id_reptiles {
border-style: solid;
border-width: thin;
padding: 40px;
}
The output of the above HTML page shows different padding for the two paragraphs. By the way, we keep the border-width to illustrate the padding since the padding sits beneath the border.

Our second example shifts the focus to the margin property and specifies different margins for the two paragraphs. We reuse the earlier HTML file but simply change the CSS file ("padding_margin.css").
#id_jurassic {
border-style: solid;
border-width: thin;
margin: 16px;
}
#id_reptiles {
border-style: solid;
border-width: thin;
margin: 40px;
}
The output (see below) shows that the two paragraphs have different margins -- recall that the margin is the outer space beyond the border.

The above two properties (padding and margin) also come in more specific flavors! These variants apply to different four sides of the box. For the sake of completeness, we provide them here.