All language subtitles for 0002 - CSS Selectors Tutorial for Beginners.en.clean

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,910 --> 00:00:05,030 [Music] 2 00:00:05,040 --> 00:00:07,190 hello and welcome hi i'm dave this 3 00:00:07,200 --> 00:00:09,350 tutorial is part of a beginner css 4 00:00:09,360 --> 00:00:11,589 series i'll be using the chrome web 5 00:00:11,599 --> 00:00:14,310 browser the visual studio code editor 6 00:00:14,320 --> 00:00:16,230 and the live server extension for visual 7 00:00:16,240 --> 00:00:18,630 studio code to view the web page 8 00:00:18,640 --> 00:00:20,630 there are links to these tools starter 9 00:00:20,640 --> 00:00:23,029 code files and all resources in the 10 00:00:23,039 --> 00:00:25,029 description below 11 00:00:25,039 --> 00:00:27,589 today we're working with css selectors 12 00:00:27,599 --> 00:00:29,669 and there are three levels of selectors 13 00:00:29,679 --> 00:00:31,509 that are the most common now i've got 14 00:00:31,519 --> 00:00:33,990 visual studio code open here on the left 15 00:00:34,000 --> 00:00:37,510 and i have a blank style.css file that 16 00:00:37,520 --> 00:00:40,869 is linked to our html file and the html 17 00:00:40,879 --> 00:00:43,190 file is open in chrome already i'm using 18 00:00:43,200 --> 00:00:45,110 the live server extension with visual 19 00:00:45,120 --> 00:00:47,510 studio code so we will instantly see our 20 00:00:47,520 --> 00:00:49,430 changes in our page 21 00:00:49,440 --> 00:00:53,510 the structure of the html page has an h1 22 00:00:53,520 --> 00:00:56,389 heading and after that we have three 23 00:00:56,399 --> 00:00:58,950 articles each article has its own h2 24 00:00:58,960 --> 00:01:01,590 heading and paragraph inside 25 00:01:01,600 --> 00:01:03,590 so as we talk about the three different 26 00:01:03,600 --> 00:01:06,149 types of basic selectors in css let's 27 00:01:06,159 --> 00:01:08,870 start with the most common which is an 28 00:01:08,880 --> 00:01:11,510 element selector and i'm going to select 29 00:01:11,520 --> 00:01:13,750 the body element now there is only one 30 00:01:13,760 --> 00:01:16,310 body element per page but we have some 31 00:01:16,320 --> 00:01:19,910 very small font and i will get into more 32 00:01:19,920 --> 00:01:22,469 properties for each of the declarations 33 00:01:22,479 --> 00:01:24,230 as we go through tutorials but i'm going 34 00:01:24,240 --> 00:01:27,109 to use a few today just for examples and 35 00:01:27,119 --> 00:01:29,670 i'm going to set the font size to 22 36 00:01:29,680 --> 00:01:33,190 pixels and save now we can see this made 37 00:01:33,200 --> 00:01:36,469 all of our text on our page larger and 38 00:01:36,479 --> 00:01:38,710 this is due to inheritance so these 39 00:01:38,720 --> 00:01:41,429 other elements actually inherited from 40 00:01:41,439 --> 00:01:44,069 the body element and we'll get more into 41 00:01:44,079 --> 00:01:46,149 inheritance in just a little bit too but 42 00:01:46,159 --> 00:01:47,190 this 43 00:01:47,200 --> 00:01:49,830 in itself is an element selector now 44 00:01:49,840 --> 00:01:51,990 there's only one body element per page 45 00:01:52,000 --> 00:01:54,870 but say i went ahead and selected the 46 00:01:54,880 --> 00:01:57,990 paragraph using the paragraph selector 47 00:01:58,000 --> 00:02:00,789 and now i can set the color of the text 48 00:02:00,799 --> 00:02:02,469 and i'm going to set the color of the 49 00:02:02,479 --> 00:02:04,149 text to purple 50 00:02:04,159 --> 00:02:07,270 and i'll save and notice it changed all 51 00:02:07,280 --> 00:02:10,309 three of our paragraphs to purple so 52 00:02:10,319 --> 00:02:12,550 element selectors select all of the 53 00:02:12,560 --> 00:02:15,350 elements of that type so just specifying 54 00:02:15,360 --> 00:02:18,869 p for the paragraph element selects 55 00:02:18,879 --> 00:02:21,270 all paragraph elements now the second 56 00:02:21,280 --> 00:02:23,990 type of selector is a class and so let's 57 00:02:24,000 --> 00:02:26,550 define a class selector i'm going to 58 00:02:26,560 --> 00:02:29,190 call this class gray note that a class 59 00:02:29,200 --> 00:02:31,589 starts with a period before whatever you 60 00:02:31,599 --> 00:02:33,430 name the class and you can choose the 61 00:02:33,440 --> 00:02:35,589 name of the class but make sure it makes 62 00:02:35,599 --> 00:02:36,470 sense 63 00:02:36,480 --> 00:02:38,150 and remember your writing code for 64 00:02:38,160 --> 00:02:40,550 others to read not just yourself okay 65 00:02:40,560 --> 00:02:42,390 inside of this i'm going to set the 66 00:02:42,400 --> 00:02:45,110 color to gray that makes sense to me 67 00:02:45,120 --> 00:02:48,790 now i'll save that and notice paragraphs 68 00:02:48,800 --> 00:02:51,830 2 and three turn the gray color and if 69 00:02:51,840 --> 00:02:53,990 we look at our html which is a little 70 00:02:54,000 --> 00:02:56,470 crowded over here but if i scroll down 71 00:02:56,480 --> 00:02:58,790 to the second paragraph here we have a 72 00:02:58,800 --> 00:02:59,830 class 73 00:02:59,840 --> 00:03:03,110 set equal to gray on that paragraph 74 00:03:03,120 --> 00:03:05,830 likewise we have a class set equal to 75 00:03:05,840 --> 00:03:08,869 gray on the third paragraph as well so 76 00:03:08,879 --> 00:03:11,750 classes can be used more than once and 77 00:03:11,760 --> 00:03:13,350 they're very useful that way you're 78 00:03:13,360 --> 00:03:15,350 creating styles that you can apply 79 00:03:15,360 --> 00:03:17,110 throughout your project or throughout 80 00:03:17,120 --> 00:03:19,750 your page and then they can be reused 81 00:03:19,760 --> 00:03:21,670 with more than one element and note 82 00:03:21,680 --> 00:03:24,869 classes are more specific than selecting 83 00:03:24,879 --> 00:03:27,670 all paragraphs for example and because 84 00:03:27,680 --> 00:03:30,550 they're more specific they overruled the 85 00:03:30,560 --> 00:03:32,309 purple setting that we had for all 86 00:03:32,319 --> 00:03:35,350 paragraphs and now gray applied to these 87 00:03:35,360 --> 00:03:37,750 last two paragraphs here and we will 88 00:03:37,760 --> 00:03:40,710 also discuss specificity in more detail 89 00:03:40,720 --> 00:03:42,390 in just a few minutes as well now 90 00:03:42,400 --> 00:03:44,710 classes are the most common type of 91 00:03:44,720 --> 00:03:46,630 selector with css 92 00:03:46,640 --> 00:03:48,470 but the more specific 93 00:03:48,480 --> 00:03:51,830 selector yet is an id selector and i 94 00:03:51,840 --> 00:03:54,949 have an id named second an id attribute 95 00:03:54,959 --> 00:03:57,910 inside of the html and here i'm going to 96 00:03:57,920 --> 00:04:01,509 apply a font style and we'll set that to 97 00:04:01,519 --> 00:04:02,789 italic 98 00:04:02,799 --> 00:04:05,350 and i like to have a space afterwards 99 00:04:05,360 --> 00:04:07,350 now i'll save and notice the second 100 00:04:07,360 --> 00:04:11,030 paragraph became italic as well so it 101 00:04:11,040 --> 00:04:12,630 specifically 102 00:04:12,640 --> 00:04:16,310 addressed the second paragraph now ids 103 00:04:16,320 --> 00:04:18,870 should only exist once in an html 104 00:04:18,880 --> 00:04:20,949 document they should be unique in other 105 00:04:20,959 --> 00:04:24,310 words however it is not good practice to 106 00:04:24,320 --> 00:04:28,469 really use ids inside of your css now it 107 00:04:28,479 --> 00:04:31,670 will validate your code has no problem 108 00:04:31,680 --> 00:04:33,430 using an id 109 00:04:33,440 --> 00:04:36,230 however typically you should use classes 110 00:04:36,240 --> 00:04:38,950 and sometimes element selectors but 111 00:04:38,960 --> 00:04:41,590 rarely if ever should you use an id 112 00:04:41,600 --> 00:04:44,310 selector inside of your css they do have 113 00:04:44,320 --> 00:04:46,550 other valid uses if you remember from 114 00:04:46,560 --> 00:04:50,150 html linking a form input back to a 115 00:04:50,160 --> 00:04:52,469 label element and then as you learn 116 00:04:52,479 --> 00:04:54,550 javascript you may have some other uses 117 00:04:54,560 --> 00:04:57,030 for id attributes as well but we try to 118 00:04:57,040 --> 00:04:59,749 keep them out of css and now that we've 119 00:04:59,759 --> 00:05:01,670 covered the three basic types of 120 00:05:01,680 --> 00:05:03,830 selectors let's discuss what you can do 121 00:05:03,840 --> 00:05:06,230 with some of them just a little bit more 122 00:05:06,240 --> 00:05:09,110 so let's have our h1 here and let's set 123 00:05:09,120 --> 00:05:12,230 the color for the h1 to blue 124 00:05:12,240 --> 00:05:14,230 and we'll save and we can see it only 125 00:05:14,240 --> 00:05:17,270 selected the h1 we only have one h1 on 126 00:05:17,280 --> 00:05:20,150 our page but we can also group selectors 127 00:05:20,160 --> 00:05:23,270 so let's set all of the h2s as well to 128 00:05:23,280 --> 00:05:26,070 blue and you can see i just put a comma 129 00:05:26,080 --> 00:05:27,830 between and then 130 00:05:27,840 --> 00:05:30,390 this declaration or this rule set the 131 00:05:30,400 --> 00:05:32,390 entire rule set if we had more rules in 132 00:05:32,400 --> 00:05:35,029 here more declarations i should say 133 00:05:35,039 --> 00:05:38,710 it would apply to both the h1 and the h2 134 00:05:38,720 --> 00:05:41,749 so you can go ahead and group selectors 135 00:05:41,759 --> 00:05:43,749 with a comma in between now notice what 136 00:05:43,759 --> 00:05:46,550 happens if i remove the comma and save 137 00:05:46,560 --> 00:05:49,830 this declaration or our full rule set 138 00:05:49,840 --> 00:05:51,909 especially if we had more declarations 139 00:05:51,919 --> 00:05:54,550 none of it would apply to the h1 or the 140 00:05:54,560 --> 00:05:56,469 h2 now what's going on here this 141 00:05:56,479 --> 00:05:59,909 selector is looking for any h2s that 142 00:05:59,919 --> 00:06:03,350 exist inside of an h1 it's not looking 143 00:06:03,360 --> 00:06:06,390 for an h1 or an h2 it's only looking for 144 00:06:06,400 --> 00:06:10,469 h2s that exist or are nested if you will 145 00:06:10,479 --> 00:06:13,029 inside of an h1 so that's why that 146 00:06:13,039 --> 00:06:15,430 didn't work that may be useful to know 147 00:06:15,440 --> 00:06:17,830 in the future but what we need is a 148 00:06:17,840 --> 00:06:20,230 comma in between and then we can select 149 00:06:20,240 --> 00:06:24,309 both the h1 and all h2s on the page with 150 00:06:24,319 --> 00:06:26,070 this rule set so let's look at an 151 00:06:26,080 --> 00:06:28,070 example of where we would select an 152 00:06:28,080 --> 00:06:30,790 element that's inside another element so 153 00:06:30,800 --> 00:06:32,629 let's start with our paragraph elements 154 00:06:32,639 --> 00:06:35,029 again and now we'll look for any span 155 00:06:35,039 --> 00:06:37,029 element inside 156 00:06:37,039 --> 00:06:39,749 any paragraph element and here let's set 157 00:06:39,759 --> 00:06:42,629 a couple of values so we'll set 158 00:06:42,639 --> 00:06:43,990 text dash 159 00:06:44,000 --> 00:06:45,590 transform 160 00:06:45,600 --> 00:06:47,590 and i'm going to choose 161 00:06:47,600 --> 00:06:50,390 uppercase here and then i'm also going 162 00:06:50,400 --> 00:06:53,350 to set a background 163 00:06:53,360 --> 00:06:54,870 color 164 00:06:54,880 --> 00:06:57,350 and let's set this to gold so it stands 165 00:06:57,360 --> 00:06:59,350 out we're essentially highlighting what 166 00:06:59,360 --> 00:07:02,390 we want to see and now we can see 167 00:07:02,400 --> 00:07:04,950 we have set this word whatever this word 168 00:07:04,960 --> 00:07:07,749 is this is latin from our lorem epson 169 00:07:07,759 --> 00:07:10,550 we've set it to uppercase and same with 170 00:07:10,560 --> 00:07:14,390 epsom and then we also set gold behind 171 00:07:14,400 --> 00:07:16,710 each of these words with this however 172 00:07:16,720 --> 00:07:18,790 this doesn't make a lot of sense this is 173 00:07:18,800 --> 00:07:21,350 a much better example of where we should 174 00:07:21,360 --> 00:07:24,790 use a class we might name our class 175 00:07:24,800 --> 00:07:26,629 something like 176 00:07:26,639 --> 00:07:28,629 highlight 177 00:07:28,639 --> 00:07:32,390 and then apply that to our span elements 178 00:07:32,400 --> 00:07:33,670 inside 179 00:07:33,680 --> 00:07:36,870 of the index so we've come in here and 180 00:07:36,880 --> 00:07:38,950 find our span 181 00:07:38,960 --> 00:07:42,469 and i'll select both of these 182 00:07:42,479 --> 00:07:49,909 and then we can add the class highlight 183 00:07:49,919 --> 00:07:51,909 and that makes it reusable if i spell 184 00:07:51,919 --> 00:07:54,869 highlight correctly there we go and save 185 00:07:54,879 --> 00:07:57,670 and now it's once again applied but this 186 00:07:57,680 --> 00:08:00,950 would keep our css much more organized 187 00:08:00,960 --> 00:08:03,589 and actually more reusable it's more 188 00:08:03,599 --> 00:08:05,589 flexible this way because we might have 189 00:08:05,599 --> 00:08:08,070 some span elements doing other things 190 00:08:08,080 --> 00:08:10,230 that we didn't want to apply 191 00:08:10,240 --> 00:08:12,790 this rule set too and now one last 192 00:08:12,800 --> 00:08:15,350 selector to discuss it exists we 193 00:08:15,360 --> 00:08:18,550 wouldn't use it that often but it is the 194 00:08:18,560 --> 00:08:21,430 universal selector and this means it's 195 00:08:21,440 --> 00:08:24,390 selecting everything on the page you 196 00:08:24,400 --> 00:08:26,070 typically only see this for what is 197 00:08:26,080 --> 00:08:28,550 called a css reset that we will cover in 198 00:08:28,560 --> 00:08:30,309 the future but just so you know it 199 00:08:30,319 --> 00:08:33,829 exists this selects everything so let's 200 00:08:33,839 --> 00:08:36,230 think about what we could apply here 201 00:08:36,240 --> 00:08:38,949 that would be applicable to everything 202 00:08:38,959 --> 00:08:41,670 so let's change the font 203 00:08:41,680 --> 00:08:44,310 and we'll set it to this mono space font 204 00:08:44,320 --> 00:08:46,630 and save and now you can see all of the 205 00:08:46,640 --> 00:08:48,389 text on the page 206 00:08:48,399 --> 00:08:51,190 turned into this monospace font instead 207 00:08:51,200 --> 00:08:53,509 of the default font that we had now this 208 00:08:53,519 --> 00:08:55,269 is not how we would use this again in 209 00:08:55,279 --> 00:08:57,910 the future we'll learn about a css reset 210 00:08:57,920 --> 00:09:00,070 that would use that selector but 211 00:09:00,080 --> 00:09:02,150 typically that's the only time you see 212 00:09:02,160 --> 00:09:05,030 that universal selector used okay let's 213 00:09:05,040 --> 00:09:07,269 discuss the cascade 214 00:09:07,279 --> 00:09:10,310 css is an acronym for cascading style 215 00:09:10,320 --> 00:09:12,550 sheets and essentially that means css 216 00:09:12,560 --> 00:09:14,790 works like a waterfall it works from the 217 00:09:14,800 --> 00:09:17,990 top down and that means if i were to put 218 00:09:18,000 --> 00:09:20,310 in another definition i'll just copy 219 00:09:20,320 --> 00:09:22,710 this down for paragraph and where we 220 00:09:22,720 --> 00:09:23,590 have 221 00:09:23,600 --> 00:09:26,470 the color purple on our first paragraph 222 00:09:26,480 --> 00:09:28,389 it really applies to all but then of 223 00:09:28,399 --> 00:09:31,110 course the gray class overrides that on 224 00:09:31,120 --> 00:09:32,470 the final two 225 00:09:32,480 --> 00:09:35,190 but if i came here and said 226 00:09:35,200 --> 00:09:37,829 red instead of purple 227 00:09:37,839 --> 00:09:39,750 notice how this paragraph 228 00:09:39,760 --> 00:09:42,389 turns red and that's because 229 00:09:42,399 --> 00:09:46,710 it read this rule set last so first css 230 00:09:46,720 --> 00:09:48,870 looked at this at the top it starts at 231 00:09:48,880 --> 00:09:51,350 the top and it works its way down and so 232 00:09:51,360 --> 00:09:53,990 it read this last essentially this is 233 00:09:54,000 --> 00:09:57,110 the last rule and that means whichever 234 00:09:57,120 --> 00:10:00,550 definition it reads at the last will be 235 00:10:00,560 --> 00:10:03,829 applied now specificity can override 236 00:10:03,839 --> 00:10:05,590 this and that's where we talked about 237 00:10:05,600 --> 00:10:08,710 elements and then classes classes are 238 00:10:08,720 --> 00:10:11,030 more specific for example 239 00:10:11,040 --> 00:10:13,350 than just an element selector so we have 240 00:10:13,360 --> 00:10:16,470 our class here that has the color gray 241 00:10:16,480 --> 00:10:19,269 and it is overriding the red now if we 242 00:10:19,279 --> 00:10:22,230 remove this class and we save 243 00:10:22,240 --> 00:10:24,790 now they're all red but even though 244 00:10:24,800 --> 00:10:26,550 we're looking at the cascade and 245 00:10:26,560 --> 00:10:29,269 starting from the top down even if i put 246 00:10:29,279 --> 00:10:31,509 this class above 247 00:10:31,519 --> 00:10:34,310 the paragraph selectors and save 248 00:10:34,320 --> 00:10:36,230 it still applies to these last two 249 00:10:36,240 --> 00:10:38,710 paragraphs because it has more 250 00:10:38,720 --> 00:10:40,389 specificity 251 00:10:40,399 --> 00:10:42,870 than an element selector and the element 252 00:10:42,880 --> 00:10:45,910 selector here purple of course is 253 00:10:45,920 --> 00:10:47,190 overridden 254 00:10:47,200 --> 00:10:49,269 with the last rule being the element 255 00:10:49,279 --> 00:10:51,350 selector here for red so we start out 256 00:10:51,360 --> 00:10:53,750 with a red paragraph and it's really 257 00:10:53,760 --> 00:10:56,790 applied to all paragraphs except that 258 00:10:56,800 --> 00:10:57,670 then 259 00:10:57,680 --> 00:11:01,110 css sees there is a gray class applied 260 00:11:01,120 --> 00:11:03,509 to the second and third paragraphs and 261 00:11:03,519 --> 00:11:05,990 that has more specificity so it 262 00:11:06,000 --> 00:11:09,430 overrules whatever is set by the element 263 00:11:09,440 --> 00:11:11,829 selector now a problem you can have is 264 00:11:11,839 --> 00:11:14,630 when you have a large css file and maybe 265 00:11:14,640 --> 00:11:17,430 you have set this gray class on these 266 00:11:17,440 --> 00:11:20,310 paragraphs and later on let's say you're 267 00:11:20,320 --> 00:11:23,030 way down a couple of hundred lines and 268 00:11:23,040 --> 00:11:25,269 you're setting a color on a paragraph 269 00:11:25,279 --> 00:11:27,430 and you can't understand why it's not 270 00:11:27,440 --> 00:11:29,910 being applied so let's go ahead and 271 00:11:29,920 --> 00:11:32,949 expand chrome here and you can right 272 00:11:32,959 --> 00:11:35,670 click and choose inspect 273 00:11:35,680 --> 00:11:38,310 and once you inspect your page 274 00:11:38,320 --> 00:11:40,230 then you can look at the different 275 00:11:40,240 --> 00:11:43,030 classes and css rules that are applied 276 00:11:43,040 --> 00:11:44,870 so here's a good example of that we have 277 00:11:44,880 --> 00:11:47,030 got our third paragraph highlighted up 278 00:11:47,040 --> 00:11:49,269 here in the elements tab 279 00:11:49,279 --> 00:11:52,150 and then we're looking at the styles and 280 00:11:52,160 --> 00:11:55,350 we can see we've got paragraph rules 281 00:11:55,360 --> 00:11:57,350 here these element selectors but color 282 00:11:57,360 --> 00:12:00,470 purple is crossed out and then color red 283 00:12:00,480 --> 00:12:03,509 is crossed out so css knows these rule 284 00:12:03,519 --> 00:12:05,750 sets exist for paragraph but it also 285 00:12:05,760 --> 00:12:08,230 knows it should follow what is the most 286 00:12:08,240 --> 00:12:11,190 specific using the rules of specificity 287 00:12:11,200 --> 00:12:13,990 so here we have a color gray that is 288 00:12:14,000 --> 00:12:16,310 being applied so you can see 289 00:12:16,320 --> 00:12:17,910 sometimes when you're struggling with 290 00:12:17,920 --> 00:12:20,230 your css and you have written a rule and 291 00:12:20,240 --> 00:12:22,790 you're not understanding why it is not 292 00:12:22,800 --> 00:12:25,110 being applied you should inspect your 293 00:12:25,120 --> 00:12:27,670 code with dev tools and once again you 294 00:12:27,680 --> 00:12:29,910 right click and choose inspect to do 295 00:12:29,920 --> 00:12:31,829 that and then just go to the correct 296 00:12:31,839 --> 00:12:33,350 tabs elements 297 00:12:33,360 --> 00:12:35,509 highlight the element that you want look 298 00:12:35,519 --> 00:12:37,990 at the styles and you'll see how it is 299 00:12:38,000 --> 00:12:40,870 being read by css in the browser or how 300 00:12:40,880 --> 00:12:43,590 the browser is reading your css and then 301 00:12:43,600 --> 00:12:45,509 see how those rule sets are being 302 00:12:45,519 --> 00:12:47,750 applied which ones are being overridden 303 00:12:47,760 --> 00:12:49,190 and which ones are actually being 304 00:12:49,200 --> 00:12:51,590 applied okay i'm going to close that out 305 00:12:51,600 --> 00:12:54,150 now and i'll resize chrome so i'll come 306 00:12:54,160 --> 00:12:56,069 back over here to our style and once we 307 00:12:56,079 --> 00:12:57,670 go to the top let's talk about 308 00:12:57,680 --> 00:13:00,389 inheritance as i got into specificity a 309 00:13:00,399 --> 00:13:02,949 little bit already but inheritance 310 00:13:02,959 --> 00:13:04,470 that is where 311 00:13:04,480 --> 00:13:07,829 another element inherits the settings 312 00:13:07,839 --> 00:13:11,110 from or the properties from its parent 313 00:13:11,120 --> 00:13:14,710 element so the body element is parent to 314 00:13:14,720 --> 00:13:17,269 every other element here and so when we 315 00:13:17,279 --> 00:13:19,590 set the font size all of these other 316 00:13:19,600 --> 00:13:22,790 elements inherited that font size now 317 00:13:22,800 --> 00:13:25,190 typically anything related to font or 318 00:13:25,200 --> 00:13:26,550 typography 319 00:13:26,560 --> 00:13:29,030 is inherited and that can include things 320 00:13:29,040 --> 00:13:31,990 like color line height alignment all 321 00:13:32,000 --> 00:13:34,150 sorts of settings dealing with the font 322 00:13:34,160 --> 00:13:36,230 and the typography however any 323 00:13:36,240 --> 00:13:38,310 properties that are not related to those 324 00:13:38,320 --> 00:13:40,949 things are not inherited and i know 325 00:13:40,959 --> 00:13:42,230 that'll just take a little bit of 326 00:13:42,240 --> 00:13:44,629 getting used to as you learn more about 327 00:13:44,639 --> 00:13:47,829 css so for example this font size was 328 00:13:47,839 --> 00:13:50,230 inherited but another setting we could 329 00:13:50,240 --> 00:13:52,550 put is a border and i'll put three 330 00:13:52,560 --> 00:13:55,910 pixels so it's easy to see solid black 331 00:13:55,920 --> 00:13:59,110 and i'll save this now notice the border 332 00:13:59,120 --> 00:14:01,990 only went around the body element it 333 00:14:02,000 --> 00:14:04,069 didn't apply to all the other elements 334 00:14:04,079 --> 00:14:07,030 inside of the body so that's an example 335 00:14:07,040 --> 00:14:09,509 of something that is not inherited i'll 336 00:14:09,519 --> 00:14:11,430 go ahead and remove that again now we 337 00:14:11,440 --> 00:14:13,509 did briefly talk about that universal 338 00:14:13,519 --> 00:14:15,590 selector let me go back to that and show 339 00:14:15,600 --> 00:14:18,389 you that this is not inheritance this is 340 00:14:18,399 --> 00:14:22,069 actually selecting all elements so here 341 00:14:22,079 --> 00:14:24,150 if i said a border 342 00:14:24,160 --> 00:14:27,509 i'll put one pixel solid red 343 00:14:27,519 --> 00:14:30,629 and save notice it does apply to all 344 00:14:30,639 --> 00:14:32,710 elements that's because we selected all 345 00:14:32,720 --> 00:14:35,269 the elements here so setting something 346 00:14:35,279 --> 00:14:38,790 here with the universal selector is not 347 00:14:38,800 --> 00:14:41,189 going to cause inheritance really 348 00:14:41,199 --> 00:14:43,509 because it's actually selecting all the 349 00:14:43,519 --> 00:14:45,750 elements itself okay i'll remove the 350 00:14:45,760 --> 00:14:47,670 universal selector again but getting 351 00:14:47,680 --> 00:14:50,389 back to just the inheritance overall 352 00:14:50,399 --> 00:14:52,870 this can be very handy because it keeps 353 00:14:52,880 --> 00:14:55,910 us from writing repeated code in other 354 00:14:55,920 --> 00:14:58,310 words it keeps our code dry which is an 355 00:14:58,320 --> 00:15:00,150 acronym that stands for don't repeat 356 00:15:00,160 --> 00:15:02,389 yourself so we want to write less code 357 00:15:02,399 --> 00:15:04,949 and be more efficient and inheritance 358 00:15:04,959 --> 00:15:07,750 helps us do that now some things are not 359 00:15:07,760 --> 00:15:09,350 inherited like i mentioned the border 360 00:15:09,360 --> 00:15:12,550 would be one of those also form elements 361 00:15:12,560 --> 00:15:15,030 do not inherit say the font size and 362 00:15:15,040 --> 00:15:17,509 other typography settings so it wouldn't 363 00:15:17,519 --> 00:15:19,350 be that unusual 364 00:15:19,360 --> 00:15:21,269 to see something like this that would 365 00:15:21,279 --> 00:15:25,750 take a button and let's say an input 366 00:15:25,760 --> 00:15:28,150 text area and let me see if i'm leaving 367 00:15:28,160 --> 00:15:30,790 anything else out maybe a select 368 00:15:30,800 --> 00:15:33,430 anything like that and we might see then 369 00:15:33,440 --> 00:15:37,030 font and set it to inherit and that 370 00:15:37,040 --> 00:15:39,670 would go ahead and make those elements 371 00:15:39,680 --> 00:15:42,470 inherit the font settings because just a 372 00:15:42,480 --> 00:15:44,790 mental note that form elements do not 373 00:15:44,800 --> 00:15:47,430 typically inherit those font settings 374 00:15:47,440 --> 00:15:49,590 but now to take advantage of inheritance 375 00:15:49,600 --> 00:15:52,150 and write dry code we can use the body 376 00:15:52,160 --> 00:15:54,870 element which appears only once per page 377 00:15:54,880 --> 00:15:57,749 or we could use the html element 378 00:15:57,759 --> 00:15:59,670 and either would work 379 00:15:59,680 --> 00:16:01,749 oftentimes if you're just starting out 380 00:16:01,759 --> 00:16:03,509 and just remembering it might be best to 381 00:16:03,519 --> 00:16:05,350 just use the body 382 00:16:05,360 --> 00:16:08,310 oftentimes i use the html element 383 00:16:08,320 --> 00:16:09,910 because once again the inheritance 384 00:16:09,920 --> 00:16:12,389 applies there so we can save here 385 00:16:12,399 --> 00:16:14,470 without see the difference now i'll go 386 00:16:14,480 --> 00:16:16,710 ahead and apply it in html and once 387 00:16:16,720 --> 00:16:19,509 again we have the larger font because 388 00:16:19,519 --> 00:16:22,150 there are for me at least some specific 389 00:16:22,160 --> 00:16:24,870 settings for the body and i like to see 390 00:16:24,880 --> 00:16:27,509 those separate so i often put all of my 391 00:16:27,519 --> 00:16:30,629 font settings in the html selector but 392 00:16:30,639 --> 00:16:32,870 again either would work one other 393 00:16:32,880 --> 00:16:36,150 element selector that i do use at times 394 00:16:36,160 --> 00:16:38,790 is the main element it is a semantic 395 00:16:38,800 --> 00:16:40,629 element and it should appear only once 396 00:16:40,639 --> 00:16:42,629 per page but then you have to be aware 397 00:16:42,639 --> 00:16:44,870 that you're only selecting the things 398 00:16:44,880 --> 00:16:47,189 inside of the main element so if we look 399 00:16:47,199 --> 00:16:50,230 at our page i've got the h1 inside of a 400 00:16:50,240 --> 00:16:52,310 header element and not inside of the 401 00:16:52,320 --> 00:16:54,790 main element so if i were to change 402 00:16:54,800 --> 00:16:56,389 the font family 403 00:16:56,399 --> 00:16:58,550 inside of our main selector and i 404 00:16:58,560 --> 00:17:01,110 changed this to mono space which is easy 405 00:17:01,120 --> 00:17:02,949 to see the change with 406 00:17:02,959 --> 00:17:05,909 go ahead and save you can see 407 00:17:05,919 --> 00:17:08,150 all of the articles switched to mono 408 00:17:08,160 --> 00:17:10,710 space but our h1 heading is still using 409 00:17:10,720 --> 00:17:13,029 the other font okay coming back to 410 00:17:13,039 --> 00:17:15,029 specificity just something we talked 411 00:17:15,039 --> 00:17:17,590 about so a quick review there element 412 00:17:17,600 --> 00:17:19,990 selector is the least specific 413 00:17:20,000 --> 00:17:22,069 and then a class selector is more 414 00:17:22,079 --> 00:17:24,549 specific and then although we don't want 415 00:17:24,559 --> 00:17:27,590 to use them in our css an id selector is 416 00:17:27,600 --> 00:17:29,990 even more specific than that 417 00:17:30,000 --> 00:17:32,390 sometimes something's not working out 418 00:17:32,400 --> 00:17:34,150 and you're wanting to figure out why 419 00:17:34,160 --> 00:17:35,990 it's not working out or you just can't 420 00:17:36,000 --> 00:17:38,310 figure it out and you get frustrated i'm 421 00:17:38,320 --> 00:17:39,750 going to show you something that will 422 00:17:39,760 --> 00:17:42,230 work but i'm going to recommend that you 423 00:17:42,240 --> 00:17:44,630 do not use it it's kind of the nuclear 424 00:17:44,640 --> 00:17:46,789 option if you will and it's to put an 425 00:17:46,799 --> 00:17:49,590 exclamation mark and put important now 426 00:17:49,600 --> 00:17:51,990 see how red should be applied to this 427 00:17:52,000 --> 00:17:54,789 first paragraph but with the important 428 00:17:54,799 --> 00:17:57,430 flag right here after our purple setting 429 00:17:57,440 --> 00:17:59,430 and now i save 430 00:17:59,440 --> 00:18:01,029 notice what happened 431 00:18:01,039 --> 00:18:03,110 it turned the first paragraph purple 432 00:18:03,120 --> 00:18:05,190 notice what else happened it also turned 433 00:18:05,200 --> 00:18:06,950 the second paragraph and the third 434 00:18:06,960 --> 00:18:09,830 paragraph purple now why is that well 435 00:18:09,840 --> 00:18:12,549 this important essentially ruins 436 00:18:12,559 --> 00:18:15,110 everything else it overrides everything 437 00:18:15,120 --> 00:18:16,070 so 438 00:18:16,080 --> 00:18:17,909 really when you see it in code it's kind 439 00:18:17,919 --> 00:18:19,990 of an indication that it's not well 440 00:18:20,000 --> 00:18:22,470 organized or it's sloppy there's only a 441 00:18:22,480 --> 00:18:24,789 few reasons you would really want to use 442 00:18:24,799 --> 00:18:27,110 this and i feel i mean i'm hesitant to 443 00:18:27,120 --> 00:18:28,950 even show it because i feel like 444 00:18:28,960 --> 00:18:31,909 beginners could abuse this however 445 00:18:31,919 --> 00:18:33,669 you'll see it somewhere i just want you 446 00:18:33,679 --> 00:18:35,510 to know it exists 447 00:18:35,520 --> 00:18:37,510 don't use it don't use it you need to 448 00:18:37,520 --> 00:18:39,909 learn how to organize your code and 449 00:18:39,919 --> 00:18:43,110 apply it correctly and only after you've 450 00:18:43,120 --> 00:18:46,710 learned css well enough to understand 451 00:18:46,720 --> 00:18:49,270 when to use that important flag that's 452 00:18:49,280 --> 00:18:50,789 the only time you should use it you 453 00:18:50,799 --> 00:18:52,950 should really not give up the struggle 454 00:18:52,960 --> 00:18:55,029 you will learn more by struggling and 455 00:18:55,039 --> 00:18:58,070 learning how to apply these selectors in 456 00:18:58,080 --> 00:18:59,990 the proper way without using that 457 00:19:00,000 --> 00:19:02,230 important flag i do want to show you one 458 00:19:02,240 --> 00:19:04,630 thing that will help you learn and that 459 00:19:04,640 --> 00:19:07,190 is the specificity calculator and i will 460 00:19:07,200 --> 00:19:09,190 give a link to this in the resources you 461 00:19:09,200 --> 00:19:10,789 can see they've got some 462 00:19:10,799 --> 00:19:12,710 basic values here that are already 463 00:19:12,720 --> 00:19:14,710 fairly complicated but you can zero 464 00:19:14,720 --> 00:19:16,470 these out and you can compare two 465 00:19:16,480 --> 00:19:19,430 selectors to see why they are working 466 00:19:19,440 --> 00:19:21,750 and why they aren't working or why one 467 00:19:21,760 --> 00:19:23,750 overrules the other you can see an 468 00:19:23,760 --> 00:19:26,549 element selector here for an h2 has a 469 00:19:26,559 --> 00:19:28,870 score of 0 0 1 470 00:19:28,880 --> 00:19:31,990 but if i put in my class of gray it has 471 00:19:32,000 --> 00:19:35,270 a score of 0 1 0 in other words 10 472 00:19:35,280 --> 00:19:37,350 which is a higher score 473 00:19:37,360 --> 00:19:41,110 than one so the class overrules the 474 00:19:41,120 --> 00:19:43,510 element selector and likewise if we put 475 00:19:43,520 --> 00:19:45,750 in an id selector 476 00:19:45,760 --> 00:19:47,350 like we had second 477 00:19:47,360 --> 00:19:50,870 it has a score of 100 over 10 so 478 00:19:50,880 --> 00:19:52,789 that is better now you could apply more 479 00:19:52,799 --> 00:19:54,870 than one class for example to an element 480 00:19:54,880 --> 00:19:56,549 so let's see what happens if we had a 481 00:19:56,559 --> 00:19:58,390 second class and we had our highlight 482 00:19:58,400 --> 00:19:59,430 class 483 00:19:59,440 --> 00:20:02,070 now this has a score of 20 but it's 484 00:20:02,080 --> 00:20:04,470 still not going to be as high as a score 485 00:20:04,480 --> 00:20:07,669 of 100 so this specificity calculator 486 00:20:07,679 --> 00:20:10,390 will certainly help you understand why 487 00:20:10,400 --> 00:20:12,390 one rule is being applied or why one 488 00:20:12,400 --> 00:20:14,549 selector i should say is being applied 489 00:20:14,559 --> 00:20:16,710 over another remember to keep striving 490 00:20:16,720 --> 00:20:19,110 for progress over perfection and a 491 00:20:19,120 --> 00:20:21,350 little progress every day will go a very 492 00:20:21,360 --> 00:20:22,549 long way 493 00:20:22,559 --> 00:20:24,149 please give this video a like if it's 494 00:20:24,159 --> 00:20:26,310 helped you and thank you for watching 495 00:20:26,320 --> 00:20:27,990 and subscribing you're helping my 496 00:20:28,000 --> 00:20:29,190 channel grow 497 00:20:29,200 --> 00:20:31,270 have a great day and let's write more 498 00:20:31,280 --> 00:20:35,320 code together very soon 34960

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.