Write a program to demonstrate the CSS Box Model.

1
2
3
   <!DOCTYPE html>
4
    <html>
5
    <head>
6
    <style>
7
    div {
8
        background-color: lightgrey;
9
        width: 300px;
10
        border: 25px solid green;
11
        padding: 25px;
12
        margin: 25px;
13
    }
14
    </style>
15
    </head>
16
    <body>
17
18
    <h2> The Box Model</h2>
19
20
    <p>The CSS box model is essentially a box.</p>
21
22
    <div>This text is the actual content of the box. We have added margin, padding, border, width and background-color.</div>
23
24
    </body>
25
    </html>
26
27
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core Concept of the CSS Box Model

Every element in HTML, like div, p, h1, etc., is treated as a rectangular box. The box has four layers from inside to outside.

  1. Content: The original content, which is presented inside, like text, image, video, or anything (in this example, 300px)
  2. Padding: The space between the content and the border.
  3. Border: The rectangular boundary around the padding, like 25px solid green. You can make the border rounded by using border-radius: 25px, where the pixel value or a value in % like 2% or 50% depends upon your requirement.
  4. Margin: The outside space of the border is 25px.

Why use it?

To control the layout, spacing, and alignment of any element, this CSS box model is used.With the help of the CSS box model, we can decide the spaces or gaps between the contents, which improves the visibility and readability of the page or website.

Real-Life Applications

  • Cards and containers: To show product cards on e-commerce websites like Amazon or Flipkart, or any other website like dhimanp, margin, padding, and border are used.
  • Layout Design: To properly frame sections like the header, sidebar, footer, etc.
  • Buttons and Navigation Bars: To add padding inside the buttons and to maintain the margin from other elements.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.