# CSS Box Model & Display Property

## What is CSS Box Model

The CSS Box Model is a fundamental CSS concept that describes how every HTML element is represented as a rectangular box. it consists of four parts: `Content` `Padding` `Border` and `Margin`.

It helps us control the size, spacing and layout of HTML elements on a webpage.

**Diagram**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/6c794bce-c7f8-4751-a781-88c4c3aa4645.png align="center")

Let discuss all the parts one by one

1. **Content**

- The actual text, image or other content inside a element.
- `width` and `height` generally control the content area by default.

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/ef8061ea-560e-42d1-8cee-b71a580f9c11.png align="center")

You can see in the Browser Preview Picture here `width` and `height` of the content is the by default. Margin is also by default you can change it with the help of universal selector. let see

**CSS File**

```css
 *{
    margin: 0;
    padding:0;
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/c49b3ea2-fe79-481a-80af-f8bbcc24bffb.png align="center")

2. **Padding**

- The space between content and the border.
- Create inner spacing around the content.

Here we set the padding 20px.

**CSS File**

```css
 .box{
    border: 2px solid red;
    background-color: bisque;
    height: 300px;
    width: 300px;
    padding:20px;
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/30724c97-08c8-448d-a53a-614071e1c7fd.png align="center")

In this picture arrow shows the padding area.

You can also give specific padding using `padding-top` `padding-bottom` `padding-right` `padding-left` properties.

Syntax of shorthand notation is:

`padding : top / right / bottom / left`

3. **Border**

- The boundary surrounding the content and padding
- Create inner spacing around the content.

Here we set border 20px

**CSS File**

```css
.box{
    border: 20px solid red;
    background-color: bisque;
    height: 300px;
    width: 300px;
    padding:20px;
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/aa315ac8-a05f-4536-a535-35b545e18aec.png align="center")

This arrow shows the border area.

4. **Margin**

- The space outside the border.
- Creates distance between an element and other elements.

Here we set the margin 20px.

**CSS File**

```css
 .box{
    border: 2px solid red;
    background-color: bisque;
    height: 300px;
    width: 300px;
    margin: 20px;
    padding:20px;
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/b7e78bd0-5578-4390-aa1c-a14f8b920b6d.png align="center")

You can also give specific margin using `margin-top` `margin-bottom` `margin-right` `margin-left` properties.

Syntax of shorthand notation is:

`margin : top / right / bottom / left`

| Values | Applied sides |
| --- | --- |
| 1 value | All four sides |
| 2 values | Top & Bottom , Left & Right |
| 3 values | Top , Left & Right , Bottom |
| 4 values | Top , Right , Bottom , Left |

Shorthand notation is exactly the same for both `margin` and `padding`

# Display Property

## **What is Block Level Element**

A block-level element is an HTML element that starts on a new line and occupies the full available width of its parent container by default even if its content does not require that much space.

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div>Welcome to chaicode</div>
<div>Software dev Cohort</div>


</body>
</html>
```

**CSS File**

```css
div{
    border: 2px solid red;
    background-color: bisque; 
    height: 50px;
    width:600px;  
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/98a96652-df2f-46b5-8a5f-740f286421ae.png align="center")

- It respect the given custom height and width

**CSS File**

```css
div{
    border: 2px solid red;
    background-color: bisque; 
    height: 50px;
    width:600px;  
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/ecc1341d-b8cd-4c35-8fd7-32578685c3b9.png align="center")

- It also respect the given custom margin and padding.

**CSS File**

```css
 div{
    border: 2px solid red;
    background-color: bisque; 
    height: 50px;
    width:600px;  
    margin: 25px;
    padding: 50px;
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/0b2a7ed4-a6af-4869-aa60-8bba01c2827b.png align="center")

Some commonly used block-level elements are:

- `<div>`
- `<p>`
- `<h1> to <h6>`
- `<section>`
- `<header>`
- `<footer>`
- `<article>`
- `<ul>`
- `<ol>`
- `<li>`

## What is Inline elements

Inline elements are HTML elements that do not start on a new line. They occupy only as much horizontal width as their content requires and allow other elements to appear alongside them on the same line.

**HTML File**

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<span>Welcome to chaicode</span>
<span>Software dev cohort</span>
</body>
</html>
```

**CSS File**

```css
span{
    border: 2px solid red;
    background-color: bisque;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/80e3664a-b4be-4c50-8350-1e83d2ff319a.png align="center")

It does not respect the custom height and width.

**CSS File**

```css
span{
    border: 2px solid red;
    background-color: bisque;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/64825fa5-8613-474e-9cf5-8425ec36bd1c.png align="center")

It did not change the height and width.

It apply only horizontal margin not vertically. But padding you can apply.

**CSS File**

```css
span{
    border: 2px solid red;
    background-color: bisque;
    margin: 20px;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/79cf2742-57fa-4528-a6d4-e45577852c59.png align="center")

**CSS File**

```css
span{
    border: 2px solid red;
    background-color: bisque;
    margin: 20px;
    padding: 30px;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview of padding**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/a6b9e78b-953c-4128-b36c-cd5fb1d64059.png align="center")

Some commonly used inline elements are:

- `<span>`
- `<a>`
- `<strong>`
- `<b>`
- `<i>`
- `<em>`
- `<img>`
- `<label>`

# What is `display` Property

This property in CSS is used to define how an HTML element is displayed on webpage and how it behaves in the layout. It controls whether an element behaves as a block, inline, flex container, grid container or other display type. like `display:inline` `display:block` `display:inline-block`

`inline-block` behaves like an inline but you can add custom `height width margin padding`.

# `box-sizing:border-box` property

This property tells the browser to include an element's content , padding and border within its specified width and height.

Let check box size without applying this property and padding

**CSS File**

```css
*{
    margin: 0;
    padding:0;
}
 .box{
    border: 2px solid red;
    background-color: bisque;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/6dbaaa74-9d83-46ce-94d7-7dce05d41ad6.png align="center")

Box Size is 403 x 153.2

Now check the size of box using `padding`

**CSS File**

```css
 .box{
    border: 2px solid red;
    background-color: bisque;
    padding: 20px;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/437ffcbf-7b18-4f22-adb9-d55910d6ca29.png align="center")

Box Size is Increased.

now check the difference using `box-sizing` and `padding` together

**CSS File**

```css
 .box{
    border: 2px solid red;
    background-color: bisque;
    box-sizing: border-box;
    padding: 20px;
    height: 150px;
    width:400px;     
 }
```

**Browser Preview**

![](https://cdn.hashnode.com/uploads/covers/6a670b7b711c6f3b57c87574/a0f4d604-a2e6-43f3-abbb-5c0b84a4a0f9.png align="center")

now check it does not increase the box size. It include content in specified height and width.

## Conclusion

The CSS Box Model is an important concept in web development that helps us understand how HTML elements are sized, spaced, and positioned on a webpage. It consists of four main components: Content, Padding, Border, and Margin. We also learned about block-level and inline elements, the `display` property, and how `box-sizing: border-box` helps control the overall dimensions of an element. Understanding these concepts makes it easier to create well-structured layouts, manage spacing, and design responsive websites efficiently.
