HTML Academy
Referencing other custom properties
Introduction to CSS custom properties10/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
Shared and local values
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Custom properties for a component

The heading and button currently share one setting, --card-color. Let's give each part of the card its own property: --card-title-color and --card-button-color.

For now, both settings will reference the palette's primary color. Later, we'll be able to change just the button's color while keeping the heading connected to the shared palette.

When we replace the declarations, we also need to update their references. Until those references are updated, the heading and button text may be barely visible against the card. Complete all three steps to restore the color to both elements.

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
:root { --page-color: #203044; --color-primary: #14a76c; } body { background-color: var(--page-color); } .card { --card-color: var(--color-primary); --card-radius: 12px; border-radius: var(--card-radius); --card-spacing: 20px; padding: var(--card-spacing); } .card__title { color: var(--card-color); } .card__button { background-color: var(--card-color); } .card__image { border-radius: var(--card-radius); margin-bottom: var(--card-spacing); } .card__description { margin-bottom: var(--card-spacing); }

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, replace the --card-color declaration with two declarations: --card-title-color: var(--color-primary) and --card-button-color: var(--color-primary).
    2. In the .card__title rule, set color: var(--card-title-color).
    3. In the .card__button rule, set background-color: var(--card-button-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.