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

Styles in the Document

Inline styles work, but imagine you want all the paragraphs on a page to be green. You would have to add the same style attribute to every paragraph, and if you changed your mind, you would have to change each of them again.

That’s why CSS has rules. A rule says which elements to style and how. It starts with a selector, which selects the elements, followed by declarations in curly braces:

li {
	color: orange;
	text-align: center;
}

The selector li selects all li elements on the page, so this rule makes all list items orange and centered. The declarations are the same as in the style attribute. It is customary to write each declaration on its own line, indented, and to end each one with a semicolon, even the last one.

The second way to add CSS to a page is to write rules in the style element. It goes into head, because it isn’t part of the content of the page, but information about how to display it:

<head>
	<meta charset="utf-8">
	<title>Domestic Cat</title>
	<style>
		li {
			color: orange;
		}
	</style>
</head>

Instructions

  1. 1

    In head, below the title element, add an empty style element: <style></style>.

  2. 2

    In the style element, create a rule for p with the declaration color: green. Both paragraphs will turn green at once.

Back

4/6

  1. 1. Introduction to CSS
  2. 2. Inline Styles
  3. 3. Quiz: Inline Styles
  4. 4. Styles in the Document
  5. 5. External Stylesheets
  6. 6. Summary
Next

Try it yourself

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