Search code examples
csscss-variables

Can we inherit a custom variable defined in a class in another class?


I've been going through the basic CSS tutorials and I had a small confusion. I would appreciate any help with this. Below are two examples to demonstrate what is causing the confusion

Scenario 1: Inheriting a variable from :root

:root {
  --red-color: red;
}
.red-box {
  background: red;
  background: var(--red-color);
  height: 200px;
  width:200px;
}
<div class="red-box"></div>

Scenario 2: Inheriting a variable from another class. [I HAVE CONFUSION WITH THIS CASE]

.penguin {

  /* change code below */
  --penguin-skin: gray;
  --penguin-belly: white;
  --penguin-beak: orange;
  /* change code above */

  position: relative;
  margin: auto;
  display: block;
  margin-top: 5%;
  width: 300px;
  height: 300px;
}

.penguin-top {
  top: 10%;
  left: 25%;
  background: var(--penguin-skin, gray);
  width: 50%;
  height: 45%;
  border-radius: 70% 70% 60% 60%;
}

.penguin-bottom {
  top: 40%;
  left: 23.5%;
  background: var(--penguin-skin, gray);
  width: 53%;
  height: 45%;
  border-radius: 70% 70% 100% 100%;
}

.right-hand {
  top: 0%;
  left: -5%;
  background: var(--penguin-skin, gray);
  width: 30%;
  height: 60%;
  border-radius: 30% 30% 120% 30%;
  transform: rotate(45deg);
  z-index: -1;
}

.left-hand {
  top: 0%;
  left: 75%;
  background: var(--penguin-skin, gray);
  width: 30%;
  height: 60%;
  border-radius: 30% 30% 30% 120%;
  transform: rotate(-45deg);
  z-index: -1;
}

.right-cheek {
  top: 15%;
  left: 35%;
  background: var(--penguin-belly, white);
  width: 60%;
  height: 70%;
  border-radius: 70% 70% 60% 60%;
}

.left-cheek {
  top: 15%;
  left: 5%;
  background: var(--penguin-belly, white);
  width: 60%;
  height: 70%;
  border-radius: 70% 70% 60% 60%;
}

.belly {
  top: 60%;
  left: 2.5%;
  background: var(--penguin-belly, white);
  width: 95%;
  height: 100%;
  border-radius: 120% 120% 100% 100%;
}

.right-feet {
  top: 85%;
  left: 60%;
  background: var(--penguin-beak, orange);
  width: 15%;
  height: 30%;
  border-radius: 50% 50% 50% 50%;
  transform: rotate(-80deg);
  z-index: -2222;
}

.left-feet {
  top: 85%;
  left: 25%;
  background: var(--penguin-beak, orange);
  width: 15%;
  height: 30%;
  border-radius: 50% 50% 50% 50%;
  transform: rotate(80deg);
  z-index: -2222;
}

.right-eye {
  top: 45%;
  left: 60%;
  background: black;
  width: 15%;
  height: 17%;
  border-radius: 50%;
}

.left-eye {
  top: 45%;
  left: 25%;
  background: black;
  width: 15%;
  height: 17%;
  border-radius: 50%;
}

.sparkle {
  top: 25%;
  left: 15%;
  background: white;
  width: 35%;
  height: 35%;
  border-radius: 50%;
}

.blush-right {
  top: 65%;
  left: 15%;
  background: pink;
  width: 15%;
  height: 10%;
  border-radius: 50%;
}

.blush-left {
  top: 65%;
  left: 70%;
  background: pink;
  width: 15%;
  height: 10%;
  border-radius: 50%;
}

.beak-top {
  top: 60%;
  left: 40%;
  background: var(--penguin-beak, orange);
  width: 20%;
  height: 10%;
  border-radius: 50%;
}

.beak-bottom {
  top: 65%;
  left: 42%;
  background: var(--penguin-beak, orange);
  width: 16%;
  height: 10%;
  border-radius: 50%;
}

body {
  background:#c6faf1;
}

.penguin * {
  position: absolute;
}
<div class="penguin">
      <div class="penguin-bottom">
        <div class="right-hand"></div>
        <div class="left-hand"></div>
        <div class="right-feet"></div>
        <div class="left-feet"></div>
      </div>
      <div class="penguin-top">
        <div class="right-cheek"></div>
        <div class="left-cheek"></div>
        <div class="belly"></div>
        <div class="right-eye">
          <div class="sparkle"></div>
        </div>
        <div class="left-eye">
          <div class="sparkle"></div>

My Question:

I have clearly seen the tutorials mentioning that we can make use of variables from :root in other classes, but as per the example in Scenario 2 I am not able to understand how is the variable inherited from one class to another? I am asking this since I have not seen this mentioned specifically and I just want to be sure about my knowledge.

Thanks for your help in advance :)


Solution

  • From the specification:

    Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc.

    And also:

    Inherited: yes

    So it's not about the inheritance from another class but the usual inheritance from a parent (or ancestor) element exactly like any inherited property.

    In your case penguin-top is a child of penguin so it will inherit its custom properties.


    Worth to note that :root is the html element which is the ancestor of all the elements that's why all them will logically inherit custom property defined inside :root