Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,420 --> 00:00:02,520
In this video,
2
00:00:02,520 --> 00:00:06,170
let's start applying the box model in practice
3
00:00:06,170 --> 00:00:09,483
and in particular margins and paddings.
4
00:00:11,160 --> 00:00:15,140
And let's start with padding and one great candidate
5
00:00:15,140 --> 00:00:18,670
for adding some padding is this header element here,
6
00:00:18,670 --> 00:00:21,220
which has this background color.
7
00:00:21,220 --> 00:00:23,930
So an element which has background color,
8
00:00:23,930 --> 00:00:27,760
can always use some padding in order to create some space
9
00:00:27,760 --> 00:00:31,960
between the content and the outside of an element.
10
00:00:31,960 --> 00:00:36,150
That's because remember padding is basically just space
11
00:00:36,150 --> 00:00:38,823
that is inside of the element.
12
00:00:39,840 --> 00:00:42,710
So this element is called the main header,
13
00:00:42,710 --> 00:00:45,290
and I think we have it somewhere down here
14
00:00:46,330 --> 00:00:48,053
and indeed here it is.
15
00:00:48,900 --> 00:00:53,370
Now actually, what I want to do is to take this code here,
16
00:00:53,370 --> 00:00:58,300
which is for these boxes and put it a bit more to the top.
17
00:00:58,300 --> 00:01:02,680
So I like to have, like these big level boxes,
18
00:01:02,680 --> 00:01:05,810
so boxes that contain multiple child elements
19
00:01:05,810 --> 00:01:07,967
or more at the top.
20
00:01:07,967 --> 00:01:08,990
All right.
21
00:01:08,990 --> 00:01:13,030
So this header here is kind of the first of these big boxes.
22
00:01:13,030 --> 00:01:16,223
And so I will put it here right after the body.
23
00:01:17,190 --> 00:01:18,023
All right.
24
00:01:19,370 --> 00:01:22,610
But now let's start by adding some padding.
25
00:01:22,610 --> 00:01:24,960
So we use the padding, a property,
26
00:01:24,960 --> 00:01:27,820
and then we need to specify some length.
27
00:01:27,820 --> 00:01:30,550
So let's use 20 pixels.
28
00:01:30,550 --> 00:01:34,800
And so pixels is exactly the same unit that we also used
29
00:01:34,800 --> 00:01:39,360
for the border width, and also for font sizes.
30
00:01:39,360 --> 00:01:40,800
Remember?
31
00:01:40,800 --> 00:01:43,320
So let's see what that looks like.
32
00:01:43,320 --> 00:01:47,780
And indeed, we now got ourselves some new space that is
33
00:01:47,780 --> 00:01:51,480
between the content, so between all of this,
34
00:01:51,480 --> 00:01:54,900
and of course the outside of the element.
35
00:01:54,900 --> 00:01:58,950
Now it looks like the space here is the same as here.
36
00:01:58,950 --> 00:02:03,640
So it's 20 pixels, but then here there's a lot more space.
37
00:02:03,640 --> 00:02:08,640
So let's right click here on this element and inspect.
38
00:02:08,850 --> 00:02:10,850
And so here you see the reason for that.
39
00:02:11,800 --> 00:02:14,810
So when we hover over an element,
40
00:02:14,810 --> 00:02:17,480
we actually get the margin off that element.
41
00:02:17,480 --> 00:02:22,480
And so that is that red or that orange overlay. Now right?
42
00:02:23,020 --> 00:02:26,620
And now the header itself, you see the green space there,
43
00:02:26,620 --> 00:02:28,470
that is the padding.
44
00:02:28,470 --> 00:02:29,510
Okay.
45
00:02:29,510 --> 00:02:31,100
So you'll see that in fact,
46
00:02:31,100 --> 00:02:33,370
the padding goes all around the box
47
00:02:33,370 --> 00:02:35,940
or all around the inside of the box
48
00:02:35,940 --> 00:02:39,130
and it is really 20 pixels everywhere.
49
00:02:39,130 --> 00:02:39,963
Okay.
50
00:02:39,963 --> 00:02:43,330
So we will fix this margin here in a second
51
00:02:43,330 --> 00:02:45,823
and by then, everything will make sense.
52
00:02:47,160 --> 00:02:48,290
Okay.
53
00:02:48,290 --> 00:02:50,840
Now actually down here in the styles,
54
00:02:50,840 --> 00:02:53,630
we can see the box model.
55
00:02:53,630 --> 00:02:54,463
Right?
56
00:02:54,463 --> 00:02:56,200
And so here we see that indeed,
57
00:02:56,200 --> 00:03:01,200
there is padding of 20 everywhere, so on all four sides.
58
00:03:01,310 --> 00:03:04,790
So this one here is actually the width and height,
59
00:03:04,790 --> 00:03:09,300
then the padding, then the border and then the margin.
60
00:03:09,300 --> 00:03:10,830
So for each element,
61
00:03:10,830 --> 00:03:13,810
there is a complete representation of the box model
62
00:03:13,810 --> 00:03:15,790
right here in the dev tools
63
00:03:15,790 --> 00:03:18,903
and many times this is extremely helpful.
64
00:03:20,430 --> 00:03:23,593
So we will use this now for sure, a bit later.
65
00:03:24,950 --> 00:03:28,340
Now this padding here does create the same padding
66
00:03:28,340 --> 00:03:31,110
on all four sides of the box.
67
00:03:31,110 --> 00:03:33,400
But if we wanted to create different paddings
68
00:03:33,400 --> 00:03:38,400
on different sides, we can do that also just very easily.
69
00:03:38,660 --> 00:03:40,630
So let's say we want a different paddings
70
00:03:40,630 --> 00:03:41,853
at the left and right.
71
00:03:43,500 --> 00:03:45,530
So we can say for example,
72
00:03:45,530 --> 00:03:48,483
40 pixels on the left and on the right.
73
00:03:50,720 --> 00:03:53,390
And so this is in fact exactly the same thing
74
00:03:53,390 --> 00:03:55,010
as with the border.
75
00:03:55,010 --> 00:03:56,530
So remember with the border,
76
00:03:56,530 --> 00:04:01,093
we also have border top, border bottom, left, and right.
77
00:04:02,310 --> 00:04:03,500
Okay.
78
00:04:03,500 --> 00:04:04,333
And now indeed,
79
00:04:04,333 --> 00:04:08,480
there's a lot more space here on the left side than
80
00:04:08,480 --> 00:04:09,650
on the bottom.
81
00:04:09,650 --> 00:04:11,560
And the same here on the right side,
82
00:04:11,560 --> 00:04:14,400
even though we cannot really see it by the content,
83
00:04:14,400 --> 00:04:18,060
but we can see it here using the dev tools. Right?
84
00:04:18,060 --> 00:04:20,280
So you see that here on the right,
85
00:04:20,280 --> 00:04:25,280
we have 40, 40 on the left and then 20 on top and on bottom.
86
00:04:26,020 --> 00:04:26,963
All right.
87
00:04:27,800 --> 00:04:30,950
But now, we can actually write this in an even better way
88
00:04:30,950 --> 00:04:34,323
by using something called a shorthand once again.
89
00:04:35,810 --> 00:04:40,600
So a shorthand is always when we specify multiple values
90
00:04:40,600 --> 00:04:42,520
here for one property.
91
00:04:42,520 --> 00:04:44,840
So for padding, what we can do is to,
92
00:04:44,840 --> 00:04:48,090
instead of just specifying one value,
93
00:04:48,090 --> 00:04:50,880
which will then be the same on all four sites,
94
00:04:50,880 --> 00:04:53,090
we can specify two values.
95
00:04:53,090 --> 00:04:56,510
And if we do that, then the first value will be padding
96
00:04:56,510 --> 00:04:58,470
on the top and bottom.
97
00:04:58,470 --> 00:05:01,260
And the second value will be padding on the left
98
00:05:01,260 --> 00:05:02,293
and right side.
99
00:05:03,150 --> 00:05:05,583
So to achieve the same result as before,
100
00:05:06,530 --> 00:05:10,570
we would say 20 pixels and 40 pixels.
101
00:05:10,570 --> 00:05:14,630
And so if we now removed this quote here, like this,
102
00:05:14,630 --> 00:05:19,430
using a comment, then our result is still the same.
103
00:05:19,430 --> 00:05:20,570
Okay.
104
00:05:20,570 --> 00:05:25,570
So we use this shorthand to add 20 to top and bottom,
105
00:05:25,680 --> 00:05:28,220
and 40 to left and right.
106
00:05:28,220 --> 00:05:30,890
And this way we don't have to write all of these
107
00:05:30,890 --> 00:05:33,000
different lines of codes here.
108
00:05:33,000 --> 00:05:35,770
We can just do it all in one go.
109
00:05:35,770 --> 00:05:39,340
Next up, let's play a little bit with margins.
110
00:05:39,340 --> 00:05:42,520
And remember that margins are basically spaces
111
00:05:42,520 --> 00:05:44,640
between elements.
112
00:05:44,640 --> 00:05:49,193
So let's now add some space between the LI elements.
113
00:05:50,620 --> 00:05:52,920
So these ones here,
114
00:05:52,920 --> 00:05:55,780
you see that they're very close together
115
00:05:55,780 --> 00:05:57,730
and it doesn't look nice.
116
00:05:57,730 --> 00:06:00,900
And therefore we can add some margin to the bottom
117
00:06:00,900 --> 00:06:02,593
of each of these elements.
118
00:06:04,110 --> 00:06:08,650
So somewhere down here, there must,
119
00:06:08,650 --> 00:06:11,020
so there is the LI already,
120
00:06:11,020 --> 00:06:13,710
so we don't need to select it over and over again.
121
00:06:13,710 --> 00:06:16,163
We just write it here, inside the same rule.
122
00:06:17,900 --> 00:06:20,983
And so let's say margin to the bottom,
123
00:06:23,530 --> 00:06:24,803
10 pixels.
124
00:06:25,710 --> 00:06:30,500
And so now that does look a lot better, right?
125
00:06:30,500 --> 00:06:31,893
Let's select this here.
126
00:06:33,020 --> 00:06:33,953
And indeed,
127
00:06:35,240 --> 00:06:38,850
we get 10 pixels
128
00:06:38,850 --> 00:06:42,050
at the margin bottom on each of them.
129
00:06:42,050 --> 00:06:43,480
Just scroll to the last one,
130
00:06:43,480 --> 00:06:46,050
because now I want to show you something.
131
00:06:46,050 --> 00:06:48,440
So usually in the last list item,
132
00:06:48,440 --> 00:06:50,710
we don't want to have any space
133
00:06:50,710 --> 00:06:54,630
because we actually only want space between the items,
134
00:06:54,630 --> 00:06:57,050
but not at the end of the items.
135
00:06:57,050 --> 00:07:00,460
So again, the less one should not have any space.
136
00:07:00,460 --> 00:07:03,050
And so let's use a trick for that,
137
00:07:03,050 --> 00:07:06,470
which is actually something that we learned before.
138
00:07:06,470 --> 00:07:11,470
So we can say LI, and then last-child.
139
00:07:13,600 --> 00:07:17,293
And then here, we basically reset what we just did.
140
00:07:18,320 --> 00:07:21,770
So we say margin bottom and set it to zero.
141
00:07:21,770 --> 00:07:26,760
And whenever we use zero, we do not specify any unit.
142
00:07:26,760 --> 00:07:27,593
Okay?
143
00:07:27,593 --> 00:07:30,523
So we just say zero and that will then override it.
144
00:07:31,460 --> 00:07:35,293
And now that space that we had here before should be gone.
145
00:07:36,370 --> 00:07:40,230
So you'll see this one has no margin, but the one before it,
146
00:07:40,230 --> 00:07:44,450
that does have these 10 pixels at the bottom.
147
00:07:44,450 --> 00:07:46,800
Nice, that's beautiful.
148
00:07:46,800 --> 00:07:49,970
And we will now keep adding more and more margins
149
00:07:49,970 --> 00:07:52,690
and paddings to all of our elements.
150
00:07:52,690 --> 00:07:54,080
But before we do that,
151
00:07:54,080 --> 00:07:58,330
we should actually do something called a global reset.
152
00:07:58,330 --> 00:08:01,420
So if you take a look at our page here,
153
00:08:01,420 --> 00:08:04,030
and let's close this, then you notice that,
154
00:08:04,030 --> 00:08:08,930
there are actually already a lot of spaces here, right?
155
00:08:08,930 --> 00:08:12,970
So there's space around this H2 element here,
156
00:08:12,970 --> 00:08:15,300
between the paragraphs,
157
00:08:15,300 --> 00:08:18,490
also here, as we saw before using the dev tools,
158
00:08:18,490 --> 00:08:22,990
there was a lot of margin on the top and bottom of this H1.
159
00:08:22,990 --> 00:08:26,610
And having all of these default margins and paddings,
160
00:08:26,610 --> 00:08:30,280
makes it quite hard for us to style our page.
161
00:08:30,280 --> 00:08:34,300
So what we want to do is to reset all those margins
162
00:08:34,300 --> 00:08:38,010
and paddings, and basically set them to zero.
163
00:08:38,010 --> 00:08:40,763
So how do you think we can do that?
164
00:08:41,597 --> 00:08:43,933
Do you think we should use inheritance?
165
00:08:44,920 --> 00:08:49,670
So do you think that margin and padding get inherited
166
00:08:49,670 --> 00:08:52,640
from one element to the other?
167
00:08:52,640 --> 00:08:57,623
Well, let's test and set the margin to zero.
168
00:08:59,050 --> 00:09:00,740
So let's see.
169
00:09:00,740 --> 00:09:04,030
And well, something actually did change.
170
00:09:04,030 --> 00:09:07,860
So the body lost the margin that it had.
171
00:09:07,860 --> 00:09:12,100
So now everything is really like glued to the border here.
172
00:09:12,100 --> 00:09:16,440
So to the border of the viewport, so the browser itself,
173
00:09:16,440 --> 00:09:21,000
but not all the elements lost its margin, right?
174
00:09:21,000 --> 00:09:23,420
And so this is not really the way to go
175
00:09:23,420 --> 00:09:27,930
because margin is not a property that is related to text,
176
00:09:27,930 --> 00:09:31,340
and therefore it's not a property that gets inherited
177
00:09:31,340 --> 00:09:34,320
from parents to child elements.
178
00:09:34,320 --> 00:09:37,210
Instead if we want the margin to be applied
179
00:09:37,210 --> 00:09:40,110
to every single element, then for that,
180
00:09:40,110 --> 00:09:43,090
we can use the universal selector.
181
00:09:43,090 --> 00:09:47,680
So margin set to zero and padding set to zero.
182
00:09:47,680 --> 00:09:52,033
And now this is exactly the result that we were looking for.
183
00:09:53,180 --> 00:09:55,960
So now all the default spaces are gone
184
00:09:55,960 --> 00:10:00,150
and we are free to format our page as we wish.
185
00:10:00,150 --> 00:10:02,400
And doing something like this here,
186
00:10:02,400 --> 00:10:06,800
so this kind of global reset is extremely common.
187
00:10:06,800 --> 00:10:09,750
So this is usually the very first thing that I do
188
00:10:09,750 --> 00:10:12,600
whenever I start any new project.
189
00:10:12,600 --> 00:10:16,070
And also keep in mind that, this margin and padding here
190
00:10:16,070 --> 00:10:18,500
are extremely easy to overwrite.
191
00:10:18,500 --> 00:10:21,990
So every other selector will actually overwrite
192
00:10:21,990 --> 00:10:23,970
this universal selector.
193
00:10:23,970 --> 00:10:27,810
And in fact, we can see that right here in the main header,
194
00:10:27,810 --> 00:10:30,140
where we did specify some padding,
195
00:10:30,140 --> 00:10:33,200
and so of course it is still visible here.
196
00:10:33,200 --> 00:10:34,150
Okay.
197
00:10:34,150 --> 00:10:38,960
So once again, this class selector here
198
00:10:38,960 --> 00:10:43,960
has a higher priority than the global selector.
199
00:10:43,960 --> 00:10:45,680
And in more technical terms,
200
00:10:45,680 --> 00:10:47,840
we say that the universal selector
201
00:10:47,840 --> 00:10:50,890
has a specificity of zero,
202
00:10:50,890 --> 00:10:54,380
which is what VS code shows us here, in this tool tip.
203
00:10:54,380 --> 00:10:55,690
While on the other hand,
204
00:10:55,690 --> 00:11:00,600
this selector here does have the one in one of these places.
205
00:11:00,600 --> 00:11:02,630
So this one is more specific.
206
00:11:02,630 --> 00:11:04,800
It has a higher priority,
207
00:11:04,800 --> 00:11:07,250
and therefore the padding does get applied.
208
00:11:07,250 --> 00:11:11,673
And the same is true also right here in this list.
209
00:11:12,550 --> 00:11:16,050
So here we also, already should have the 10 pixels
210
00:11:16,050 --> 00:11:18,980
that we just specified previously.
211
00:11:18,980 --> 00:11:19,813
Okay.
212
00:11:19,813 --> 00:11:22,380
And now with this reset in place,
213
00:11:22,380 --> 00:11:26,400
it is extremely easy to now add our custom spaces
214
00:11:26,400 --> 00:11:29,423
and to make this page really look designed.
215
00:11:30,300 --> 00:11:32,110
And since we're already here,
216
00:11:32,110 --> 00:11:36,340
let's actually start here with the <aside>
217
00:11:36,340 --> 00:11:37,980
or keep going here.
218
00:11:37,980 --> 00:11:39,210
Because once again,
219
00:11:39,210 --> 00:11:42,940
this is one of the elements which has a background color.
220
00:11:42,940 --> 00:11:45,690
And so therefore in order to create some space,
221
00:11:45,690 --> 00:11:48,720
we should use padding because, otherwise,
222
00:11:48,720 --> 00:11:52,390
this border here is really glued to the content.
223
00:11:52,390 --> 00:11:54,083
So that's not what we want.
224
00:11:55,140 --> 00:12:00,140
So let's say padding top of 50 pixels
225
00:12:01,530 --> 00:12:06,530
and padding bottom of also 50 pixels,
226
00:12:08,700 --> 00:12:13,270
let's give it a save and that looks a lot better.
227
00:12:13,270 --> 00:12:17,200
So for now we're leaving left and right side at zero,
228
00:12:17,200 --> 00:12:19,093
and we will fix that a bit later.
229
00:12:20,010 --> 00:12:21,680
So how could we write this here,
230
00:12:21,680 --> 00:12:23,793
actually in a bit better way?
231
00:12:24,630 --> 00:12:26,880
Well, I hope you guessed it.
232
00:12:26,880 --> 00:12:29,970
And the answer would be to use the shorthand.
233
00:12:29,970 --> 00:12:32,870
So just padding at the top and bottom,
234
00:12:32,870 --> 00:12:36,200
which is the first value of 50 pixels
235
00:12:36,200 --> 00:12:40,723
and the second value is left and right where we want zero.
236
00:12:42,330 --> 00:12:43,163
Now, that's it.
237
00:12:44,590 --> 00:12:45,750
So take this out,
238
00:12:45,750 --> 00:12:49,430
give it a save and it still does look the same.
239
00:12:49,430 --> 00:12:50,263
Now, okay.
240
00:12:51,240 --> 00:12:54,860
Now let's go up to the top of the page.
241
00:12:54,860 --> 00:12:57,240
And now here, we probably need some space
242
00:12:57,240 --> 00:13:02,240
between this header and this H2 element here, right?
243
00:13:02,240 --> 00:13:04,200
So this looks very bad.
244
00:13:04,200 --> 00:13:07,800
And so we need some space outside of this element here.
245
00:13:07,800 --> 00:13:10,150
So between these two, and for that,
246
00:13:10,150 --> 00:13:13,700
we can use a margin bottom below,
247
00:13:13,700 --> 00:13:16,590
basically this header element.
248
00:13:16,590 --> 00:13:19,480
So let's come here to our main header
249
00:13:19,480 --> 00:13:24,480
and at a margin bottom of let's say, 60 pixels.
250
00:13:25,570 --> 00:13:26,730
And it is in fact,
251
00:13:26,730 --> 00:13:30,830
very common to use mostly space at the bottom
252
00:13:30,830 --> 00:13:33,520
in order to create vertical space.
253
00:13:33,520 --> 00:13:34,660
Okay.
254
00:13:34,660 --> 00:13:37,560
So just a stick to either margin top
255
00:13:37,560 --> 00:13:39,560
or always margin bottom.
256
00:13:39,560 --> 00:13:41,140
So one of them is fine,
257
00:13:41,140 --> 00:13:44,540
but it's best not to always mix them when you want
258
00:13:44,540 --> 00:13:46,570
to create space between elements
259
00:13:46,570 --> 00:13:49,970
and personally, I really like to use margin bottom.
260
00:13:49,970 --> 00:13:54,100
It makes a lot of sense in my mind to do it like this.
261
00:13:54,100 --> 00:13:56,260
Okay and let's keep going.
262
00:13:56,260 --> 00:14:00,023
And maybe add some space now, below this H2.
263
00:14:01,330 --> 00:14:03,173
So H2 is right here.
264
00:14:04,920 --> 00:14:09,920
So let's say a margin at the bottom, once again,
265
00:14:10,580 --> 00:14:14,090
and here we just use 30 pixels.
266
00:14:14,090 --> 00:14:17,940
And again, these values here are quite random
267
00:14:17,940 --> 00:14:21,370
and later we will use a system to automatically,
268
00:14:21,370 --> 00:14:24,053
basically create these values for us.
269
00:14:24,900 --> 00:14:29,090
Next, let's create some space here between this part.
270
00:14:29,090 --> 00:14:33,290
So basically this image and then the rest of the content.
271
00:14:33,290 --> 00:14:36,320
And if we take a look at our markup,
272
00:14:36,320 --> 00:14:39,850
that part, so this image, this author,
273
00:14:39,850 --> 00:14:42,493
and this other image here is this header.
274
00:14:43,820 --> 00:14:47,240
So let's add some margin bottom to this header.
275
00:14:47,240 --> 00:14:49,910
And so I'm giving it another name.
276
00:14:49,910 --> 00:14:51,700
So this one is the main header.
277
00:14:51,700 --> 00:14:55,773
So let's call this one here, the post header.
278
00:14:59,410 --> 00:15:01,663
So let's copy that here,
279
00:15:06,140 --> 00:15:07,033
put it here,
280
00:15:11,494 --> 00:15:14,213
and then margin bottom of 40 pixels.
281
00:15:17,370 --> 00:15:19,710
So that's a lot nicer.
282
00:15:19,710 --> 00:15:22,440
Next, I want to think about the paragraphs
283
00:15:22,440 --> 00:15:26,510
because, well, this looks very messy right now.
284
00:15:26,510 --> 00:15:29,390
And so just by adding some space to the bottom
285
00:15:29,390 --> 00:15:31,040
of each paragraph,
286
00:15:31,040 --> 00:15:33,720
that will make it a lot better immediately.
287
00:15:33,720 --> 00:15:38,720
So margin bottom, and let's try 15 pixels here.
288
00:15:39,930 --> 00:15:43,760
And indeed that is a lot better already.
289
00:15:43,760 --> 00:15:45,733
Now the same with these lists.
290
00:15:46,800 --> 00:15:49,600
So now here, in this ordered list
291
00:15:49,600 --> 00:15:52,960
and this unordered list that we have here,
292
00:15:52,960 --> 00:15:56,600
now, actually the numbers and the bullet points are a gone.
293
00:15:56,600 --> 00:15:58,900
And that is because we actually need some space
294
00:15:58,900 --> 00:16:01,653
on the left side so that they can be shown.
295
00:16:02,610 --> 00:16:05,480
So let's add them back because,
296
00:16:05,480 --> 00:16:09,780
our global reset, basically removed all those spaces.
297
00:16:09,780 --> 00:16:11,390
Remember?
298
00:16:11,390 --> 00:16:14,410
So we want that space in the unordered list
299
00:16:14,410 --> 00:16:16,413
and also in the ordered list.
300
00:16:17,890 --> 00:16:21,420
So let's say margin on the left side,
301
00:16:21,420 --> 00:16:22,473
for example,
302
00:16:23,430 --> 00:16:24,723
a 50 pixel.
303
00:16:26,020 --> 00:16:29,030
And so now you see that, they are back.
304
00:16:29,030 --> 00:16:32,240
So everything moved 50 pixels to the right.
305
00:16:32,240 --> 00:16:36,623
And so now the one, so the numbers and the bullets are back.
306
00:16:39,690 --> 00:16:43,833
Let's also add some space to the bottom,
307
00:16:45,550 --> 00:16:47,000
20 pixels.
308
00:16:47,000 --> 00:16:47,910
Okay.
309
00:16:47,910 --> 00:16:49,993
And now let's move on to the H3.
310
00:16:51,270 --> 00:16:52,853
So that's this one here.
311
00:16:54,890 --> 00:16:59,717
So margin bottom 20 pixels,
312
00:17:01,860 --> 00:17:04,010
and something is wrong.
313
00:17:04,010 --> 00:17:07,150
I cannot spell margin apparently.
314
00:17:07,150 --> 00:17:09,440
So that looks a lot better.
315
00:17:09,440 --> 00:17:12,360
And now we also need to specify some distance
316
00:17:12,360 --> 00:17:16,370
between this text here and this heading.
317
00:17:16,370 --> 00:17:17,640
So right now it looks like,
318
00:17:17,640 --> 00:17:21,970
the space between these two is exactly the same
319
00:17:21,970 --> 00:17:23,970
and that's not good.
320
00:17:23,970 --> 00:17:25,940
And I'm actually doing this
321
00:17:25,940 --> 00:17:27,790
because I want to show you something.
322
00:17:28,800 --> 00:17:32,140
So I'm gonna specify the margin top now
323
00:17:32,140 --> 00:17:35,000
and set it to 40 pixels.
324
00:17:35,000 --> 00:17:38,050
Okay. Now that works beautifully.
325
00:17:38,050 --> 00:17:42,690
However, let me now show you something using the dev tools.
326
00:17:42,690 --> 00:17:44,533
So I'm inspecting this element.
327
00:17:45,600 --> 00:17:49,000
And so you see that there is exactly the 20 pixels
328
00:17:49,000 --> 00:17:50,930
that'd we specified to the bottom,
329
00:17:50,930 --> 00:17:54,890
and the 40 pixels that we specified to the top.
330
00:17:54,890 --> 00:17:58,430
However, this paragraph that comes before it,
331
00:17:58,430 --> 00:18:01,990
actually also has a margin to the bottom of,
332
00:18:01,990 --> 00:18:04,700
I believe, 15 pixels.
333
00:18:04,700 --> 00:18:07,040
So this one, right?
334
00:18:07,040 --> 00:18:12,040
So right now this element here has 40 pixels to the top
335
00:18:12,390 --> 00:18:15,900
and this one has 15 pixels to the bottom.
336
00:18:15,900 --> 00:18:20,900
And so both of these spaces occupy this area here, right?
337
00:18:21,420 --> 00:18:24,440
And so we might intuitively think that,
338
00:18:24,440 --> 00:18:27,740
these two spaces do get added up.
339
00:18:27,740 --> 00:18:32,350
So basically that, these 15 pixels here will get added
340
00:18:32,350 --> 00:18:35,470
to the 40 pixels that come from here.
341
00:18:35,470 --> 00:18:38,720
But in fact, that is not what happens.
342
00:18:38,720 --> 00:18:41,040
So we see here that in fact,
343
00:18:41,040 --> 00:18:42,910
the distance between the two elements
344
00:18:42,910 --> 00:18:45,120
is really just 40 pixels.
345
00:18:45,120 --> 00:18:47,720
It is not 55 pixels.
346
00:18:47,720 --> 00:18:48,981
Okay.
347
00:18:48,981 --> 00:18:52,640
And this phenomena is called collapsing margins.
348
00:18:52,640 --> 00:18:53,660
So basically,
349
00:18:53,660 --> 00:18:57,133
when we have two margins that occupied the same space,
350
00:18:57,133 --> 00:19:01,510
only one of them is actually visible on the page.
351
00:19:01,510 --> 00:19:04,980
And that is usually the larger of the two.
352
00:19:04,980 --> 00:19:05,920
So for example,
353
00:19:05,920 --> 00:19:10,123
let's say that we specified, like 40 pixels here.
354
00:19:11,090 --> 00:19:13,240
Then we might've thought that,
355
00:19:13,240 --> 00:19:15,760
this was actually 80 pixels here,
356
00:19:15,760 --> 00:19:18,240
so following the same logic from before.
357
00:19:18,240 --> 00:19:21,040
But of course it is still only 40 pixels
358
00:19:21,040 --> 00:19:23,920
because of the collapsing margins.
359
00:19:23,920 --> 00:19:27,920
But what if we had like 100 pixels here?
360
00:19:27,920 --> 00:19:32,920
Well then, now the space here would in fact be 100 pixels.
361
00:19:33,870 --> 00:19:37,683
So exactly this orange space that you see right there.
362
00:19:38,770 --> 00:19:40,040
Right?
363
00:19:40,040 --> 00:19:42,170
So this one now still has its 40,
364
00:19:42,170 --> 00:19:43,720
but this one has a hundred.
365
00:19:43,720 --> 00:19:47,410
And so now the space between them is still not 140,
366
00:19:47,410 --> 00:19:52,030
but it's these hundred pixels of the bottom of the <p>.
367
00:19:52,030 --> 00:19:53,110
Okay.
368
00:19:53,110 --> 00:19:55,360
So this is what we mean when we talk
369
00:19:55,360 --> 00:19:57,400
about collapsing margins,
370
00:19:57,400 --> 00:19:59,265
which is something that you might hear
371
00:19:59,265 --> 00:20:04,090
once you start hearing people talking about some CSS,
372
00:20:04,090 --> 00:20:09,090
weird quirks or like weird stuff that is inside of CSS.
373
00:20:09,470 --> 00:20:10,303
Okay.
374
00:20:10,303 --> 00:20:15,303
And, but this, I think we are basically almost done already.
375
00:20:15,380 --> 00:20:17,340
Let's just add some more space here
376
00:20:17,340 --> 00:20:21,570
between these related posts and all this content here,
377
00:20:21,570 --> 00:20:24,583
which is the article, right?
378
00:20:25,590 --> 00:20:30,280
So we have article and the <aside>.
379
00:20:30,280 --> 00:20:32,653
So we need some space there as well.
380
00:20:33,530 --> 00:20:36,550
And I don't think if I already selected the article anywhere
381
00:20:37,770 --> 00:20:40,843
and it looks like I didn't,
382
00:20:42,850 --> 00:20:45,000
so let's put that at the top.
383
00:20:45,000 --> 00:20:45,930
And since I'm here,
384
00:20:45,930 --> 00:20:49,330
I will grab this nav and put it also at the top
385
00:20:49,330 --> 00:20:52,610
because it's one of those bigger elements.
386
00:20:52,610 --> 00:20:55,070
So I like to keep them at the top.
387
00:20:55,070 --> 00:20:58,423
And of course the same goes for the article.
388
00:21:00,750 --> 00:21:02,113
So article,
389
00:21:03,920 --> 00:21:08,873
and a nice margin bottom of 60 pixel.
390
00:21:11,070 --> 00:21:12,530
Okay.
391
00:21:12,530 --> 00:21:14,230
And here we go,
392
00:21:14,230 --> 00:21:18,590
that's it, that's our page nasty formatted
393
00:21:18,590 --> 00:21:22,090
in terms of margins and paddings.
394
00:21:22,090 --> 00:21:24,210
So just to quickly recap,
395
00:21:24,210 --> 00:21:27,760
whenever you need some space inside of an element,
396
00:21:27,760 --> 00:21:29,270
which is very useful,
397
00:21:29,270 --> 00:21:31,760
mostly when there is a background color
398
00:21:31,760 --> 00:21:33,910
or a border on the element,
399
00:21:33,910 --> 00:21:36,480
then you always use padding.
400
00:21:36,480 --> 00:21:37,590
On the other hand,
401
00:21:37,590 --> 00:21:41,380
in order to create space outside of an element,
402
00:21:41,380 --> 00:21:44,850
or also to create space between multiple elements,
403
00:21:44,850 --> 00:21:46,750
always use margin.
404
00:21:46,750 --> 00:21:49,570
And in case that you need to add vertical space,
405
00:21:49,570 --> 00:21:52,080
then I advise you to most of the time,
406
00:21:52,080 --> 00:21:53,510
stick to margin bottom.
407
00:21:53,510 --> 00:21:58,130
And so basically that's it about adding space
408
00:21:58,130 --> 00:21:59,880
to our page elements.
409
00:21:59,880 --> 00:22:01,180
And in the next lecture,
410
00:22:01,180 --> 00:22:05,193
we will then also add dimensions, so height and width.
29900
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.