Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,490 --> 00:00:04,110
At the end of the previous lecture,
2
00:00:04,110 --> 00:00:06,250
we discussed how it would be nice
3
00:00:06,250 --> 00:00:08,930
to be able to name some elements
4
00:00:08,930 --> 00:00:13,210
and then use those names to select them in CSS.
5
00:00:13,210 --> 00:00:14,490
And so in this video,
6
00:00:14,490 --> 00:00:19,490
we will learn to do exactly that using Classes and IDs.
7
00:00:21,170 --> 00:00:23,630
So remember how in the last lecture
8
00:00:23,630 --> 00:00:28,230
we wanted to style only this text here as italic.
9
00:00:28,230 --> 00:00:31,460
So this paragraph that is inside this header,
10
00:00:31,460 --> 00:00:33,640
but not this other paragraph
11
00:00:33,640 --> 00:00:36,040
that is also inside of a header.
12
00:00:36,040 --> 00:00:38,030
So we came up with the solution
13
00:00:38,030 --> 00:00:41,880
of selecting only the paragraphs inside of headers
14
00:00:41,880 --> 00:00:44,460
that are inside of articles.
15
00:00:44,460 --> 00:00:46,030
Remember that.
16
00:00:46,030 --> 00:00:48,670
The problem with this is that this essentially
17
00:00:48,670 --> 00:00:53,410
encodes the HTML structure into our CSS selector.
18
00:00:53,410 --> 00:00:55,660
And that is not a good idea.
19
00:00:55,660 --> 00:01:00,190
And so instead we will now basically name this element here
20
00:01:00,190 --> 00:01:02,610
with something called an ID.
21
00:01:02,610 --> 00:01:03,540
And so with that,
22
00:01:03,540 --> 00:01:06,130
we can then avoid writing a big
23
00:01:06,130 --> 00:01:08,623
and a long selector like this.
24
00:01:10,040 --> 00:01:12,230
So let's go here.
25
00:01:12,230 --> 00:01:16,110
And the paragraph in question is this one here.
26
00:01:16,110 --> 00:01:21,110
And so now we can specify a special attribute called an ID.
27
00:01:21,640 --> 00:01:24,210
So the attribute is called ID.
28
00:01:24,210 --> 00:01:28,290
And now let's simply call this one Author.
29
00:01:28,290 --> 00:01:31,200
So remember this name here, Author.
30
00:01:31,200 --> 00:01:32,920
And so now this element here,
31
00:01:32,920 --> 00:01:36,400
this paragraph is basically called Author.
32
00:01:36,400 --> 00:01:41,400
And so now in Or CSS, we can use the Hash selector.
33
00:01:42,330 --> 00:01:46,260
So this one, which is in fact, the ID selector.
34
00:01:46,260 --> 00:01:50,803
So Hash and now the name of the ID that we just specified.
35
00:01:51,780 --> 00:01:53,730
And now here we can,
36
00:01:53,730 --> 00:01:58,730
one more time set the font style to italic now, right?
37
00:02:00,610 --> 00:02:02,960
And now we can get rid of this code here
38
00:02:02,960 --> 00:02:05,660
because it's no longer necessary.
39
00:02:05,660 --> 00:02:08,090
Now, instead of deleting this code,
40
00:02:08,090 --> 00:02:10,050
I actually want to keep it here.
41
00:02:10,050 --> 00:02:14,180
And so we can do something similar as we did in HTML,
42
00:02:14,180 --> 00:02:16,290
where we wrote comments.
43
00:02:16,290 --> 00:02:17,123
Remember that.
44
00:02:18,010 --> 00:02:22,940
So here we wrote some HTML comments to keep this code here
45
00:02:22,940 --> 00:02:26,260
without being actually active, right?
46
00:02:26,260 --> 00:02:28,563
And so in HTML, we can do the same.
47
00:02:29,450 --> 00:02:31,500
It just works in a different way.
48
00:02:31,500 --> 00:02:32,640
So in CSS,
49
00:02:32,640 --> 00:02:36,240
we write a Slash then the Star
50
00:02:36,240 --> 00:02:38,500
and then from this place on
51
00:02:38,500 --> 00:02:40,810
everything else will be a comment.
52
00:02:40,810 --> 00:02:42,640
So we need to close this comment
53
00:02:42,640 --> 00:02:45,620
because of course we want this code to be active.
54
00:02:45,620 --> 00:02:48,020
And so we write now the opposite.
55
00:02:48,020 --> 00:02:50,163
So Star and Slash.
56
00:02:51,360 --> 00:02:53,160
So now only this is gray
57
00:02:53,160 --> 00:02:55,920
and so only this is the comment.
58
00:02:55,920 --> 00:02:58,180
So if I save this now,
59
00:02:58,180 --> 00:03:01,130
then you see that in fact, that only this text,
60
00:03:01,130 --> 00:03:05,203
so only this paragraph is italic and not this one.
61
00:03:06,040 --> 00:03:08,840
Let's actually add another style here.
62
00:03:08,840 --> 00:03:10,563
So another declaration.
63
00:03:11,590 --> 00:03:16,590
And let's set the font size to 18 pixels.
64
00:03:17,710 --> 00:03:19,210
So make it a bit smaller
65
00:03:19,210 --> 00:03:23,433
because remember how the other paragraphs are 22 pixels big.
66
00:03:24,560 --> 00:03:27,640
And indeed this one here got smaller
67
00:03:27,640 --> 00:03:29,440
and all the other paragraphs,
68
00:03:29,440 --> 00:03:32,693
including of course this one here, stayed the same.
69
00:03:33,560 --> 00:03:38,210
Now let's do something similar to the copyright text here.
70
00:03:38,210 --> 00:03:40,410
So this one, and with that,
71
00:03:40,410 --> 00:03:44,570
we will then replace this CSS rule right here.
72
00:03:44,570 --> 00:03:48,050
So we can again, comment it out the way I just did it,
73
00:03:48,050 --> 00:03:51,580
but let me actually show you a nice VS code trick.
74
00:03:51,580 --> 00:03:53,430
So in order to comment a code
75
00:03:53,430 --> 00:03:56,120
that you previously selected like this,
76
00:03:56,120 --> 00:04:01,070
all you have to do is to hit Command or Control + Slash.
77
00:04:01,070 --> 00:04:02,890
So I will do that now.
78
00:04:02,890 --> 00:04:04,920
And so you see that it automatically
79
00:04:04,920 --> 00:04:07,770
edit these comments here, right?
80
00:04:07,770 --> 00:04:10,450
And if I then hit Command or Control
81
00:04:10,450 --> 00:04:13,640
on windows plus Slash again,
82
00:04:13,640 --> 00:04:15,750
then it turns the comment back off.
83
00:04:15,750 --> 00:04:20,320
So I can go back and forth between these two modes.
84
00:04:20,320 --> 00:04:22,370
So if we reload this now,
85
00:04:22,370 --> 00:04:25,550
of course it will be back to 22 pixels.
86
00:04:25,550 --> 00:04:29,793
And so let's go to our HTML and give this one a name.
87
00:04:30,990 --> 00:04:33,883
So a name using the ID attribute.
88
00:04:35,460 --> 00:04:38,943
So ID, let's say copyright.
89
00:04:40,050 --> 00:04:42,180
Let's then select this text,
90
00:04:42,180 --> 00:04:45,470
copy it and just paste it here.
91
00:04:45,470 --> 00:04:49,130
And remember we use the ID selector like this,
92
00:04:49,130 --> 00:04:51,403
plus the name that we just specified,
93
00:04:53,490 --> 00:04:58,103
and let's go back to a font size of 16 pixels.
94
00:05:00,320 --> 00:05:02,880
And here it is.
95
00:05:02,880 --> 00:05:05,240
So it worked once again.
96
00:05:05,240 --> 00:05:07,090
Now, besides the ID,
97
00:05:07,090 --> 00:05:10,540
there's actually another way of giving elements names,
98
00:05:10,540 --> 00:05:15,290
which is by using the Class and instead of an ID.
99
00:05:15,290 --> 00:05:19,190
So we specified the ID attribute here
100
00:05:19,190 --> 00:05:20,970
to give this element a name,
101
00:05:20,970 --> 00:05:24,730
but we could also have used the Class attribute.
102
00:05:24,730 --> 00:05:28,540
Now the big difference between IDs and Classes
103
00:05:28,540 --> 00:05:32,770
is that we are not allowed to repeat ID names,
104
00:05:32,770 --> 00:05:34,080
or in other words,
105
00:05:34,080 --> 00:05:38,150
we can only use each ID name only once.
106
00:05:38,150 --> 00:05:42,550
So it will not be allowed to now add an ID of copyright
107
00:05:42,550 --> 00:05:44,590
to something else.
108
00:05:44,590 --> 00:05:47,230
So I could not for example,
109
00:05:47,230 --> 00:05:52,230
now go here and add the ID of copyright.
110
00:05:52,960 --> 00:05:56,790
So this would actually be invalid HTML.
111
00:05:56,790 --> 00:05:59,290
So we are not allowed to do that.
112
00:05:59,290 --> 00:06:03,570
And so if we need to reuse a name multiple times,
113
00:06:03,570 --> 00:06:06,860
then we need to use Classes for that.
114
00:06:06,860 --> 00:06:08,270
Okay.
115
00:06:08,270 --> 00:06:11,370
So let me show you an example of using Classes.
116
00:06:11,370 --> 00:06:15,680
And so what I want to do now is to style these authors here
117
00:06:15,680 --> 00:06:19,920
of these three related posts in a different way.
118
00:06:19,920 --> 00:06:23,750
So basically that's this P-element here
119
00:06:23,750 --> 00:06:26,510
and this one and this one.
120
00:06:26,510 --> 00:06:27,590
And so again,
121
00:06:27,590 --> 00:06:32,590
I can now not use the ID of say related author.
122
00:06:33,460 --> 00:06:36,473
So instead I need to use the Class attribute.
123
00:06:37,750 --> 00:06:42,750
And let's call it related-author.
124
00:06:42,890 --> 00:06:44,040
So in CSS,
125
00:06:44,040 --> 00:06:48,590
it is a convention that if we have a Class or an ID name
126
00:06:48,590 --> 00:06:50,250
with multiple words,
127
00:06:50,250 --> 00:06:54,190
we separate these words by Dashes like this.
128
00:06:54,190 --> 00:06:57,700
You could also use Underscores for example.
129
00:06:57,700 --> 00:06:59,320
So that's also valid,
130
00:06:59,320 --> 00:07:01,660
but it's just a convention in CSS
131
00:07:01,660 --> 00:07:04,760
to always use Dash like this.
132
00:07:04,760 --> 00:07:05,593
Okay?
133
00:07:06,780 --> 00:07:08,130
So anyway,
134
00:07:08,130 --> 00:07:09,630
let's now grab this here
135
00:07:09,630 --> 00:07:12,323
and paste it to all these paragraphs.
136
00:07:14,520 --> 00:07:17,683
So related-author three times.
137
00:07:18,530 --> 00:07:22,260
And so now let's go to our CSS
138
00:07:22,260 --> 00:07:26,290
and style all the elements that have this Class.
139
00:07:26,290 --> 00:07:31,140
So for the IDs, we used the Hash to select them.
140
00:07:31,140 --> 00:07:33,860
And for Classes, we use the Dot.
141
00:07:33,860 --> 00:07:36,410
So this is the Class selector.
142
00:07:36,410 --> 00:07:37,520
Okay?
143
00:07:37,520 --> 00:07:40,053
So related-author.
144
00:07:41,040 --> 00:07:43,340
And let's say that here we want
145
00:07:43,340 --> 00:07:48,340
the font size to be 18 pixels, let's say.
146
00:07:49,770 --> 00:07:53,090
And let's also specify a new text property
147
00:07:53,090 --> 00:07:57,053
that we didn't learn before, which is the font-weight.
148
00:07:58,060 --> 00:08:01,903
So this one we can use to make text bold, for example.
149
00:08:02,920 --> 00:08:06,030
So as you saw, maybe here, there are multiple options.
150
00:08:06,030 --> 00:08:09,750
We can use some numbers, but we will use that later.
151
00:08:09,750 --> 00:08:14,750
For now, let's just use a keyword, which is Bold.
152
00:08:14,960 --> 00:08:18,000
So let's save it to see the results.
153
00:08:18,000 --> 00:08:23,000
And indeed now only these three paragraphs are different.
154
00:08:24,180 --> 00:08:26,390
So they are bold and they are smaller
155
00:08:26,390 --> 00:08:27,923
than all the other paragraphs.
156
00:08:28,820 --> 00:08:32,309
So of course in the HTML, they are still paragraphs,
157
00:08:32,309 --> 00:08:37,270
but now they got these additional styles here, right?
158
00:08:37,270 --> 00:08:40,210
Now maybe you are wondering something here now,
159
00:08:40,210 --> 00:08:42,690
which is that we are now in fact,
160
00:08:42,690 --> 00:08:46,990
selecting these paragraphs multiple times, right?
161
00:08:46,990 --> 00:08:51,990
So we are selecting this paragraph here, in fact, right?
162
00:08:52,410 --> 00:08:55,310
But we are also selecting it here
163
00:08:55,310 --> 00:08:59,150
and we are selecting it actually also up here.
164
00:08:59,150 --> 00:09:03,610
So three times now we selected this paragraph.
165
00:09:03,610 --> 00:09:05,010
So you might be thinking
166
00:09:05,010 --> 00:09:08,600
which one of these gets applied actually.
167
00:09:08,600 --> 00:09:12,020
So isn't this like some kind of conflict?
168
00:09:12,020 --> 00:09:14,190
Well, the short answer is
169
00:09:14,190 --> 00:09:17,500
that actually all of these selectors do apply.
170
00:09:17,500 --> 00:09:19,480
So all of these three rules
171
00:09:19,480 --> 00:09:22,870
of the three selectors do in fact apply.
172
00:09:22,870 --> 00:09:26,230
But I will explain more about how all of this works
173
00:09:26,230 --> 00:09:29,570
and why it works this way in an extra video,
174
00:09:29,570 --> 00:09:31,780
because this is something really important
175
00:09:31,780 --> 00:09:35,980
that you will need to understand in order to master CSS.
176
00:09:35,980 --> 00:09:40,270
And so this topic deserves a special lecture on itself,
177
00:09:40,270 --> 00:09:43,530
which will be a bit later in this section.
178
00:09:43,530 --> 00:09:46,990
But now moving forward here,
179
00:09:46,990 --> 00:09:50,190
let me now actually give you a small challenge.
180
00:09:50,190 --> 00:09:54,350
So what I want to do is to remove the bullet points
181
00:09:54,350 --> 00:09:56,363
of only this list here.
182
00:09:57,360 --> 00:10:00,430
So remember that we created a list for this.
183
00:10:00,430 --> 00:10:04,153
And so of course, this one has bullet points right now.
184
00:10:05,020 --> 00:10:08,400
So I want you to select only this list here,
185
00:10:08,400 --> 00:10:11,693
only this element and remove it's bullet points.
186
00:10:12,590 --> 00:10:15,880
Now we never removed any bullet points.
187
00:10:15,880 --> 00:10:18,110
So let me show you how it works.
188
00:10:18,110 --> 00:10:20,260
And for that, I will now in fact,
189
00:10:20,260 --> 00:10:22,393
select all the unordered lists.
190
00:10:24,100 --> 00:10:28,650
So here we use the property list-style
191
00:10:28,650 --> 00:10:31,793
and set it to the keyword of None.
192
00:10:33,150 --> 00:10:33,983
Okay?
193
00:10:35,260 --> 00:10:38,480
So the bullet points disappeared from this one,
194
00:10:38,480 --> 00:10:41,040
but also from this one.
195
00:10:41,040 --> 00:10:42,340
And so, as I said,
196
00:10:42,340 --> 00:10:45,600
I want you to now select only this element here
197
00:10:45,600 --> 00:10:48,230
and then remove the bullet points in there.
198
00:10:48,230 --> 00:10:53,230
And I want you to do this by using a Class called Related.
199
00:10:53,400 --> 00:10:54,460
Okay?
200
00:10:54,460 --> 00:10:56,193
So let me comment this one here.
201
00:10:57,300 --> 00:10:59,530
And so yeah.
202
00:10:59,530 --> 00:11:03,700
Create a Class on this element on this list here
203
00:11:03,700 --> 00:11:06,710
and then remove the bullet points from this one.
204
00:11:06,710 --> 00:11:09,853
And I will see you back here in a minute, once you are done.
205
00:11:12,800 --> 00:11:14,010
Okay.
206
00:11:14,010 --> 00:11:17,630
So let's start by going to the HTML.
207
00:11:17,630 --> 00:11:21,100
And then of course this here is the UL
208
00:11:21,100 --> 00:11:22,373
that we want to select.
209
00:11:23,430 --> 00:11:26,620
And remember how I wanted you to use a Class.
210
00:11:26,620 --> 00:11:29,680
And I will explain a bit later why.
211
00:11:29,680 --> 00:11:32,683
So we used the Class here called Related,
212
00:11:34,710 --> 00:11:38,880
and now we go to our CSS
213
00:11:38,880 --> 00:11:43,880
and select this element with the Class that we just named.
214
00:11:45,750 --> 00:11:49,240
So of course, using the Class selector like this.
215
00:11:49,240 --> 00:11:51,400
And now only on this one,
216
00:11:51,400 --> 00:11:55,620
we say that list-style should be None.
217
00:11:55,620 --> 00:11:57,320
Give it a save,
218
00:11:57,320 --> 00:12:00,980
and so this one still has the bullet points
219
00:12:00,980 --> 00:12:03,060
and this one doesn't.
220
00:12:03,060 --> 00:12:04,290
Great.
221
00:12:04,290 --> 00:12:08,400
So we did this because we don't want this list here
222
00:12:08,400 --> 00:12:10,840
to actually look like a list.
223
00:12:10,840 --> 00:12:14,673
It should only semantically be a list of related posts.
224
00:12:16,990 --> 00:12:20,640
But anyway, let's now go back to our HTML
225
00:12:20,640 --> 00:12:24,610
and talk a little bit about why we used a Class
226
00:12:24,610 --> 00:12:26,380
and not an ID.
227
00:12:26,380 --> 00:12:27,550
Because in fact,
228
00:12:27,550 --> 00:12:32,550
there's only one list element of Related posts, right?
229
00:12:32,620 --> 00:12:37,130
So we could easily have called this an ID, right?
230
00:12:37,130 --> 00:12:40,381
Because remember that the IDs are four names
231
00:12:40,381 --> 00:12:42,600
that are only used once,
232
00:12:42,600 --> 00:12:43,933
while Classes are used
233
00:12:43,933 --> 00:12:47,470
when we want to give multiple elements the same name.
234
00:12:47,470 --> 00:12:50,460
So when we want to style multiple elements
235
00:12:50,460 --> 00:12:52,790
the same way in CSS.
236
00:12:52,790 --> 00:12:54,450
So knowing about that,
237
00:12:54,450 --> 00:12:59,350
why did I tell you to use a Class and not an ID?
238
00:12:59,350 --> 00:13:03,310
Well, the reason for that is that usually in the real world,
239
00:13:03,310 --> 00:13:05,350
we never use IDs.
240
00:13:05,350 --> 00:13:08,760
We always use Classes because by using Classes,
241
00:13:08,760 --> 00:13:11,240
we are prepared for the future.
242
00:13:11,240 --> 00:13:15,820
So imagine that we used in fact, an ID of Related,
243
00:13:15,820 --> 00:13:18,620
but then at some point later, somewhere on the page,
244
00:13:18,620 --> 00:13:22,130
we wanted to add another list of related posts.
245
00:13:22,130 --> 00:13:24,990
Maybe we wanted one in the sidebar
246
00:13:24,990 --> 00:13:27,530
and we wanted one in the footer.
247
00:13:27,530 --> 00:13:30,400
So imagining a more complex layout.
248
00:13:30,400 --> 00:13:33,760
That would completely be possible, right?
249
00:13:33,760 --> 00:13:38,400
But then if we had used an ID of Related in the beginning,
250
00:13:38,400 --> 00:13:42,820
we would now have to go back and change that ID to a Class
251
00:13:42,820 --> 00:13:47,030
so that we could now add another list of related posts.
252
00:13:47,030 --> 00:13:49,300
And so that would be a source of error
253
00:13:49,300 --> 00:13:52,380
if we've, for some reason, forgot that.
254
00:13:52,380 --> 00:13:56,060
And we would also have to go to our CSS
255
00:13:56,060 --> 00:13:57,323
and change that there.
256
00:13:58,170 --> 00:14:01,940
So at first it was an ID of Related,
257
00:14:01,940 --> 00:14:06,510
we would now have to come back here and change it to a Dot.
258
00:14:06,510 --> 00:14:08,450
So to the Class selector.
259
00:14:08,450 --> 00:14:11,330
And while this doesn't seem to be a big problem,
260
00:14:11,330 --> 00:14:13,630
it can actually lead to bugs.
261
00:14:13,630 --> 00:14:18,290
So to errors in the code if we have a really big project.
262
00:14:18,290 --> 00:14:19,720
And so by default,
263
00:14:19,720 --> 00:14:22,470
we simply avoid this potential problem
264
00:14:22,470 --> 00:14:25,540
by simply always using Classes.
265
00:14:25,540 --> 00:14:26,730
Okay.
266
00:14:26,730 --> 00:14:30,740
Now I will still keep this ID here for you.
267
00:14:30,740 --> 00:14:35,477
So this ID of copyright and this ID of author,
268
00:14:36,460 --> 00:14:40,100
just so you also know how to work with IDs,
269
00:14:40,100 --> 00:14:41,360
but in the future,
270
00:14:41,360 --> 00:14:44,070
I will actually no longer use IDs
271
00:14:44,070 --> 00:14:47,400
because as I just said in the real world,
272
00:14:47,400 --> 00:14:49,740
we simply always use Classes,
273
00:14:49,740 --> 00:14:52,810
even if we only use the Class name once.
274
00:14:52,810 --> 00:14:54,070
Because by doing that,
275
00:14:54,070 --> 00:14:56,770
we are making our code ready for the future
276
00:14:56,770 --> 00:14:59,900
and ready for potential changes.
277
00:14:59,900 --> 00:15:01,180
Okay.
278
00:15:01,180 --> 00:15:03,830
So I hope that made sense.
279
00:15:03,830 --> 00:15:06,140
And you can now go ahead if you want
280
00:15:06,140 --> 00:15:08,270
and name some more elements
281
00:15:08,270 --> 00:15:10,240
and style them in different ways,
282
00:15:10,240 --> 00:15:12,070
if you feel like it.
283
00:15:12,070 --> 00:15:14,940
So basically to practice this a little bit,
284
00:15:14,940 --> 00:15:16,210
and then if you'd like,
285
00:15:16,210 --> 00:15:18,890
you can come with me to the next video
286
00:15:18,890 --> 00:15:21,673
where we will start working with colors.
20661
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.