Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Rules and Comments

Welcome to the second CSS lesson! In the previous lesson, you wrote your first rule. Let’s look at its parts and their names more closely, because we’ll use them all the time:

The rule h1 { color: blue; } with its parts labeled: h1 is the selector, color: blue; is a declaration, color is its property and blue its value. The whole is a rule.
  • A rule is the whole thing: a selector and declarations in curly braces.
  • The selector selects the elements that the rule styles. The selector h1 selects all h1 elements.
  • A declaration says what to change and how. It consists of a property and a value, separated by a colon, and it ends with a semicolon.
  • The property is what we change, for example the color of the text (color).
  • The value is what we change the property to, for example blue.

A stylesheet is simply a list of rules. Spaces and line breaks don’t matter to the browser, so h1{color:blue} would work too. But rules written on several lines, with the declarations indented, are much easier to read.

A stylesheet can also contain comments: notes for people that the browser ignores. A comment starts with /* and ends with */, and it can span several lines:

/* Main heading */
h1 {
	color: blue;
}

/*
p {
	color: green;
}
*/

Comments are used to explain the code, and also to turn a part of it off for a while: the rule for p above is inside a comment, so it does nothing. Watch out: the HTML comment <!-- --> doesn’t work in CSS. Here, only /* */ works.

In style.css, we’ve left the rule from the previous lesson, and we’ve removed the inline style from the main heading so that it doesn’t get in the way.

Instructions

  1. 1

    In the rule, change the selector from p to h1. The rule will color the main heading instead of the paragraphs.

  2. 2

    Change the value of the color property from green to sienna, a reddish brown.

  3. 3

    Above the rule, write a comment that says what the rule is for, for example /* Main heading */.

Back

1/17

  1. 1. Rules and Comments
  2. 2. Quiz: Rules
  3. 3. Universal Selector
  4. 4. Type Selector
  5. 5. Class Selector
  6. 6. ID Selector
  7. 7. Quiz: Class or ID?
  8. 8. Attribute Selector
  9. 9. Pseudo-classes
  10. 10. Grouping Selectors
  11. 11. Chaining Selectors
  12. 12. Selecting Descendants
  13. 13. Selecting Children
  14. 14. The Cascade
  15. 15. Specificity
  16. 16. Inheritance
  17. 17. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.