Moving CSS Styles to External Files
Until now all of our styling was done globally to the whole page or styling using the style attribute. In this session we will learn how to export our CSS into an external file. In general it's a great idea to put your CSS in an external file so you can share it with other pages. Most websites have one major CSS file that is used throughout all the pages of the site.
Video Transcript
00:00Alright, so we said that there are three different Ways of working with CSS
00:03we said that you can do it inline by going directly to a paragraph
00:07we said that you could set the style attribute in your head
00:10and then put inside the information of your CSS
00:13now there's a third way that I want to show you how to do
00:15which is very commonly way of using CSS and the reason is very simple
00:19when you're building a website that has 10, 20, 30 pages
00:23There's a lot of common denominators
00:25There's absolutely no reason in the world to put your CSS directly into your page
00:29because many times your CSS is a global thing throughout your application
00:32such as your overall style of your site.
00:36To do that we could actually put our information in a separate CSS file
00:40So I created a separate CSS file
00:42Oh.. And I mixed the things up together as always
00:46Alright, so I created a separate CSS file
00:48and we're going to keep that one empty for a second
00:50and in our link that HTML file I'm going to grab the CSS information, cut it out
00:56I could even delete the style attribute and place that information
00:59directly inside of this new CSS file that was just created
01:04Now the only thing that's left for me is to link between the files
01:07to basically tell the HTML file that this is the CSS file of that page
01:13now by the way one other really powerful usage of it is
01:16for example you might have different CSS for if it's a mobile device or
01:20If a user is coming from a mobile browser or they're coming from a non-mobile
01:25you could have a lot more control over your CSS
01:27creating different alternative versions of your website
01:30depending on where your users are coming from
01:32or for example even as you have in blogs and forms
01:35where in a form many times you could change the style
01:38or change the view of a page
01:39so you might have for example a darker view and a lighter view
01:43for people with disabilities or so on and so forth
01:45and you could switch on and switch off different capabilities
01:48by adding CSS attributes and removing them
01:50which is a really really powerful control mechanism
01:53that is really only capable once you take the files outside of your HTML file
01:57and with that said let's look at the HTML tag
02:00that we do that with and it's called link
02:02now the link itself asks for what is it linking
02:06which is a Rel which is relative or reliance
02:09or whatever you want to call it you put inside there what it is
02:11and we're going to put inside that it's a stylesheet
02:14so we're linking inside your application a stylesheet.
02:18Now, historically we also would say what type of stylesheet
02:21because there are a lot of different types of stylesheets out there but
02:24The winner was CSS and there's no need for you to even know about other types
02:28and there's actually no need for you to use the type attribute anymore
02:32and which is HTML5 world that we live in is just not needed
02:35and to be honest even in HTML4 everyone only used CSS
02:39so if you don't put it in, the browser will know how to recognize and
02:42know that you're actually talking about CSS Alright?
02:45So all that's left's for us to put a href which you know what href's are,
02:49with a link to our actual file which in our case is 8002.link.CSS
02:56and I'm going to close my link up and because we're working in HTML5
03:01which is not a strict open close policy as much as XHTML is
03:06we don't have to close the link
03:08but if you want to, I personally usually am inclined to,
03:11I will do a self close to my link because it just makes me feel good.
03:14the browsers don't really care if you do a self close or not
03:17I personally do care Alright?
03:19So i'm going to do that Self close
03:20basically saying there's no content inside of this link
03:23beyond the external information that we sent out.
03:26Now that we've done that we can go into our browser
03:28and literally just load in our styled elements
03:31so I'm going to go into my link HTML file and
03:34you'll see that we're going to get the exact same result
03:36but this time around our style information is in an external file
03:41Alright? That make sense? -Yes it does
03:43Cool, so could you tell them again the 3 ways we could put CSS, by heart.
03:51Umm... (laughing)
03:57I have to go home and do my homework
03:59No, no, come on. So we just did one of them what was it?
04:04-The stylesheet? -OK, so external stylesheet
04:07-Right -Right
04:09-At the top with....yeah -with the style tag, right.
04:12- And the third one? - At the beginning with the header
04:17that was the second one we said inline inline in the paragraph itself
04:21you don't have to memorize it but there you go
04:23so if you knew it even better than my mama then you're doing good
04:27- I'm going to study this - you should study
04:29-Yeah, it's like... - yeah alright! See you in the next video
