Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,540 --> 00:00:05,870
One of the very first tricks that I learned with CSS
2
00:00:05,870 --> 00:00:10,070
was how to center a page inside of the browser.
3
00:00:10,070 --> 00:00:11,240
So back in the day,
4
00:00:11,240 --> 00:00:15,800
many pagers were not centered and I found it really cool
5
00:00:15,800 --> 00:00:19,370
that we could very easily center a page in a browser
6
00:00:19,370 --> 00:00:22,670
using a very simple trick. And so let me now
7
00:00:22,670 --> 00:00:26,683
share that really nice trick with you in this video.
8
00:00:27,865 --> 00:00:30,960
Now, the very first thing that we need to do
9
00:00:30,960 --> 00:00:34,120
in order to pull off this trick is to
10
00:00:34,120 --> 00:00:38,170
put all of our content into a container element,
11
00:00:38,170 --> 00:00:41,090
because otherwise, if we don't do that,
12
00:00:41,090 --> 00:00:44,710
then what is there actually to center in the window?
13
00:00:44,710 --> 00:00:49,430
So what I mean is that right now, as we keep resizing
14
00:00:49,430 --> 00:00:52,630
the browser window, all of the content actually
15
00:00:52,630 --> 00:00:55,650
stretches from one side to the other.
16
00:00:55,650 --> 00:00:59,600
So there is no content there to center
17
00:00:59,600 --> 00:01:03,140
in the center of the browser, right?
18
00:01:03,140 --> 00:01:06,680
So we first need a container and that container
19
00:01:06,680 --> 00:01:08,443
then needs a certain width.
20
00:01:09,810 --> 00:01:14,810
So let's put all of this into a box and
21
00:01:15,010 --> 00:01:18,960
this box doesn't need to have any special meaning.
22
00:01:18,960 --> 00:01:22,450
So we don't need a semantic HTML element here.
23
00:01:22,450 --> 00:01:25,913
And so we can simply use a div element.
24
00:01:27,440 --> 00:01:29,823
Now this one should not close here,
25
00:01:30,690 --> 00:01:34,823
but it should close right before the end of the body.
26
00:01:36,570 --> 00:01:38,420
So it starts right there
27
00:01:39,300 --> 00:01:42,873
and we close it only here after the footer.
28
00:01:43,720 --> 00:01:44,970
And now as I save it,
29
00:01:44,970 --> 00:01:48,770
all the indentation should automatically get fixed
30
00:01:48,770 --> 00:01:53,160
and it did, nice. So this is a really nice thing
31
00:01:53,160 --> 00:01:58,160
about prettier, so when we keep adding other elements,
32
00:01:58,280 --> 00:02:00,970
then all the indentation will automatically get fixed,
33
00:02:00,970 --> 00:02:02,883
which is really, really helpful.
34
00:02:03,750 --> 00:02:06,210
Then here, we need to give us a name
35
00:02:06,210 --> 00:02:09,253
and I like to use the name of container.
36
00:02:10,210 --> 00:02:13,573
Let's copy that class and style it here.
37
00:02:14,630 --> 00:02:17,730
And as I was saying, now we actually need to give
38
00:02:17,730 --> 00:02:19,870
this container a width because
39
00:02:19,870 --> 00:02:23,343
otherwise there is not really anything to center.
40
00:02:25,550 --> 00:02:29,090
So let's start with 700 pixels
41
00:02:29,090 --> 00:02:31,800
and I'll make it a bit wider later.
42
00:02:31,800 --> 00:02:33,260
Okay.
43
00:02:33,260 --> 00:02:37,600
So we have this width of 700 pixels now.
44
00:02:37,600 --> 00:02:40,670
So you'll see it that now the width of this element.
45
00:02:40,670 --> 00:02:42,370
So this header here,
46
00:02:42,370 --> 00:02:45,760
which is a child element of the container is
47
00:02:45,760 --> 00:02:49,263
now only 702 pixels as well.
48
00:02:51,600 --> 00:02:56,497
So again, this div here has a width of 700,
49
00:02:57,610 --> 00:02:59,480
so we can see that here.
50
00:02:59,480 --> 00:03:01,750
And so now the child elements,
51
00:03:01,750 --> 00:03:05,200
which are inside of it can of course not be bigger than
52
00:03:05,200 --> 00:03:10,200
that. So they can only be as large as the container element.
53
00:03:10,200 --> 00:03:15,100
And so therefore this one is also a 600 pixels wide
54
00:03:15,100 --> 00:03:20,083
or actually 700. So that's 620 plus 40 plus 40.
55
00:03:21,280 --> 00:03:22,410
Okay.
56
00:03:22,410 --> 00:03:26,040
So this has nothing to do like with inheritance or something
57
00:03:26,040 --> 00:03:29,640
like that. Instead it is simply the logic that
58
00:03:29,640 --> 00:03:33,030
the child element can never be wider than the parent
59
00:03:33,030 --> 00:03:37,100
element. And so if the parent element is 700,
60
00:03:37,100 --> 00:03:40,443
then all the others are going to be 700 as well.
61
00:03:41,820 --> 00:03:46,630
So this one for example is 700 and the h2 as well,
62
00:03:46,630 --> 00:03:48,223
and so on and so forth.
63
00:03:49,300 --> 00:03:52,670
Right, the same here goes for the image of course,
64
00:03:52,670 --> 00:03:54,483
which is 100% of the parent.
65
00:03:56,070 --> 00:03:58,810
And so now as we keep making this bigger
66
00:03:58,810 --> 00:04:01,870
this 700 pixel container here,
67
00:04:01,870 --> 00:04:04,820
always keep staying on the left side.
68
00:04:04,820 --> 00:04:07,100
And so now as a final step,
69
00:04:07,100 --> 00:04:10,730
we need to center this container element
70
00:04:10,730 --> 00:04:12,880
inside of the browser.
71
00:04:12,880 --> 00:04:15,563
And so that is the actual trick.
72
00:04:17,470 --> 00:04:21,500
So giving the container width is part one,
73
00:04:21,500 --> 00:04:25,620
and now part two is to add some margins to the left
74
00:04:25,620 --> 00:04:27,763
and to the right of the container.
75
00:04:31,100 --> 00:04:35,500
So we can say margin left and then set it to auto,
76
00:04:35,500 --> 00:04:37,223
which stands for automatic.
77
00:04:38,420 --> 00:04:43,320
And margin to the right also automatic.
78
00:04:43,320 --> 00:04:47,050
So what this means is that the margin on the left needs to
79
00:04:47,050 --> 00:04:50,240
be the same than the margin on the right side.
80
00:04:50,240 --> 00:04:52,970
And they should both be calculated automatically
81
00:04:52,970 --> 00:04:54,300
by the browser.
82
00:04:54,300 --> 00:04:57,910
And so therefore this will mean that there will be a margin
83
00:04:57,910 --> 00:04:59,660
on the left and on the right,
84
00:04:59,660 --> 00:05:02,260
which will be exactly the same size.
85
00:05:02,260 --> 00:05:04,320
And therefore, as a result,
86
00:05:04,320 --> 00:05:08,480
the container will look centered inside of the body.
87
00:05:08,480 --> 00:05:11,493
So basically inside of the browser.
88
00:05:12,530 --> 00:05:17,370
So if we give us a safe , then it does actually work.
89
00:05:17,370 --> 00:05:19,030
Great.
90
00:05:19,030 --> 00:05:21,060
This is fantastic.
91
00:05:21,060 --> 00:05:24,373
So as we resize, let's see,
92
00:05:25,780 --> 00:05:30,000
then the margin on the left and on the right keep growing.
93
00:05:30,000 --> 00:05:34,300
And they keep giving us basically this illusion that the
94
00:05:34,300 --> 00:05:36,950
page is completely centered
95
00:05:36,950 --> 00:05:39,560
so that the container is centered.
96
00:05:39,560 --> 00:05:43,203
So if I inspect this now and go to the container,
97
00:05:44,490 --> 00:05:47,430
then here, I can see that the margin is right now,
98
00:05:47,430 --> 00:05:50,660
180 on both sides.
99
00:05:50,660 --> 00:05:52,453
And if I resize this,
100
00:05:54,120 --> 00:05:56,890
then you see that now it is only for example,
101
00:05:56,890 --> 00:05:59,583
72 and a half on each side.
102
00:06:00,570 --> 00:06:03,040
So again, it is the same on both sides
103
00:06:03,040 --> 00:06:06,160
because it is auto on both sides.
104
00:06:06,160 --> 00:06:08,900
And auto means that the browser will automatically
105
00:06:08,900 --> 00:06:11,053
figure out the width.
106
00:06:12,188 --> 00:06:13,825
Okay.
107
00:06:13,825 --> 00:06:17,520
Now here actually I want this to be 800 pixels,
108
00:06:17,520 --> 00:06:19,690
but I put it a bit smaller before,
109
00:06:19,690 --> 00:06:24,400
because if we then put it like this, we can actually not,
110
00:06:24,400 --> 00:06:27,760
not see anything. So it doesn't look centered,
111
00:06:27,760 --> 00:06:31,940
but of course, if we keep growing the window,
112
00:06:31,940 --> 00:06:36,247
then now this entire column is nicely centered, right?
113
00:06:38,170 --> 00:06:40,340
So this is a really great trick.
114
00:06:40,340 --> 00:06:45,340
And I remember back in the day, like way over 15 years ago,
115
00:06:45,500 --> 00:06:48,463
I was really happy when I discovered this trick.
116
00:06:50,480 --> 00:06:51,910
Now, just to finish,
117
00:06:51,910 --> 00:06:55,630
we can also write the serie as a shorthand once again,
118
00:06:55,630 --> 00:06:57,923
which is actually even more common.
119
00:06:59,100 --> 00:07:03,260
So the margin at the top and the bottom right now is zero
120
00:07:03,260 --> 00:07:05,460
and left and right, is auto.
121
00:07:05,460 --> 00:07:07,910
And so that is the second value of the shorthand.
122
00:07:09,590 --> 00:07:11,990
So we can get rid of this.
123
00:07:11,990 --> 00:07:15,710
And there we go, let's test it again.
124
00:07:15,710 --> 00:07:19,393
And indeed it had work the same, right?
125
00:07:20,320 --> 00:07:23,890
And with this, we finished this nice trick.
126
00:07:23,890 --> 00:07:26,870
And also actually we finished this part of
127
00:07:26,870 --> 00:07:29,190
the section about the box model.
128
00:07:29,190 --> 00:07:32,700
And so what this means is that we are now ready for the next
129
00:07:32,700 --> 00:07:35,530
part of the coding challenge.
130
00:07:35,530 --> 00:07:39,810
So after revealing this and maybe revealing this entire
131
00:07:39,810 --> 00:07:42,870
file, so how it looks like right now
132
00:07:42,870 --> 00:07:45,030
after this part of the section.
133
00:07:45,030 --> 00:07:47,010
So once you're done doing that,
134
00:07:47,010 --> 00:07:51,300
then let's move on to the next video and start our next
135
00:07:51,300 --> 00:07:52,710
coding challenge.
136
00:07:52,710 --> 00:07:53,683
So see you there.
10145
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.