Previous task
Introduction to HTML and CSS10/13
Back to the list of tasks
  1. 1. Let's Go!
  2. 2. CSS in action
  3. 3. Consolidation
  4. 4. HTML basics
  5. 5. Single HTML tags
  6. 6. Attributes of HTML tags
  7. 7. Looking for errors in HTML
  8. 8. Basics of CSS
  9. 9. Other ways to connect CSS
  10. 10. CSS selectors
  11. 11. CSS classes
  12. 12. CSS properties and values
  13. 13. Looking for errors in CSS
Nest task
  • Courses
  • Sign up
  • Log in

Loading...
It will be ready in a few seconds.

  • Theory
  • Theory

CSS selectors

Congratulations! You have just plugged external CSS styles into your HTML page.

When you specify tag styles using the style attribute, the browser immediately knows which tag to apply those styles to. But when styles are attached using an external file or via the <style> tag, the browser searches for the tags to be styled using "selectors".

You're already a little familiar with selectors: in the previous assignment, you used the p selector, which was in front of the curly brackets in the CSS code. The syntax of CSS rules looks like this:

selector {
  property1: value1;
  property2: value2;
  …
}

The language of selectors is very powerful and flexible. The simplest selectors are selectors by tag name: p, h1, and so on. The browser applies styles from a rule with this selector to all matching tags. For example, to all paragraphs or to all first-level headings.

In our outline, selector p has highlighted all theparagraphs in green. How to make them different colors? You can add other tags inside the paragraphs. Then you can use selectors for different tags in CSS and give them different colors.

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html> <head> <title>CSS selectors</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Course outline</h1> <p><strong>Paired tags.</strong></p> <p><strong>Single tags.</strong></p> <p><strong>Tag attributes.</strong></p> <p><strong>Inline styles</strong></p> <p><em>External Styles.</em></p> <p><em>Styling by class.</em></p> </body> </html>
CSS
body { font-family: "Tahoma", serif; } /* A little help. h1 { color: #999999; } */

The code has changed, click "Update" or enable autorun.

You have moved to another page

Click inside the mini-browser to highlight this window.

100%
GoalsCompleted
0

    Practice with simple selectors:

    1. For h1 set color: #99999999;.
    2. For strong set color: green;.
    3. For em set color: red;.

    © 2023-2024, codehero.pro