CSS Box Model Overview:

The CSS box model is a fundamental concept that describes how elements are rendered on a webpage. It comprises four main components:

  • Content: The actual content of the element, such as text, images, or other media.
  • Padding: The space between the content and the element's border.
  • Border: A border surrounding the padding and content.
  • Margin: The space outside the border, which separates the element from other elements on the page.

1. Content:
 

The content area of an element refers to the space occupied by the actual content, whether it's text, images, or other elements. It is defined by the width and height properties.


Example:
 

<!DOCTYPE html>
<html>
<head>
  <style>
    .content-box {
      width: 200px;
      height: 100px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="content-box">
    This is the content area.
  </div>
</body>
</html>
 





In this example, the <div> element with class content-box has a width of 200 pixels and a height of 100 pixels, creating the content area where text or other content can be displayed.


2. Padding:
 

Padding is the space between the content area and the element's border. It can be specified using the padding property and can have different values for each side (top, right, bottom, left).


Example:
 
<!DOCTYPE html>
<html>
<head>
  <style>
    .padding-box {
      width: 200px;
      height: 100px;
      padding: 20px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="padding-box">
    This is the padding area.
  </div>
</body>
</html>



 

Here, the <div> element with class padding-box has 20 pixels of padding on all sides, creating space between the content and the border.


3. Border:
 

The border surrounds the padding and content area and can be styled using properties like border-width, border-style, and border-color.


Example:

 

<!DOCTYPE html>
<html>
<head>
  <style>
    .border-box {
      width: 200px;
      height: 100px;
      padding: 20px;
      border: 2px solid black;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="border-box">
    This is the border area.
  </div>
</body>
</html>





4. Margin:
 

The margin is the space outside the border, which creates separation between elements. It can be specified using the margin property and also supports values for each side.


Example:

 
<!DOCTYPE html>
<html>
<head>
  <style>
    .margin-box {
      width: 200px;
      height: 100px;
      padding: 20px;
      border: 2px solid black;
      margin: 30px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="margin-box">
    This is the margin area.
  </div>
</body>
</html>



 

Here, the <div> element with class margin-box has a margin of 30 pixels on all sides, creating space around the border.


Box Model Visualization:
 

It's essential to visualize how the box model works. The total space an element occupies on the page includes the content, padding, border, and margin.
 

<!DOCTYPE html>
<html>
<head>
  <style>
    .box-model {
      width: 200px;
      height: 100px;
      padding: 20px;
      border: 2px solid black;
      margin: 30px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="box-model">
    This is the complete box model.
  </div>
</body>
</html>



 

In this example, the box-model class represents the entire box model with content, padding, border, and margin. Visually, you'll see how each component contributes to the overall size and layout of the element.



 



Practice Excercise Practice now