CSS | Box model

CSS box model is a container which contains multiple properties including borders, margin, padding and the content itself. It is used to create the design and layout of web pages. It can be used as a toolkit for customizing the layout of different elements. The web browser renders every element as a rectangular box according to the CSS box model.
Box-Model has multiple properties in CSS. Some of them are given below:

  • borders
  • margins
  • padding
  • Content

The following figure illustrates the box model.
box model property

Border Area: It is the area between the box’s padding and margin. Its dimensions are given by the width and height of border.

Margin Area: This area consists of space between border and margin. The dimensions of Margin area are the margin-box width and the margin-box height. It is useful to separate the element from its neighbors.

Padding Area: It includes the element’s padding. This area is actually the space around the content area and within the border box. Its dimensions are given by the width of the padding-box and the height of the padding-box.



Content Area: This area consists of content like text, image, or other media content. It is bounded by the content edge and its dimensions are given by content box width and height.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE 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>                    

chevron_right


box model

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE 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>

chevron_right


Output:
css box model property



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.