Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

CSS Box Model

In CSS, the term "box model" is used when talking about design and layout.


CSS Box Model

The CSS box model is essentially a box that wraps around every HTML element. It consists of: content, padding, borders and margins.

The CSS box model is a foundational concept in web design and layout that defines how elements are structured and how their dimensions and spacing are calculated.


1. Components of he Box Model

Each element in CSS is essentially a rectangle box, which consists of the following parts (from the inside out):


1. Content

  • The innermost part of the box where text and images appear.
  • The size of this area can be adjusted using properties like width and height.

2. Padding

  • The space between the content and the border.
  • Padding is transparent and can be set using the padding property. It increases the space around the content but inside the border.

3. Border

  • A line surrounding the padding (or content, if padding is not specified).
  • The border can be styled, colored, and adjusted in thickness using properties like border-width, border-style, and border-color.

4. Margin

  • The outermost layer that creates space between the element’s border and surrounding elements.
  • Margins are transparent and can be adjusted using the margin property.

2. Box Model Properties

  • Width and Height: Define the dimensions of the content area.
  • Padding: Can be set for each side individually (padding-top, padding-right, padding-bottom, padding-left) or for all sides simultaneously using padding.
  • Border: Controlled using border, border-width, border-style, and border-color. You can also set borders for individual sides (e.g., border-top).
  • Margin: Similar to padding, margins can be specified for each side or all sides.

3. Box-Sizing property

By default, the width and height properties only include the content area. To include the padding and border in the total width and height of an element, you can use:

box-sizing:border-box;

  • box-sizing: content-box;(default)- The width and height only include the content.
  • box-sizing: border-box; The width and height include content, padding and border.
  • Content-size:200 px by 100 px
  • Total width:200 +20(left padding) + 20(right padding) + 5(left border) + 5(right border)=250px
  • Total height:100 + 20 (top padding) + 20 (bottom padding) + 5 (top border) + 5 (bottom border) = 150px

Key points of CSS Box Model

1. Components:

  • Content: The area where text, images, or other content is displayed. Dimensions are controlled by width and height.
  • Padding: Space between the content and the border, making the content area larger. It is transparent.
  • Border: A line surrounding the padding and content, which can be styled in terms of color, thickness, and type.
  • Margin: Space outside the border, creating distance between elements. It is also transparent.

2. Total Size Calculation:

  • The total size of an element = content + padding + border + margin.

3. Box-Sizing Property:

  • box-sizing: content-box; (default): Width and height only include the content.
  • box-sizing: border-box;: Width and height include content, padding, and border, making size calculations easier.

4. Padding and Margin:

  • Can be set for all sides at once or individually (e.g., padding-top, margin-left).