02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Best practices when it comes to CSS rules

As your CSS grows so does the effort of the HTML browsers to render your code. We will spend some time talking about how you can build your code more effectively to make it easier for the browser to process your pages.

Video Transcript

00:00Before we move on to the next chapter,
00:03I wanted to take a slight break
00:04from learning new concepts,
00:06to really talking about something,
00:07well, it is a new concept,
00:09which is about what is the best way
00:11to create CSS that performs the best.
00:13And that's gonna be the topic of this lecture
00:15as we wrap up this chapter.
00:17So let's jump right into it.
00:19For the sake of the next sample,
00:21I'm gonna go ahead and add a little bit more
00:23into my code.
00:24To do that, I really wanna embed a font.
00:27And what I'm gonna do is I'm gonna go to Google Fonts
00:30and just select a font that I want.
00:33Now, notice when you're selecting a font
00:34that is gonna give you the name of the font,
00:36like Roberto or O.S. Wald, and so forth.
00:42Find the name of the font that you wanna work with.
00:45And then go back into your HTML, into your index,
00:48and I want you to go ahead even before your CSS
00:51and add in, and you'll find it inside of our source files,
00:55a direct path to the Google Library fonts.
00:58Now, all that is left for me to do
01:01is to define the font that I wanna use.
01:02In this case, I'm using Acculonica.
01:07Now, when I'm using Acculonica here,
01:09really, now that I defined it,
01:11now that I said it's gonna automatically
01:12download the font into the user's browser.
01:16As we do that, the next step is inside of our CSS,
01:19I could go ahead and don't worry about the syntax,
01:21it's really just a link and we're just linking the path.
01:24So make sure that you just grab the link
01:25inside of our source files
01:27and place it inside of your index
01:29right before our styling.
01:30And in our styling, I'm just gonna go ahead,
01:32for example, into my H1,
01:35and I'm just gonna add here another property.
01:36Notice how I'm breaking a line.
01:38This time around, I'm gonna use font family,
01:40and I'm gonna set it to be that string.
01:43Now, I could set it with wrap it around with a string,
01:45which is probably the better commonality,
01:47the better way to do it.
01:48And if I go ahead and save this,
01:50we'll see that for a global H1,
01:52that global H1 should stand apart from our other fonts
01:56and should have a different font.
01:58And notice how it's now set in a completely different font
02:00than all the rest.
02:02Now, if I wanted also all my other items
02:04to have that font, I'm just gonna go ahead
02:07and also for any question here,
02:09I'm gonna go ahead and just set that font family.
02:11So I'm just gonna copy it, set it inside.
02:14And really what I'm doing here is a double mission here
02:18because I wanna show you something
02:19that is very commonly done online
02:21that is not necessarily the best way of doing things.
02:24They're just gonna go ahead here,
02:26and now I'm gonna set all these fonts
02:28to have that specific font family inside of them.
02:32Now, if I go ahead and click on refresh,
02:33we'll see that now all the fonts have been updated
02:36to use this font that we want to be using.
02:39Now, the only problem with this logic,
02:42and if I just go ahead here and click on here
02:43and to inspect, oh, what am I doing?
02:45Click here to inspect the element.
02:47We'll see that it's using that font.
02:49If we click onto this element,
02:50we'll see that it's using that font,
02:52and so on and so forth.
02:54Now, the only problem that we have here
02:56is that we're really overusing our typing.
02:59We added in a lot of extra typing
03:01that if there was a better way of doing it,
03:04wouldn't it be wonderful?
03:06And there absolutely is.
03:07Now, in our case, if I wanted to make sure,
03:10now, this is in general a rule in CSS
03:12is we batch in together all the rules
03:15that are gonna be replicated into the highest commonality.
03:19So in this case, I could say, hey,
03:20every H1, every H2, and every H3,
03:23no matter which one you are,
03:25you're gonna use that font.
03:27Once I've done that, I don't have to define it
03:29in every specific rule, and now I'm, again,
03:32cutting down the file size and cutting down
03:34the amount of references and directions
03:37that I need to put in.
03:39So this is a really important element here.
03:41So for example, in this case,
03:43I didn't really need this,
03:45and let me just undo this for a second
03:46because I deleted the wrong line.
03:47I deleted the 45 by mistake here,
03:50but that's basically what I'm talking about.
03:52You wanna basically, whatever is applicable
03:55to more than one item,
03:57don't put it in specifically that item.
03:59For example, if we wanted Alconica, this font family,
04:03to be for everything in our body,
04:04then I would put that in our body,
04:06and I would not refer it to every single item.
04:08So the more global you go,
04:10the better because you're gonna cut down on file size,
04:13and you're gonna cut down also on the amount of processing
04:16that the browser has to do.
04:17There's less overriding here.
04:19Well, really, there is no overriding in this situation.
04:22That is true, by the way, also in this case.
04:25To avoid overriding, what I could do,
04:27is in my footer, is literally take this color attribute
04:30that we've added here, if I just copy it,
04:33and I just wanna paste it inside
04:35and create it as a standalone attribute,
04:37and then I could go ahead and delete it.
04:39Although I added a little bit more characters,
04:41what I did here is I prevented an override
04:44that is not needed.
04:45These are global rules that are applicable
04:47to all these items,
04:48while these are specifically only to these items.
04:51And if I go ahead and I click on refresh,
04:53I should be getting the exact same result.
04:55The only difference is,
04:56is that I'm making it a much more easier,
04:58I'm making it much more easy on the processor,
05:01on the browser to process it,
05:02and I'm also in the process, for the most part,
05:05making my file smaller,
05:07having less things in my item
05:10and a lot less than I need.
05:12So that finishes our chapter two.
05:14And our third chapter,
05:15we're gonna start digging into attributes
05:17and really starting with color,
05:19and that's gonna be our main topic of the next chapter.
05:21So I hope you enjoyed this chapter,
05:23and we'll see you in the next one
05:24when we start exploring much more deeply
05:26into the way properties are configured,
05:28taking the colors as the case and sample.
05:32See you in the next lecture, in the next chapter.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!