Open In App

CSS Box Model

Last Updated : 11 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS Box Model is very important to understand how elements are structured, styled, and spaced on a webpage. By understanding the Box Model, developers can create attractive layouts that adapt seamlessly to various screen sizes and devices.

What is the CSS Box Model?

The CSS Box Model is a layout model that describes how different components of a web element (content, padding, border, and margin) are structured and positioned. Each web element generates a rectangular box that encompasses these components, and the Box Model allows developers to control the element’s size and spacing effectively.

Key Components of the Box Model

The CSS Box Model consists of four primary components:

1. Content Area

  • The content area is where the actual content, such as text, images, or other media, is displayed.
  • It is sized using the width and height properties.
  • The boundary of the content area is known as the content edge.

2. Padding Area

  • The padding surrounds the content area and creates space inside the border.
  • It can be adjusted using the padding property (or padding-top, padding-right, padding-bottom, and padding-left for individual sides).
  • The padding area increases the overall size of the element without changing the content area.

3. Border Area

  • The border wraps around the padding and content, defining the edge of the element.
  • It can be styled using properties such as border width border color, border style, and border-color.
  • The width of the border affects the overall size of the element.

4. Margin Area

  • The margin is the outermost space that separates the element from adjacent elements.
  • It can be set using the margin property (or margin-top, margin-right, margin-bottom, and margin-left for individual sides).
  • Unlike padding and bordborderse margin does not increase the element’s total size but affects its placement on the page.


The following figure illustrates the Box model in CSS.

box model property

How the Box Model Works

When setting the width and height properties for an element, we’re mainly adjusting the content area. However, to calculate the full size of the element, we need to consider padding and borders also.

While setting the width and height properties of an element with CSS, we have only set the width and height of the content area. We need to add padding and borders in order to calculate the full size of an element. Although margin affects the total area an element takes on the page but it is not considered to be a part of the actual size of the box as margins show peculiar behaviors like margin collapsing. Consider the below example.

p {
width: 80px;
height: 70px;
margin: 0;
border: 2px solid black;
padding: 5px;
}

Calculating Total Width and Height

To calculate the full size of an element, use the following formulas:

Total Width Calculation

Total element width = width + left padding + right padding + left border + right border

  • Total width of the element is 94px.
  • Total width = 80px (width) + 10px (left padding + right padding) + 4px (left border + right border) = 94px.

Total Height Calculation

Total element height = height + top padding + bottom padding + top border + bottom border

  • Total height of the element is 84px.
  • Total height = 70px (height) + 10px (top padding + bottom padding) + 4px (top border + bottom border) = 84px.

Examples of Box models in CSS

Now, We have learned the working of the CSS Box Model in-depth and now we will see Box Model examples so that we can properly understand it.

Example 1: Basic Box Model Structure

In this example we will see the use of the CSS Box model for aligning & displaying it properly.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS Box Model</title>
    <style>
        .main {
            font-size: 36px;
            font-weight: bold;
            Text-align: center;
        }

        .gfg {
            margin-left: 60px;
            border: 50px solid #009900;
            width: 300px;
            height: 200px;
            text-align: center;
            padding: 50px;
        }

        .gfg1 {
            font-size: 42px;
            font-weight: bold;
            color: #009900;
            margin-top: 60px;
            background-color: #c5c5db;
        }

        .gfg2 {
            font-size: 18px;
            font-weight: bold;
            background-color: #c5c5db;
        }
    </style>
</head>

<body>
    <div class="main">
        CSS Box-Model Property
    </div>

    <div class="gfg">
        <div class="gfg1">
            GeeksforGeeks
        </div>

        <div class="gfg2">
            A computer science portal for geeks
        </div>
    </div>
</body>

</html>


Output

box model

Example 2: Box Sizing with box-sizing

By default, the width and height apply only to the content area. The box-sizing property changes this behavior.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .main {
            font-size: 32px;
            font-weight: bold;
            text-align: center;
        }
        
        #box {
            padding-top: 40px;
            width: 400px;
            height: 100px;
            border: 50px solid green;
            margin: 50px;
            text-align: center;
            font-size: 32px;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="main">CSS Box-Model Property</div>
    <div id="box">GeeksforGeeks</div>
</body>

</html>

Output:

css box model property

CSS Box Model – FAQs

What is the CSS Box Model?

The CSS Box Model is a fundamental concept in web design that describes the rectangular boxes generated for elements in the document tree. Each box consists of four parts: content, padding, border, and margin.

What are the components of the CSS Box Model?

The components of the CSS Box Model are:

  • Content: The actual content of the box, such as text or an image.
  • Padding: The space between the content and the border.
  • Border: The edge surrounding the padding and content.
  • Margin: The space outside the border, separating the element from other elements.

How do I set the width and height of an element using the CSS Box Model?

The width and height properties set the size of the content area. The total size of the element includes the content, padding, border, and margin unless the box-sizing property is used to include padding and border in the specified width and height.

How do I add padding to an element in the CSS Box Model?

Padding can be added to an element in the CSS Box Model using the padding property. You can set padding for all sides, or individually for top, right, bottom, and left sides using padding-top, padding-right, padding-bottom, and padding-left.

How do I add a border to an element in the CSS Box Model?

Borders can be added to an element in the CSS Box Model using the border property. You can set the border width, style, and color. You can also use border-top, border-right, border-bottom, and border-left to specify borders individually.



Next Article

Similar Reads

three90RightbarBannerImg