Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,570 --> 00:00:04,950
The CSS box model is one of the most fundamental
2
00:00:04,950 --> 00:00:07,060
parts of CSS.
3
00:00:07,060 --> 00:00:09,440
And so you really need to understand it
4
00:00:09,440 --> 00:00:11,600
in order to learn CSS.
5
00:00:11,600 --> 00:00:15,543
And so that is exactly what we will do in this video.
6
00:00:17,070 --> 00:00:20,380
Now, what exactly is the box model?
7
00:00:20,380 --> 00:00:24,740
Well, the box model defines how elements are displayed on a
8
00:00:24,740 --> 00:00:27,840
webpage and how they are sized.
9
00:00:27,840 --> 00:00:29,650
So in this box model,
10
00:00:29,650 --> 00:00:33,780
each and every element on a webpage can be seen as a
11
00:00:33,780 --> 00:00:38,730
rectangular box and each of these boxes can have content,
12
00:00:38,730 --> 00:00:43,320
a border and space inside and outside of it.
13
00:00:43,320 --> 00:00:45,960
So let's define all of the pieces of the
14
00:00:45,960 --> 00:00:50,960
box model one by one, and starting with the content
15
00:00:51,030 --> 00:00:54,820
This is the actual content of the element.
16
00:00:54,820 --> 00:00:58,879
So it can be text or images or a table,
17
00:00:58,879 --> 00:01:03,879
a video, or really any other content that we might specify.
18
00:01:04,690 --> 00:01:09,310
Using CSS properties, we can specify both the height and
19
00:01:09,310 --> 00:01:11,330
the width of the content area,
20
00:01:11,330 --> 00:01:15,420
which we will do in one of the next lectures.
21
00:01:15,420 --> 00:01:18,630
Next, we can define a border that goes
22
00:01:18,630 --> 00:01:20,480
all around the element.
23
00:01:20,480 --> 00:01:24,680
And actually we already did that before. Remember?
24
00:01:24,680 --> 00:01:27,520
And by the way, this border is technically
25
00:01:27,520 --> 00:01:30,233
still inside of the element.
26
00:01:31,270 --> 00:01:36,270
And now the next ingredient of the box model is the padding.
27
00:01:36,400 --> 00:01:40,560
So padding is basically invisible white space that we can
28
00:01:40,560 --> 00:01:43,230
create around the elements content.
29
00:01:43,230 --> 00:01:46,660
So between the content and the border,
30
00:01:46,660 --> 00:01:49,530
you can see that nicely in this diagram,
31
00:01:49,530 --> 00:01:51,830
and it will make even more sense when
32
00:01:51,830 --> 00:01:54,240
we start using this in practice.
33
00:01:54,240 --> 00:01:58,280
But anyway, just like the border, the padding is also
34
00:01:58,280 --> 00:02:01,360
still inside of the element.
35
00:02:01,360 --> 00:02:05,930
So essentially padding is empty space that we can create
36
00:02:05,930 --> 00:02:08,330
inside of elements.
37
00:02:08,330 --> 00:02:12,840
Then similar to padding, we also have margin.
38
00:02:12,840 --> 00:02:15,170
Now the difference is that margin creates
39
00:02:15,170 --> 00:02:17,680
empty space around the element.
40
00:02:17,680 --> 00:02:21,480
So outside of the element itself.
41
00:02:21,480 --> 00:02:22,750
So in practice,
42
00:02:22,750 --> 00:02:25,310
we use margin in order to create space
43
00:02:25,310 --> 00:02:28,360
between elements on our page.
44
00:02:28,360 --> 00:02:31,290
So if for example we want space between
45
00:02:31,290 --> 00:02:34,150
two certain elements, we simply specify the
46
00:02:34,150 --> 00:02:37,020
margin property on one of them.
47
00:02:37,020 --> 00:02:40,890
So again, padding is empty space that we can
48
00:02:40,890 --> 00:02:43,960
add inside of any element.
49
00:02:43,960 --> 00:02:48,010
It will sit between the content itself and the border,
50
00:02:48,010 --> 00:02:52,350
which is basically the outermost part of the element.
51
00:02:52,350 --> 00:02:56,930
So the content, padding and border all together are the
52
00:02:56,930 --> 00:03:01,540
visible parts of the element as we can see it on the page.
53
00:03:01,540 --> 00:03:05,180
Then, around all that, we can add some margin
54
00:03:05,180 --> 00:03:08,640
in order to create space between elements.
55
00:03:08,640 --> 00:03:12,340
Now what's important to note here is that all of these are
56
00:03:12,340 --> 00:03:14,140
actually optional,
57
00:03:14,140 --> 00:03:19,140
so we don't have to specify paddings or a border or margins
58
00:03:19,440 --> 00:03:24,440
so we can define none of them or some, or even all of them.
59
00:03:24,500 --> 00:03:28,820
It all depends on how we want our layout to look like.
60
00:03:28,820 --> 00:03:31,270
Finally, there is also something called
61
00:03:31,270 --> 00:03:35,590
the fill area and this one can be a bit confusing,
62
00:03:35,590 --> 00:03:38,460
but let's try to understand it.
63
00:03:38,460 --> 00:03:41,290
So remember that the text content and
64
00:03:41,290 --> 00:03:45,710
images are inside the content box, right?
65
00:03:45,710 --> 00:03:48,610
Now, the same does not apply for background
66
00:03:48,610 --> 00:03:52,290
images or the background color of an element.
67
00:03:52,290 --> 00:03:56,040
These properties will actually be applied not only to
68
00:03:56,040 --> 00:03:59,770
the content area, but to the entire fill area,
69
00:03:59,770 --> 00:04:03,200
which also includes the padding and the border.
70
00:04:03,200 --> 00:04:07,440
So basically if we apply a background image or a color,
71
00:04:07,440 --> 00:04:11,373
it will occupy the entire visible part of the element.
72
00:04:13,150 --> 00:04:14,350
Now, for some people,
73
00:04:14,350 --> 00:04:17,730
this can all sound very abstract and hard to imagine
74
00:04:17,730 --> 00:04:19,200
in the real world.
75
00:04:19,200 --> 00:04:20,900
And so in these situations,
76
00:04:20,900 --> 00:04:25,370
it's very useful to think of an analogy in the real world.
77
00:04:25,370 --> 00:04:29,160
And one analogy that I personally find very helpful
78
00:04:29,160 --> 00:04:32,710
is to imagine a frame on a wall.
79
00:04:32,710 --> 00:04:34,420
So in this analogy,
80
00:04:34,420 --> 00:04:37,880
the drawing or the image that is in the frame is
81
00:04:37,880 --> 00:04:39,510
the content area.
82
00:04:39,510 --> 00:04:43,160
Then, many frames actually have this like kind of
83
00:04:43,160 --> 00:04:46,910
white paper between the drawing or the image
84
00:04:46,910 --> 00:04:49,350
and the actual frame itself.
85
00:04:49,350 --> 00:04:52,770
And so if we bring that back to our box model,
86
00:04:52,770 --> 00:04:55,540
then that would of course be the padding.
87
00:04:55,540 --> 00:04:59,600
So the space between the content area and the border.
88
00:04:59,600 --> 00:05:04,580
And so in this analogy, the frame is of course the border,
89
00:05:04,580 --> 00:05:07,300
right? So that makes a lot of sense.
90
00:05:07,300 --> 00:05:08,133
Finally,
91
00:05:08,133 --> 00:05:12,160
when we hang a picture on the wall using a frame like this,
92
00:05:12,160 --> 00:05:15,850
then of course there might be some space between other
93
00:05:15,850 --> 00:05:20,050
elements on the wall or even some other walls on the sides.
94
00:05:20,050 --> 00:05:22,750
And so in our box model analogy,
95
00:05:22,750 --> 00:05:25,420
that of course would be the margin.
96
00:05:25,420 --> 00:05:29,285
So it is the space that is outside of the frame and
97
00:05:29,285 --> 00:05:33,140
creating space between frames. So for example,
98
00:05:33,140 --> 00:05:36,620
there might be another frame somewhere here on the wall and
99
00:05:36,620 --> 00:05:40,420
the space between those frames, so outside of them,
100
00:05:40,420 --> 00:05:44,610
is what we call the margin in the box model. Okay?
101
00:05:44,610 --> 00:05:48,110
So hopefully this analogy from the real world helps you
102
00:05:48,110 --> 00:05:50,640
bring these concepts back into the
103
00:05:50,640 --> 00:05:53,560
more abstract world of CSS.
104
00:05:53,560 --> 00:05:56,650
And now, just to finish, let's quickly learn
105
00:05:56,650 --> 00:05:59,400
how the box model actually calculates
106
00:05:59,400 --> 00:06:02,540
the exact size of an element based on
107
00:06:02,540 --> 00:06:05,103
the content, border and padding.
108
00:06:07,200 --> 00:06:08,820
So, as I mentioned,
109
00:06:08,820 --> 00:06:13,520
we can specify the height and the width of the content area.
110
00:06:13,520 --> 00:06:16,930
Now, if we choose not to define the height or width,
111
00:06:16,930 --> 00:06:18,730
then the box model will simply
112
00:06:18,730 --> 00:06:22,190
imply them based on the content.
113
00:06:22,190 --> 00:06:26,466
However, these specified or implied heights and widths
114
00:06:26,466 --> 00:06:31,250
are actually not the final sizes of the element.
115
00:06:31,250 --> 00:06:33,970
And that's because the border and the padding
116
00:06:33,970 --> 00:06:37,060
are also taken into account.
117
00:06:37,060 --> 00:06:40,830
So the final width of an element as we see it in the
118
00:06:40,830 --> 00:06:44,850
final layout is defined by the left border
119
00:06:44,850 --> 00:06:49,850
plus the left padding plus the specified or implied width
120
00:06:49,960 --> 00:06:53,850
plus the right padding plus the right border.
121
00:06:53,850 --> 00:06:56,600
And the same logic of course applies to
122
00:06:56,600 --> 00:06:58,460
the height of an element.
123
00:06:58,460 --> 00:07:02,353
So it's the top border, plus the top padding
124
00:07:02,353 --> 00:07:06,020
plus the specified or implied height
125
00:07:06,020 --> 00:07:11,020
plus the bottom padding, and finally the bottom border.
126
00:07:11,180 --> 00:07:13,970
Now, as you can see, the margin is of course
127
00:07:13,970 --> 00:07:17,096
not part of the width or height calculations
128
00:07:17,096 --> 00:07:20,200
of the elements, because again,
129
00:07:20,200 --> 00:07:23,740
it is just space that is around them.
130
00:07:23,740 --> 00:07:26,450
And as I said in the beginning of the lecture,
131
00:07:26,450 --> 00:07:30,320
we can of course specify all these individual sizes using
132
00:07:30,320 --> 00:07:33,130
certain CSS properties.
133
00:07:33,130 --> 00:07:37,190
Now what's very important to note here is that this way of
134
00:07:37,190 --> 00:07:41,620
calculating heights and widths is actually just the default
135
00:07:41,620 --> 00:07:44,280
behavior of the box model.
136
00:07:44,280 --> 00:07:47,580
However, we can change this default behavior
137
00:07:47,580 --> 00:07:51,170
because it actually doesn't make much sense and
138
00:07:51,170 --> 00:07:53,650
we will do that a bit later, but for now
139
00:07:53,650 --> 00:07:55,992
I don't want to confuse you anymore
140
00:07:55,992 --> 00:07:59,443
and so let's start by using this default behavior
141
00:07:59,443 --> 00:08:02,133
starting right in the next video.
10855
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.