The CSS Box Model

It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model:

Explanation of the different parts:

  1. Border - A border that goes around the padding and content
  2. Margin - Clears an area outside the border. The margin is transparent
  3. Content - The content of the box, where text and images appear
  4. Padding - Clears an area around the content. The padding is transparent

Example:

<!DOCTYPE html>
<html>
<head>
<style>
div {
    ackground-color: lightgrey;
    width: 300px;
    border: 15px solid green;
    padding: 50px;
    margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of: borders, padding, margins, and the actual content.</p>
<div>
The CSS box model is the foundation of layout on the Web — each element is represented as a rectangular box, with the box's content, padding, border, and margin built up around one another like the layers of an onion. As a browser renders a web page layout, it works out what styles are applied to the content of each box.
</div>
</body>
</html>

CSS - Margins

All the margin,padding,height,width properties can have the following values:

1)It's specified by px, pt, cm, etc.

2)Auto - It is calculate by browser

If the margin property has four values:

margin: 25px 50px 75px 100px;

If the margin property has three values:

margin: 25px 50px 75px;

If the margin property has two values:

margin: 25px 50px;

If the margin property has One values:

margin: 25px;