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

Exploring CSS syntax

From: HTML Essential Training

Video: Exploring CSS syntax

The language of CSS is really pretty simple. Let's make a working copy of css.html, and I'm going to rename this to css-working.html, and we open that in the text editor. And let's scroll through it real slowly for those of you who are typing along at home. Of course, these paragraphs all just contain a bunch of Lorem ipsum, so you won't need to type that exactly. Just any text whatsoever will work fine. And we'll go ahead and we'll open this in the browser as well, and we see this has my reset stylesheet applied to it, so the text is really close together, but it is styled.

Exploring CSS syntax

The language of CSS is really pretty simple. Let's make a working copy of css.html, and I'm going to rename this to css-working.html, and we open that in the text editor. And let's scroll through it real slowly for those of you who are typing along at home. Of course, these paragraphs all just contain a bunch of Lorem ipsum, so you won't need to type that exactly. Just any text whatsoever will work fine. And we'll go ahead and we'll open this in the browser as well, and we see this has my reset stylesheet applied to it, so the text is really close together, but it is styled.

And here's the stylesheet declaration and you'll notice that the stylesheet itself, go ahead and open that up so you can see it, is just this very simple reset stylesheet. But for now, we're going to go ahead and type in a style element here so that we can just type some CSS into this document and look at the syntax of CSS. A CSS rule consists of a selector followed by a block of declarations. So let's create a selector here and a block of declarations.

So these curly braces here create a block and there is three declarations inside this block; there is font-family, font-size and line-height, and there is a selector, and the selector in this case is a element selector and it's called p. So this will select all of the

elements in this document, and so these are these paragraphs down here. There are three of them. And if we save this and look in the browser, we'll hit Reload. You see that these paragraphs now change.

They have this font and this size and this line spacing just as we declared it in our p selector. So this whole thing is called a rule and this is the selector. It is the letter p there and that selects all the elements that are called p. Instead, I can use a class selector. I can say .foo like this, and it will only now select elements that have a class called foo. So if I reload this in the browser you'll see that's none of these elements, and I can come down here and I can, say take this middle one, and I can say, class="foo" and save that and reload it in the browser, and now that middle paragraph there gets that style applied to it, because it's now class foo.

In fact, I can do the same thing with this h1 element and save that and reload it, and now also gets the class foo. You notice it's still bold because I didn't change the boldness within this. There's no declaration for font-weight, but it got the font-size and the line spacing. Now if I only want this to apply to

elements that are of class foo, I can put a p in front of it and say, p.foo. Now when I reload that, header is back to normal, but the paragraph is still styled.

It's also something called an ID selector, so let's create an ID selector. So the pound sign or the number sign, Shift+3, that little Hash mark there, that is the ID selector, and here I've just created an ID called p1. So now nothing applies anymore. And if I come down here and I say id="p1", save that and reload it, now that paragraph which has this id of P1 is now styled.

So the rule about ID selectors is that there can only be one element in a document with a particular ID selector. Another way to say this is that an ID selector can only be used once on one element. So if I were to come up here to this h1 and I were to say, id="p1", that would be illegal, so I'm not going to do that. On the other hand if I take the ID selector out of this p, and save that, you'll see it still doesn't work because we've said that this ID selector is on a

element.

So if I reload this, that's still not working. On the other hand, if I take out the p from the selector now we're selecting anything with this id p1, now it applies that h1 element. Oftentimes ID selectors are done this way. I tend to like to also use the element name. It's not necessary. It's not required. It is allowed. It just helps me to know when I'm looking at the style sheet what it is that I'm looking at. I'm looking at a

element. I'm looking at a h1 element or whatever.

There is also something called Descendent Selectors. I tend to use these now and then. If I were to select like this, I'm going to take out this id down here, I'm going to go ahead and I'm going to put an element inside of this paragraph and save that. Now I'm just selecting elements that are descendent from

elements. So if I reload this, you'll see that everything else is still in the old font except for this element. This link is now in the sans-serif font and then that size and all of that.

On the other hand if I put an element inside of this h1 element and save that, that does not get applied. Of course, it still gets the a. It's still becomes a link. You see it's still formatted like this header. That's because this descendent selector, this only applies to elements that are descendent from

elements. So it's inside of a

element someplace. There is another type of a descendent selector that has this right angle bracket. That's actually the child selector.

It only works for elements that are direct descendents, so it will still work for this element because it's a direct descendent. On the other hand, if I were to put this element inside of something else, like just a span, now it will no longer apply, because it's not a direct descendent anymore. It's not a child. But if I take that little right angle bracket out, now it will apply because it is a descendent. It's just not a direct child. There's also something called pseudo selectors. I know there are a lot of selectors that play here, but we're going to take a minute and look at them all, and this is the last one we're going to look at.

There is actually a lot more, but these are the primary ones, something called a pseudo selector. And if I use a colon and the word hover here, that's a pseudo selector that applies to elements and I'll save that and load it up in the browser. Now you see that that link is in the same font and everything as the rest of the paragraph, but when I hover my cursor over it, you see it changes and that's because this pseudo selector is for when the cursor hovers over the element.

And so you'll see that used quite a bit. So those are the major types of selectors. Let's take a look now at the declarations. And I'm just going to make this for all the paragraphs again. I'm going to come in here and take out some of this extra stuff that we've added. Save that and reload, and let's look at the declarations. Remember that this is a declaration, and declaration has several parts. It has the property name, in this case font-family is the property name, and it has the value, in this case sans-serif is the value.

The property name and the value are separated by a colon and terminated with a semicolon. Now in current versions of CSS, semicolons are separators which means that technically this last one is not required. And if I delete that and reload the page you see everything is still working just fine. On the other hand, if I delete one of these other semicolons and reload, now it's not working anymore. So technically as of today, these semicolons are separators. Unfortunately current versions of the CSS3 specification as of the date that I'm recording this show a change where semicolons would become terminators.

So this would no longer be legal. It would now be required to have this semicolon at the end of the last declaration. Personally, I think it should have been this way in the first place, but unfortunately it's been the other way for so long that there is a lot of code out there that does things like this, where one- liners of CSS have no semicolon. I don't know why. I tried always putting the semicolon anyway, because otherwise I might say, well you know, I want to add a few more things to this, and so I'm going to add a font-family and blah, blah, blah, and all of a sudden I'm actually missing a semicolon where it's required.

So I really always try to put in the semicolon because it's just a good idea and it really doesn't take that much effort, although sometimes I forget. And when I forget it still works, and therein lies the problem. There's a tremendous amount of code out there that doesn't use the semicolon. So I'm actually hoping that the people who are making the specification change their mind and make it a separator again, but in case they don't, you need to know that that semicolon may in the future become required. It may also become required in places like this, where I will have to put in the semicolon even in a style attribute in the HTML.

So it's just worth knowing and it's worth getting into that habit of putting the semicolons at the end of every declaration. It's allowed as it is today and it may become required in the future. Just like in HTML white space is ignored in CSS, so this, and I'm just going to delete this other one because we're not actually using it. So this is actually perfectly legal CSS. I can even take out these spaces here. I can make this entire thing with no spaces.

There are some places where spaces are required like in descendent selectors and things like that, but for the most part space is completely ignored in CSS, and you'll see that this works exactly. We now have a green header because I put the color green there in that style. Comments in CSS look like this: they are just like in the C language or in C-derived languages, a /* to start the comment and an */ to end the comment. There are no line-oriented comments in CSS.

If you do that it's not a comment. It's actually ID selector; if you do that, it's not a comment, it's just a syntax error. This makes it a comment, and so if we load this up, now we'll see that CSS is not being applied anymore. So the language of CSS is really pretty simple. It's easy to learn and it's actually pretty easy to use as well.

Show transcript

This video is part of

Image for HTML Essential Training
HTML Essential Training

82 video lessons · 101740 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 24s
    1. Welcome
      56s
    2. Using the exercise files
      1m 37s
    3. What you need to know about this course
      2m 51s
  2. 22m 0s
    1. What is HTML?
      4m 12s
    2. Examining the structure of an HTML document
      7m 50s
    3. Understanding tags and containers
      6m 4s
    4. Exploring content models in HTML5
      2m 23s
    5. Looking at obsolete elements
      1m 31s
  3. 27m 19s
    1. Understanding whitespace and comments
      3m 53s
    2. Displaying text with paragraphs
      3m 37s
    3. Applying style
      8m 5s
    4. Using block and inline tags
      6m 34s
    5. Displaying characters with references
      5m 10s
  4. 16m 36s
    1. Exploring the front matter of HTML
      2m 9s
    2. Applying CSS to your document
      3m 59s
    3. Adding scripting elements
      4m 54s
    4. Using the meta tag
      3m 34s
    5. Optimizing your page for search engines
      2m 0s
  5. 24m 59s
    1. Controlling line breaks and spaces
      2m 46s
    2. Exploring phrase elements
      1m 44s
    3. Using font markup elements
      1m 5s
    4. Highlighting text with mark
      1m 29s
    5. Adding headings
      1m 38s
    6. Using quotations and quote marks
      3m 2s
    7. Exploring preformatted text
      1m 45s
    8. Formatting lists
      2m 28s
    9. Forcing text direction
      3m 49s
    10. Suggesting word-break opportunities
      2m 29s
    11. Annotating East Asian languages
      2m 44s
  6. 29m 15s
    1. Introducing CSS
      55s
    2. Understanding CSS placement
      6m 55s
    3. Exploring CSS syntax
      10m 34s
    4. Understanding CSS units of measure
      3m 3s
    5. Some CSS examples
      7m 48s
  7. 22m 5s
    1. Using images
      4m 13s
    2. Flowing text around an image
      4m 55s
    3. Breaking lines around an image
      3m 3s
    4. Aligning images
      5m 25s
    5. Mapping links in an image
      4m 29s
  8. 22m 28s
    1. Understanding URLs
      2m 41s
    2. Working with hyperlinks
      3m 28s
    3. Using relative URLs
      4m 20s
    4. Specifying a base URL
      2m 19s
    5. Linking within a page
      4m 12s
    6. Using image links
      5m 28s
  9. 17m 2s
    1. Exploring list types
      3m 52s
    2. List elements in depth
      7m 44s
    3. Using text menus with unordered lists
      5m 26s
  10. 15m 30s
    1. Introduction to HTML semantics
      4m 9s
    2. Exploring an example
      4m 56s
    3. Marking up figures and illustrations
      2m 33s
    4. Creating collapsible details
      3m 52s
  11. 11m 18s
    1. Embedding audio
      5m 19s
    2. Embedding video
      5m 59s
  12. 11m 53s
    1. Creating ad-hoc Document Object Model (DOM) data with the data-* attribute
      4m 53s
    2. Displaying relative values with meter
      2m 57s
    3. Creating dynamic progress indicators
      4m 3s
  13. 4m 49s
    1. Overview of HTML5 microdata
      1m 8s
    2. Exploring an example with microdata
      3m 41s
  14. 7m 3s
    1. Understanding outlines
      52s
    2. A demonstration of outlining
      6m 11s
  15. 13m 1s
    1. Table basics
      7m 29s
    2. Exploring the semantic parts of a table
      2m 32s
    3. Grouping columns
      3m 0s
  16. 9m 55s
    1. Frames overview
      54s
    2. Using traditional frames
      4m 26s
    3. Exploring inline frames using iframe
      2m 7s
    4. Simulating frames with CSS
      2m 28s
  17. 53m 7s
    1. Introducing forms
      10m 24s
    2. Using text elements
      10m 12s
    3. Using checkboxes and radio buttons
      2m 37s
    4. Creating selection lists and dropdown lists
      5m 14s
    5. Submit and button elements
      8m 48s
    6. Using an image as a submit button
      2m 15s
    7. Keeping context with the hidden element
      3m 0s
    8. Setting tab order
      2m 7s
    9. Preloading an autocomplete list using the datalist feature
      5m 26s
    10. Displaying results with output
      3m 4s
  18. 19m 47s
    1. Touring a complete site
      2m 14s
    2. Touring the HTML
      8m 44s
    3. Touring the CSS
      8m 49s
  19. 29s
    1. Goodbye
      29s

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.

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 HTML Essential Training.

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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.