Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Setting properties

From: CSS: Core Concepts

Video: Setting properties

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.

Setting properties

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.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 43619 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 34s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 18s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 49s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Core Concepts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.