Skip to content

Commit 0382967

Browse files
committed
Improve CSS box model example
1 parent 27af827 commit 0382967

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

CSS/03.css-box-model.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
# [CSS Box Model](https://www.w3schools.com/css/css_boxmodel.asp)
22

3-
The CSS box model consists of four parts: margin, border, padding, and content. By default, the width and height of an element only include the content, but with `box-sizing: border-box`, it also includes padding and border.
3+
The CSS box model is an essential concept in web design, defining the structure of every HTML element on a page. It consists of four parts: margin, border, padding, and content. The `box-sizing` property plays a key role in controlling how these elements are sized.
44

5-
**HTML Example:**
5+
By default, an element's width and height only include the content. However, with `box-sizing: border-box`, these dimensions also encompass the padding and border, which is crucial for predictable layout behavior.
6+
7+
**Example: Creating Three Cards Side by Side**
8+
9+
To illustrate this, let's create an example with three cards displayed side by side using `inline-block`. Each card will have its own padding, border, and margin, showcasing their effect on the overall layout.
10+
11+
**HTML Structure:**
612

713
```html
8-
<div class="box-model-example">Content</div>
14+
<div class="card">Card 1</div>
15+
<div class="card">Card 2</div>
16+
<div class="card">Card 3</div>
917
```
1018

11-
**CSS Example:**
19+
**CSS Styling:**
1220

1321
```css
14-
.box-model-example {
15-
width: 200px;
16-
padding: 20px;
17-
margin: 10px;
18-
border: 5px solid black;
22+
.card {
23+
width: calc(33.3% - 10px);
24+
height: 100px;
25+
padding: 10px;
26+
border: 1px dotted tomato;
27+
color: tomato;
28+
display: inline-block;
1929
box-sizing: border-box;
30+
box-shadow: 5px 15px 5px 2px #eaeaea;
31+
border-radius: 4px;
32+
}
33+
.card:nth-child(1),
34+
.card:nth-child(2) {
35+
margin-right: 5px;
2036
}
2137
```
2238

23-
This sets a box with a total width of 200px, including padding and border.
39+
In this example, each `.card` is styled with `display: inline-block`, which allows the cards to sit side by side. The `box-sizing: border-box` ensures that the padding and border are included in the total width and height of each card. The margin creates space between the cards. This example is a simple yet effective way to demonstrate how the box model's components interact and affect an element's dimensions and spacing on a webpage.

0 commit comments

Comments
 (0)