Previous task
Introduction to HTML and CSS11/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 classes

Well, we colored the text again, but at what cost. The solution with additional tags for styling is not the best. It is much more convenient to use "classes"!

A class is just one of the attributes of HTML tags, for example:

<p class="important">…</p>
<p class="help">…</p>

In CSS, you can set styles for elements with a specific class. To do this, a class selector is used, which is written as .class-name, for example:

.important { color: red; } — selects all tags with class "important"
.help { color: green; }    — selects all tags with class "help"

To give paragraphs different colors, you can add different classes to them and then use class selectors.

The class name can contain Latin characters, numbers, hyphen - and underscore _ and it must begin with a Latin character.
  • index.html
  • style.css
HTML
<!DOCTYPE html> <html> <head> <title>CSS classes</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Course outline</h1> <p>Paired tags.</p> <p>Single tags.</p> <p>Tag attributes.</p> <p>Inline styles.</p> <p>External Styles.</p> <p>Styling by class.</p> </body> </html>
CSS
body { font-family: "Tahoma", serif; } h1 { color: #999999; } .completed { color: green; } .in-progress { color: orange; } .new { color: red; }

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

    Let's add design using classes:

    1. Add a completed class to the first four paragraphs.
    2. To the fifth paragraph, add the class in-progress.
    3. Add a new class to the sixth paragraph.

    © 2023-2024, codehero.pro