HTML Academy
Previous task
Introduction to CSS custom properties1/13
Back to the list of tasks
  • 1. Declaring custom properties
  • 2. Reusing values with var()
  • 3. Changing a custom property’s value
  • 4. Case-sensitive property names
  • 5. Naming custom properties
  • 6. Storing sizes in custom properties
  • 7. Sharing spacing values
  • 8. Sharing values with :root
  • 9. Referencing other custom properties
  • 10. Custom properties for a component
  • 11. Shared and local values
  • 12. Chains of custom properties
  • 13. Challenge: Custom properties
Reusing values with var()
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Declaring custom properties

The page shows a card for Karpovka Embankment. Its HTML and basic styles are already in place. In this chapter, we'll customize the card using custom properties, also known as CSS variables.

A custom property stores a value under a name we choose. The name starts with two hyphens, --. We declare it inside a CSS rule's curly braces, just like a regular declaration:

.banner {
  --text-color: #663399;
}

Declaring a custom property doesn't change any styles on its own. To use the stored value, we write var() with the property's full name, including the two hyphens:

.banner {
  --text-color: #663399;
  color: var(--text-color);
}

The heading is inside the card. Custom properties are inherited by descendant elements, so the heading can use a value declared on .card.

Let's start with the card heading's color. Replacing the color value with var() will keep the heading the same color.

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Introduction to custom properties</title> <link rel="stylesheet" href="card.css"> <link rel="stylesheet" href="style.css"> </head> <body> <article class="card"> <img class="card__image" src="karpovka-400.jpg" width="200" height="100" alt="Karpovka Embankment"> <div class="card__content"> <h2 class="card__title">Karpovka Embankment</h2> <p class="card__description"> A scenic place for a walk in Saint Petersburg. </p> <a class="card__button" href="#">Learn more</a> </div> </article> </body> </html>
CSS
.card { } .card__title { color: #3370ce; } .card__button { background-color: #3370ce; }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Goalscompleted
0
    1. In the .card rule, declare a custom property named --color with the value #3370ce.
    2. In the .card__title rule, replace the value of color with var(--color).

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.