Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
Writing a selector is really only half of properly writing a CSS rule. Once you've targeted an element on the page with a selector, you then need to define the property or properties that you wish to set to control the style of that element. So let's experiment with setting a few properties for our page element. So I'm going to go over in our exercise files to the 01_05 folder and I'm going to open up properties.htm. So again, really basic page here. We can see that I have some placeholder rules, already written some selectors there h1, h2, p, div, and if I look at the structure of the page, it's exactly what we had last time.
Same structure: the heading one followed by a paragraph, then we have a div with a heading two inside of that, and a paragraph inside of that as well. Okay, so I'm going to go back up to these embedded rules that we have here, and I'm just going to add some properties to the existing selectors. Now CSS doesn't care whether you do it all on one line or multiple lines. I have these set up for multiple lines and you'll see, in lot of instances tutorials out there, a lot of programs that write CSS for you, they'll go ahead and do it in multiple lines, and that's because it's just easier for people to read it. It's easier for you and I to see exactly what's going on here.
But as we'll test in just a moment, CSS itself and browsers really don't care if it's on one line or multiple lines. So I'm just going to click in the sort of blank line right here in the h1 rule, and I'm going to type in "font-family: Arial" and then a semicolon. So the syntax of all our properties is set up like this. The property, font-family, is followed by a colon and then the value for that, in this case the font Arial, follows that.
And then that's followed by a semicolon that tells it to stop evaluating this line and go on to the next one. Now you can have as many properties as you need to within a declaration, so I'm going to follow font-family with font-size and I'm going to need this to be 18 pixels. So when you're typing in a value, you have to know how that value is formatted. In this case, font-size is if I'm going to use pixels--I use a px--and that is immediately following the amount I want, so there's no space between those guys at all.
Now I'm going to go ahead and do the same exact thing for the h2, the paragraph, and the div tag. I'm just going to change things up slightly here and there. So here I want font-family. You can see that Aptana Studio is giving me some code-completion hinting. And if chose that and hit Return, it would go ahead and finish that for me and type the colon in for me. That's extremely helpful. So if your CSS editor that you're using has that, feel free to go ahead and use it. But I recommend actually typing it out the first few times so that you're really used to how the syntax works.
If you get too used to a program doing it for you, you don't really understand why the syntax is doing what it's doing and what a specific character is being used for. All right! So again I'm going to make that be Arial; I'm going to go down to the next line, just like last time. I'm going to do font-size, and this is instead of 18 pixels going to 16 pixels, so it's a little bit smaller. And we're going to go down to the paragraph. In here we're going to do font-family: Arial: font-size: 14 pixels. So similar, but a little bit smaller.
Now I'm going to go down to the div selector, and here I'm going to do something entirely different. Here I'm going to do background-color. I'm going to type in #ccc, and you can see again, Aptana Studio is helping me out here. And then for padding, I'm going to do 10 pixels. Now at this point, you might be saying, wow! background-color: #ccc, what does that mean, padding: 10 pixels? Don't worry about the exact meanings of all these different items yet. We're going to go into this in a lot more detail. Right now, we're just focusing on the syntax of the properties themselves.
So I'm going to go ahead and save this, and this time I'm going to test it in a different browser. I'll test it in Firefox. And we can see that we're getting Arial as our font that's being used. The size is a little bit different depending upon what we're looking at. And of course, we have that background color for the div tag that you're seeing there as well. Excellent! Okay, now I mentioned earlier that syntax-wise properties don't have to be all on separate lines. Let me show you what I mean. I'm going to go back into Aptana and if I go back up to my h1, notice that I could just go ahead and put that all on one line.
As a matter of fact, if I wanted to, I could go ahead and put everything on one line, including the curly braces. I could save this and if I go back out to the browser and refresh that, no change whatsoever. So the browser itself, or the user agents, typically do not care whether it's on multiple lines or not. The reason that most syntax is written on multiple lines, as you see here, it's just a little bit easier to read this than it is to read this. Now when I am talking about writing properties for multiple elements, you can see that all the text elements on the page-- the heading 1, the heading 2, paragraph-- they're all using Arial.
So even though their font sizes are a little bit different, there is a lot of consistency there. Well, there are several different ways to write that. For example, if I wanted to--I'm just going to create a little blank space up here at the top-- I could just group all these selectors together. I could say h1, h2, p, and then font-family: Arial. And then I could just go ahead and remove Arial from all these individual rules. So I could just go ahead and remove that font-family.
This has everything to do with efficiency. There's really no point in declaring the same font family over and over again, because what happens if you decide to change it? Okay, now I could save this and I could preview this in the browser. And again, no change whatsoever. It's exactly the same. But now it's a lot easier for me to modify this at a future date. So if I decided that I didn't like Arial anymore and I wanted to use, say, well I don't know, Georgia, I could do that, and when we preview that in our browser, we can see that the font changes. So rather than having to change that three or four times, or five or six times, I just changed it on the one rule and it went ahead and changed it for everybody.
Now even this group selector isn't as efficient as it could be. So we have heading 1, heading 2, and a paragraph specified here, but what if we wanted every single element on a page to use this font? Writing a group selector for every single element on the page would be really, really long. Well, one of the things that I can do is I can replace the selector with just body. The body tag surrounds all of our visual elements. So you can see body tag is around all of them. CSS has a guiding property called inheritance, and we're going to look at inheritance a little bit later on.
But what inheritance says basically is that if a parent element is styled, then those properties can be inherited by all child elements. So now by putting font-family: Georgia on the body selector--I'm going to save this-- if I go back to my browser and preview that again, I see no change. But what's really cool now is that every single element within the page is now going to be Georgia. And if I want to update that, I can simply update it one time. So really again, it's all about efficiency. At this point, you might be looking through all of these different properties and the values that we used here and say to yourself, how in the world am I going to memorize every single property that there is and then memorize all the acceptable values for each one? It does seem a little overwhelming at first.
Don't panic, first off. I'm going to switch over to my browser and show you something that can help you out. So this is the Full property table. This is Appendix F of the CSS 2.1 specification. You can find it at the w3.org/TR/CSS21/ propidx.html, and you can just go to the 2.1 specification and find this appendix as well. But as you can see, if I scroll through these properties, there's really not that many of them. I mean, there is a fair amount, and they're adding more to it, but on a whole, it's not that many.
And to be honest, when you want to learn more about a property, you can go directly to this page. You can find out what acceptable values it has. You can even click on the property itself and go read more about that property. So this is a very quick and easy way to learn not only the properties themselves, but what their acceptable values are. Now as you get more comfortable writing your styles, you'll find that your common properties that you use frequently, those are just going to become second nature to you, resulting in you spending a lot less time consulting this type of a reference. I'll be honest. I find myself still occasionally checking the specification for properties if it's a property that I don't use that often, but for the most part, I'm pretty comfortable with the available properties and their values, and over time you will be too.
Now for the moment, I want to keep the focus on properties by exploring the units of measurements that are available to us in CSS, and we're going to go ahead and tackle that in our next movie.
There are currently no FAQs about CSS: Core Concepts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.