Moving our CSS logic into a separate file
In the last few lectures we created a rule that was being used in our HTML document but couldn%u2019t be used in our website. By the end of this lesson you will know how to apply the CSS you created in the website and not only one web page at a time.
Video Transcript
00:00In the last lecture we took the first step into the powers of CSS where in the last lecture
00:05we actually created a rule that could then be reused.
00:09The next step is even more vital, we're going to take that styling rules that we
00:12created and put them in external file.
00:15That enables us to do a few things, one is we could reuse this file throughout our project
00:20which means more than one HTML file.
00:23For example if your site has a header and a footer and they're all exactly the same
00:26it would be silly to recreate those files in every separate file from one the user
00:30would have to load those files more than ones and two if you'd want to make a
00:34change you'd have to make a change in a lot of places.
00:36So we're going to extract our CSS into external file don't worry it's easy it's
00:40fun and we're going to do it right away.
00:43In the last lesson we moved some of our style rules into an upper style element.
00:49The problem with that is that what if we have a lot of files on our server
00:53which is very very common.
00:56Beyond that what if we want to cache elements.
00:59So one of the problems with putting everything in the same place is if you make a small change
01:02to your CSS or to your HTML then you have to re-serve it all all over to your user.
01:10The separation of the three HTML, CSS and JavaScript enables you to make a change in
01:14one without changing all the rest making the load time faster for your user.
01:20Beyond that the capability of reusing your code in different pages which is very
01:25very common and very encouraged in HTML style development.
01:31So the next thing that I want to do is I want to go to my style.css file I created that
01:36file earlier which you could find inside of your CSS folder.
01:40It is an empty file called style.css I'm going to go ahead and just copy everything inside
01:46of my style directive that we've created and literally all that I want to do next is go
01:52ahead and I'm just going to comment out the style because we're not going to need it anymore
01:57and instead of that in our head I'm going to go ahead and say by the way I want to get
02:02a link to this element I want to get a link and I want to link it directly to my element
02:07I don't have to write anything beyond that behind saying that the relative what's inside
02:12of this it's a style sheet and because I'm working in the world that is CSS3 world
02:18I don't have to define the type there is only CSS as the type and I'm going to go ahead
02:23here and set my link as an href and just close that link tab notice how I'm not putting here
02:28a close you could but you don't need to because part of the rules of HTML are defined
02:34without it but you could go ahead and put that if it makes you feel better about closing
02:38your HTML in this way all that's left for me to do is go ahead and set my CSS to
02:43be in that style that CSS and if I save this go back into my application and click on refresh
02:49we should see the same application now the only problem we're having here probably is
02:53that one is maybe we didn't save the file or maybe the path that we set was not correct
02:59so we're going to go ahead here and just make sure that our style that CSS first of all
03:03is right here that it's updated that it's saved and once we know that all these items are true
03:10I'm just going to go ahead here and just save it one more time and click on refresh and hopefully
03:14we'll have it everything carry through which it does so that's it now in general we really want
03:21to stick to putting all of our CSS into an external file that is what also search engines
03:26want you to do that is the common way of doing it but once in a while if you don't have control
03:32over that HTML you might end up putting things directly into the body itself or adding it into
03:38the style up above but if you do have the control of adding an external file in the environment
03:43that you're in that is a really really great way to define your rules because then you're making
03:49sure that you could reuse it throughout and all the other goodies we talked about previously
03:53so that's it for this lesson and we're really done with the rules for this chapter because in
03:58the next lesson we're going to continue and practice and explore a few more properties as we
04:04practice the idea of creating rules in CSS
