How to store inherit value inside a CSS custom property (aka CSS variables)?









up vote
2
down vote

favorite
2












Let's consider this simplified example1 in order to illustrate the issue:






:root 
--color:rgba(20,20,20,0.5); /*defined as the default value*/


.box
width:50px;
height:50px;
display:inline-block;
margin-right:30px;
border-radius:50%;
position:relative;

.red background:rgba(255,0,0,0.5);
.blue background:rgba(0,255,0,0.5);

.box:before
content:"";
position:absolute;
top:0;left:0;right:0;bottom:0;
border-radius:50%;
transform:translateX(30px);
background:var(--color);
filter:invert(1);

<!-- we can add any color we want -->
<div class="box red" style="--color:rgba(0,255,0,0.5);">
</div>
<div class="box blue" style="--color:rgba(0,255,255,0.5);">
</div>

<!-- we can add the same color but this won't be dynamic -->
<div class="box red" style="--color:rgba(255,0,0,0.5);">
</div>

<!-- it would be good to be able to inherit the value but this won't work -->
<div class="box red" style="--color:inherit;">
</div>





In the above code we are able to manipulate the background of the pseudo element using CSS variable. In some cases, we need to have the same color as the main element but since we don't know what color is used, we cannot set it manually and the best way should be to use the inherit value.



As explained here: Css display property set to inherit with variable doesn't work, the use of inherit won't work.



Is there any way to be able to store the inherit value within a CSS variable and use it later within any property (background in our example)?










share|improve this question



























    up vote
    2
    down vote

    favorite
    2












    Let's consider this simplified example1 in order to illustrate the issue:






    :root 
    --color:rgba(20,20,20,0.5); /*defined as the default value*/


    .box
    width:50px;
    height:50px;
    display:inline-block;
    margin-right:30px;
    border-radius:50%;
    position:relative;

    .red background:rgba(255,0,0,0.5);
    .blue background:rgba(0,255,0,0.5);

    .box:before
    content:"";
    position:absolute;
    top:0;left:0;right:0;bottom:0;
    border-radius:50%;
    transform:translateX(30px);
    background:var(--color);
    filter:invert(1);

    <!-- we can add any color we want -->
    <div class="box red" style="--color:rgba(0,255,0,0.5);">
    </div>
    <div class="box blue" style="--color:rgba(0,255,255,0.5);">
    </div>

    <!-- we can add the same color but this won't be dynamic -->
    <div class="box red" style="--color:rgba(255,0,0,0.5);">
    </div>

    <!-- it would be good to be able to inherit the value but this won't work -->
    <div class="box red" style="--color:inherit;">
    </div>





    In the above code we are able to manipulate the background of the pseudo element using CSS variable. In some cases, we need to have the same color as the main element but since we don't know what color is used, we cannot set it manually and the best way should be to use the inherit value.



    As explained here: Css display property set to inherit with variable doesn't work, the use of inherit won't work.



    Is there any way to be able to store the inherit value within a CSS variable and use it later within any property (background in our example)?










    share|improve this question

























      up vote
      2
      down vote

      favorite
      2









      up vote
      2
      down vote

      favorite
      2






      2





      Let's consider this simplified example1 in order to illustrate the issue:






      :root 
      --color:rgba(20,20,20,0.5); /*defined as the default value*/


      .box
      width:50px;
      height:50px;
      display:inline-block;
      margin-right:30px;
      border-radius:50%;
      position:relative;

      .red background:rgba(255,0,0,0.5);
      .blue background:rgba(0,255,0,0.5);

      .box:before
      content:"";
      position:absolute;
      top:0;left:0;right:0;bottom:0;
      border-radius:50%;
      transform:translateX(30px);
      background:var(--color);
      filter:invert(1);

      <!-- we can add any color we want -->
      <div class="box red" style="--color:rgba(0,255,0,0.5);">
      </div>
      <div class="box blue" style="--color:rgba(0,255,255,0.5);">
      </div>

      <!-- we can add the same color but this won't be dynamic -->
      <div class="box red" style="--color:rgba(255,0,0,0.5);">
      </div>

      <!-- it would be good to be able to inherit the value but this won't work -->
      <div class="box red" style="--color:inherit;">
      </div>





      In the above code we are able to manipulate the background of the pseudo element using CSS variable. In some cases, we need to have the same color as the main element but since we don't know what color is used, we cannot set it manually and the best way should be to use the inherit value.



      As explained here: Css display property set to inherit with variable doesn't work, the use of inherit won't work.



      Is there any way to be able to store the inherit value within a CSS variable and use it later within any property (background in our example)?










      share|improve this question















      Let's consider this simplified example1 in order to illustrate the issue:






      :root 
      --color:rgba(20,20,20,0.5); /*defined as the default value*/


      .box
      width:50px;
      height:50px;
      display:inline-block;
      margin-right:30px;
      border-radius:50%;
      position:relative;

      .red background:rgba(255,0,0,0.5);
      .blue background:rgba(0,255,0,0.5);

      .box:before
      content:"";
      position:absolute;
      top:0;left:0;right:0;bottom:0;
      border-radius:50%;
      transform:translateX(30px);
      background:var(--color);
      filter:invert(1);

      <!-- we can add any color we want -->
      <div class="box red" style="--color:rgba(0,255,0,0.5);">
      </div>
      <div class="box blue" style="--color:rgba(0,255,255,0.5);">
      </div>

      <!-- we can add the same color but this won't be dynamic -->
      <div class="box red" style="--color:rgba(255,0,0,0.5);">
      </div>

      <!-- it would be good to be able to inherit the value but this won't work -->
      <div class="box red" style="--color:inherit;">
      </div>





      In the above code we are able to manipulate the background of the pseudo element using CSS variable. In some cases, we need to have the same color as the main element but since we don't know what color is used, we cannot set it manually and the best way should be to use the inherit value.



      As explained here: Css display property set to inherit with variable doesn't work, the use of inherit won't work.



      Is there any way to be able to store the inherit value within a CSS variable and use it later within any property (background in our example)?






      :root 
      --color:rgba(20,20,20,0.5); /*defined as the default value*/


      .box
      width:50px;
      height:50px;
      display:inline-block;
      margin-right:30px;
      border-radius:50%;
      position:relative;

      .red background:rgba(255,0,0,0.5);
      .blue background:rgba(0,255,0,0.5);

      .box:before
      content:"";
      position:absolute;
      top:0;left:0;right:0;bottom:0;
      border-radius:50%;
      transform:translateX(30px);
      background:var(--color);
      filter:invert(1);

      <!-- we can add any color we want -->
      <div class="box red" style="--color:rgba(0,255,0,0.5);">
      </div>
      <div class="box blue" style="--color:rgba(0,255,255,0.5);">
      </div>

      <!-- we can add the same color but this won't be dynamic -->
      <div class="box red" style="--color:rgba(255,0,0,0.5);">
      </div>

      <!-- it would be good to be able to inherit the value but this won't work -->
      <div class="box red" style="--color:inherit;">
      </div>





      :root 
      --color:rgba(20,20,20,0.5); /*defined as the default value*/


      .box
      width:50px;
      height:50px;
      display:inline-block;
      margin-right:30px;
      border-radius:50%;
      position:relative;

      .red background:rgba(255,0,0,0.5);
      .blue background:rgba(0,255,0,0.5);

      .box:before
      content:"";
      position:absolute;
      top:0;left:0;right:0;bottom:0;
      border-radius:50%;
      transform:translateX(30px);
      background:var(--color);
      filter:invert(1);

      <!-- we can add any color we want -->
      <div class="box red" style="--color:rgba(0,255,0,0.5);">
      </div>
      <div class="box blue" style="--color:rgba(0,255,255,0.5);">
      </div>

      <!-- we can add the same color but this won't be dynamic -->
      <div class="box red" style="--color:rgba(255,0,0,0.5);">
      </div>

      <!-- it would be good to be able to inherit the value but this won't work -->
      <div class="box red" style="--color:inherit;">
      </div>






      css css3 css-variables






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 at 10:15

























      asked Nov 10 at 14:22









      Temani Afif

      62.2k93573




      62.2k93573






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:



          background:var(--color,inherit)


          By doing this, we tell our property (background) to use inherit in case --color is not defined.



          This may solve the issue but in our case it won't be enough since --color is always defined at :root level and will get inherited1 by the pseudo element thus we will never use the fallback value.



          To fix this we can consider the initial value in order to undefine our custom property and force the use of the fallback value. As described in the specification:




          The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().




          and




          To substitute a var() in a property’s value:



          1. If the custom property named by the first argument to the var()
            function is animation-tainted, and the var() function is being used in
            the animation property or one of its longhands, treat the custom
            property as having its initial value for the rest of this algorithm.

          2. If the value of the custom property named by the first argument to the
            var() function is anything but the initial value, replace the var()
            function by the value of the corresponding custom property. Otherwise,

          3. if the var() function has a fallback value as its second argument,
            replace the var() function by the fallback value. If there are any
            var() references in the fallback, substitute them as well.

          4. Otherwise, the property containing the var() function is invalid at
            computed-value time



          Our code will then look like this:






          :root 
          --color:rgba(20,20,20,0.5); /*defined as the default value*/


          .box
          width:50px;
          height:50px;
          display:inline-block;
          margin-right:30px;
          border-radius:50%;
          position:relative;

          .red background:rgba(255,0,0,0.5);
          .blue background:rgba(0,255,0,0.5);

          .box:before
          content:"";
          position:absolute;
          top:0;left:0;right:0;bottom:0;
          border-radius:50%;
          transform:translateX(30px);
          background:var(--color,inherit);
          filter:invert(1);

          <div class="box red" style="--color:initial;">
          </div>
          <div class="box blue" style="--color:initial;">
          </div>

          <div class="box" style="background:grey;--color:initial;">
          </div>





          We simply need to set initial to the custom property in order to force the inherit to be used as a value within background.




          The usage of initial can also be useful in order to stop the propagation of CSS variable at a particular level since by default it's inherited by all the elements.



          Here is an example:






          :root 
          --color: blue;

          .container div
          border:5px solid var(--color,red);
          padding:5px;

          .stop
          --color:initial;

          .green
          --color:green;

          <div class="container">
          <div>
          <div>
          <div class="stop"> <!-- we stop at this level -->
          <div>
          </div>
          </div>
          </div>
          </div>
          </div>

          <div class="container stop"> <!-- we stop at this level -->
          <div>
          <div>
          <div class="green"> <!-- we redefine at this level -->
          <div>
          </div>
          </div>
          </div>
          </div>
          </div>







          1: it's about the inheritance of the custom property and not the background property






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239880%2fhow-to-store-inherit-value-inside-a-css-custom-property-aka-css-variables%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:



            background:var(--color,inherit)


            By doing this, we tell our property (background) to use inherit in case --color is not defined.



            This may solve the issue but in our case it won't be enough since --color is always defined at :root level and will get inherited1 by the pseudo element thus we will never use the fallback value.



            To fix this we can consider the initial value in order to undefine our custom property and force the use of the fallback value. As described in the specification:




            The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().




            and




            To substitute a var() in a property’s value:



            1. If the custom property named by the first argument to the var()
              function is animation-tainted, and the var() function is being used in
              the animation property or one of its longhands, treat the custom
              property as having its initial value for the rest of this algorithm.

            2. If the value of the custom property named by the first argument to the
              var() function is anything but the initial value, replace the var()
              function by the value of the corresponding custom property. Otherwise,

            3. if the var() function has a fallback value as its second argument,
              replace the var() function by the fallback value. If there are any
              var() references in the fallback, substitute them as well.

            4. Otherwise, the property containing the var() function is invalid at
              computed-value time



            Our code will then look like this:






            :root 
            --color:rgba(20,20,20,0.5); /*defined as the default value*/


            .box
            width:50px;
            height:50px;
            display:inline-block;
            margin-right:30px;
            border-radius:50%;
            position:relative;

            .red background:rgba(255,0,0,0.5);
            .blue background:rgba(0,255,0,0.5);

            .box:before
            content:"";
            position:absolute;
            top:0;left:0;right:0;bottom:0;
            border-radius:50%;
            transform:translateX(30px);
            background:var(--color,inherit);
            filter:invert(1);

            <div class="box red" style="--color:initial;">
            </div>
            <div class="box blue" style="--color:initial;">
            </div>

            <div class="box" style="background:grey;--color:initial;">
            </div>





            We simply need to set initial to the custom property in order to force the inherit to be used as a value within background.




            The usage of initial can also be useful in order to stop the propagation of CSS variable at a particular level since by default it's inherited by all the elements.



            Here is an example:






            :root 
            --color: blue;

            .container div
            border:5px solid var(--color,red);
            padding:5px;

            .stop
            --color:initial;

            .green
            --color:green;

            <div class="container">
            <div>
            <div>
            <div class="stop"> <!-- we stop at this level -->
            <div>
            </div>
            </div>
            </div>
            </div>
            </div>

            <div class="container stop"> <!-- we stop at this level -->
            <div>
            <div>
            <div class="green"> <!-- we redefine at this level -->
            <div>
            </div>
            </div>
            </div>
            </div>
            </div>







            1: it's about the inheritance of the custom property and not the background property






            share|improve this answer


























              up vote
              2
              down vote



              accepted










              In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:



              background:var(--color,inherit)


              By doing this, we tell our property (background) to use inherit in case --color is not defined.



              This may solve the issue but in our case it won't be enough since --color is always defined at :root level and will get inherited1 by the pseudo element thus we will never use the fallback value.



              To fix this we can consider the initial value in order to undefine our custom property and force the use of the fallback value. As described in the specification:




              The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().




              and




              To substitute a var() in a property’s value:



              1. If the custom property named by the first argument to the var()
                function is animation-tainted, and the var() function is being used in
                the animation property or one of its longhands, treat the custom
                property as having its initial value for the rest of this algorithm.

              2. If the value of the custom property named by the first argument to the
                var() function is anything but the initial value, replace the var()
                function by the value of the corresponding custom property. Otherwise,

              3. if the var() function has a fallback value as its second argument,
                replace the var() function by the fallback value. If there are any
                var() references in the fallback, substitute them as well.

              4. Otherwise, the property containing the var() function is invalid at
                computed-value time



              Our code will then look like this:






              :root 
              --color:rgba(20,20,20,0.5); /*defined as the default value*/


              .box
              width:50px;
              height:50px;
              display:inline-block;
              margin-right:30px;
              border-radius:50%;
              position:relative;

              .red background:rgba(255,0,0,0.5);
              .blue background:rgba(0,255,0,0.5);

              .box:before
              content:"";
              position:absolute;
              top:0;left:0;right:0;bottom:0;
              border-radius:50%;
              transform:translateX(30px);
              background:var(--color,inherit);
              filter:invert(1);

              <div class="box red" style="--color:initial;">
              </div>
              <div class="box blue" style="--color:initial;">
              </div>

              <div class="box" style="background:grey;--color:initial;">
              </div>





              We simply need to set initial to the custom property in order to force the inherit to be used as a value within background.




              The usage of initial can also be useful in order to stop the propagation of CSS variable at a particular level since by default it's inherited by all the elements.



              Here is an example:






              :root 
              --color: blue;

              .container div
              border:5px solid var(--color,red);
              padding:5px;

              .stop
              --color:initial;

              .green
              --color:green;

              <div class="container">
              <div>
              <div>
              <div class="stop"> <!-- we stop at this level -->
              <div>
              </div>
              </div>
              </div>
              </div>
              </div>

              <div class="container stop"> <!-- we stop at this level -->
              <div>
              <div>
              <div class="green"> <!-- we redefine at this level -->
              <div>
              </div>
              </div>
              </div>
              </div>
              </div>







              1: it's about the inheritance of the custom property and not the background property






              share|improve this answer
























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:



                background:var(--color,inherit)


                By doing this, we tell our property (background) to use inherit in case --color is not defined.



                This may solve the issue but in our case it won't be enough since --color is always defined at :root level and will get inherited1 by the pseudo element thus we will never use the fallback value.



                To fix this we can consider the initial value in order to undefine our custom property and force the use of the fallback value. As described in the specification:




                The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().




                and




                To substitute a var() in a property’s value:



                1. If the custom property named by the first argument to the var()
                  function is animation-tainted, and the var() function is being used in
                  the animation property or one of its longhands, treat the custom
                  property as having its initial value for the rest of this algorithm.

                2. If the value of the custom property named by the first argument to the
                  var() function is anything but the initial value, replace the var()
                  function by the value of the corresponding custom property. Otherwise,

                3. if the var() function has a fallback value as its second argument,
                  replace the var() function by the fallback value. If there are any
                  var() references in the fallback, substitute them as well.

                4. Otherwise, the property containing the var() function is invalid at
                  computed-value time



                Our code will then look like this:






                :root 
                --color:rgba(20,20,20,0.5); /*defined as the default value*/


                .box
                width:50px;
                height:50px;
                display:inline-block;
                margin-right:30px;
                border-radius:50%;
                position:relative;

                .red background:rgba(255,0,0,0.5);
                .blue background:rgba(0,255,0,0.5);

                .box:before
                content:"";
                position:absolute;
                top:0;left:0;right:0;bottom:0;
                border-radius:50%;
                transform:translateX(30px);
                background:var(--color,inherit);
                filter:invert(1);

                <div class="box red" style="--color:initial;">
                </div>
                <div class="box blue" style="--color:initial;">
                </div>

                <div class="box" style="background:grey;--color:initial;">
                </div>





                We simply need to set initial to the custom property in order to force the inherit to be used as a value within background.




                The usage of initial can also be useful in order to stop the propagation of CSS variable at a particular level since by default it's inherited by all the elements.



                Here is an example:






                :root 
                --color: blue;

                .container div
                border:5px solid var(--color,red);
                padding:5px;

                .stop
                --color:initial;

                .green
                --color:green;

                <div class="container">
                <div>
                <div>
                <div class="stop"> <!-- we stop at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>

                <div class="container stop"> <!-- we stop at this level -->
                <div>
                <div>
                <div class="green"> <!-- we redefine at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>







                1: it's about the inheritance of the custom property and not the background property






                share|improve this answer














                In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:



                background:var(--color,inherit)


                By doing this, we tell our property (background) to use inherit in case --color is not defined.



                This may solve the issue but in our case it won't be enough since --color is always defined at :root level and will get inherited1 by the pseudo element thus we will never use the fallback value.



                To fix this we can consider the initial value in order to undefine our custom property and force the use of the fallback value. As described in the specification:




                The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().




                and




                To substitute a var() in a property’s value:



                1. If the custom property named by the first argument to the var()
                  function is animation-tainted, and the var() function is being used in
                  the animation property or one of its longhands, treat the custom
                  property as having its initial value for the rest of this algorithm.

                2. If the value of the custom property named by the first argument to the
                  var() function is anything but the initial value, replace the var()
                  function by the value of the corresponding custom property. Otherwise,

                3. if the var() function has a fallback value as its second argument,
                  replace the var() function by the fallback value. If there are any
                  var() references in the fallback, substitute them as well.

                4. Otherwise, the property containing the var() function is invalid at
                  computed-value time



                Our code will then look like this:






                :root 
                --color:rgba(20,20,20,0.5); /*defined as the default value*/


                .box
                width:50px;
                height:50px;
                display:inline-block;
                margin-right:30px;
                border-radius:50%;
                position:relative;

                .red background:rgba(255,0,0,0.5);
                .blue background:rgba(0,255,0,0.5);

                .box:before
                content:"";
                position:absolute;
                top:0;left:0;right:0;bottom:0;
                border-radius:50%;
                transform:translateX(30px);
                background:var(--color,inherit);
                filter:invert(1);

                <div class="box red" style="--color:initial;">
                </div>
                <div class="box blue" style="--color:initial;">
                </div>

                <div class="box" style="background:grey;--color:initial;">
                </div>





                We simply need to set initial to the custom property in order to force the inherit to be used as a value within background.




                The usage of initial can also be useful in order to stop the propagation of CSS variable at a particular level since by default it's inherited by all the elements.



                Here is an example:






                :root 
                --color: blue;

                .container div
                border:5px solid var(--color,red);
                padding:5px;

                .stop
                --color:initial;

                .green
                --color:green;

                <div class="container">
                <div>
                <div>
                <div class="stop"> <!-- we stop at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>

                <div class="container stop"> <!-- we stop at this level -->
                <div>
                <div>
                <div class="green"> <!-- we redefine at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>







                1: it's about the inheritance of the custom property and not the background property






                :root 
                --color:rgba(20,20,20,0.5); /*defined as the default value*/


                .box
                width:50px;
                height:50px;
                display:inline-block;
                margin-right:30px;
                border-radius:50%;
                position:relative;

                .red background:rgba(255,0,0,0.5);
                .blue background:rgba(0,255,0,0.5);

                .box:before
                content:"";
                position:absolute;
                top:0;left:0;right:0;bottom:0;
                border-radius:50%;
                transform:translateX(30px);
                background:var(--color,inherit);
                filter:invert(1);

                <div class="box red" style="--color:initial;">
                </div>
                <div class="box blue" style="--color:initial;">
                </div>

                <div class="box" style="background:grey;--color:initial;">
                </div>





                :root 
                --color:rgba(20,20,20,0.5); /*defined as the default value*/


                .box
                width:50px;
                height:50px;
                display:inline-block;
                margin-right:30px;
                border-radius:50%;
                position:relative;

                .red background:rgba(255,0,0,0.5);
                .blue background:rgba(0,255,0,0.5);

                .box:before
                content:"";
                position:absolute;
                top:0;left:0;right:0;bottom:0;
                border-radius:50%;
                transform:translateX(30px);
                background:var(--color,inherit);
                filter:invert(1);

                <div class="box red" style="--color:initial;">
                </div>
                <div class="box blue" style="--color:initial;">
                </div>

                <div class="box" style="background:grey;--color:initial;">
                </div>





                :root 
                --color: blue;

                .container div
                border:5px solid var(--color,red);
                padding:5px;

                .stop
                --color:initial;

                .green
                --color:green;

                <div class="container">
                <div>
                <div>
                <div class="stop"> <!-- we stop at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>

                <div class="container stop"> <!-- we stop at this level -->
                <div>
                <div>
                <div class="green"> <!-- we redefine at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>





                :root 
                --color: blue;

                .container div
                border:5px solid var(--color,red);
                padding:5px;

                .stop
                --color:initial;

                .green
                --color:green;

                <div class="container">
                <div>
                <div>
                <div class="stop"> <!-- we stop at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>

                <div class="container stop"> <!-- we stop at this level -->
                <div>
                <div>
                <div class="green"> <!-- we redefine at this level -->
                <div>
                </div>
                </div>
                </div>
                </div>
                </div>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 9:45

























                answered Nov 10 at 14:22









                Temani Afif

                62.2k93573




                62.2k93573



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239880%2fhow-to-store-inherit-value-inside-a-css-custom-property-aka-css-variables%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo