Dynamically change color to lighter or darker by percentage CSS (Javascript)

后端 未结 24 1662
-上瘾入骨i
-上瘾入骨i 2020-11-28 01:12

We have a big application on the site and we have a few links which are, let\'s say blue color like the blue links on this site. Now I want to make some other links, but wit

相关标签:
24条回答
  • 2020-11-28 01:24

    This is an old question, but most of the answers require the use of a preprocessor or JavaScript to operate, or they not only affect the color of the element but also the color of the elements contained within it. I am going to add a kind-of-complicated CSS-only way of doing the same thing. Probably in the future, with the more advanced CSS functions, it will be easier to do.

    The idea is based on using:

    • RGB colors, so you have separate values for red, green, and blue;
    • CSS variables, to store the color values and the darkness; and
    • The calc function, to apply the change.

    By default darkness will be 1 (for 100%, the regular color), and if you multiply by a number between 0 and 1, you'll be making the color darker. For example, if you multiply by 0.85 each of the values, you'll be making the colors 15% darker (100% - 15% = 85% = 0.85).

    Here is an example, I created three classes: .dark, .darker, and .darkest that will make the original color 25%, 50%, and 75% darker respectively:

    html {
      --clarity: 1;
    }
    
    div {
      display: inline-block;
      height: 100px;
      width: 100px;
      line-height: 100px;
      color: white;
      text-align: center;
      font-family: arial, sans-serif;
      font-size: 20px;
      background: rgba(
                      calc(var(--r) * var(--clarity)), 
                      calc(var(--g) * var(--clarity)), 
                      calc(var(--b) * var(--clarity))
                    );
    }
    
    .dark    { --clarity: 0.75; }
    .darker  { --clarity: 0.50; }
    .darkest { --clarity: 0.25; }
    
    .red {
      --r: 255;
      --g: 0;
      --b: 0;
    }
    
    .purple {
      --r: 205;
      --g: 30;
      --b: 205;
    }
    <div class="red">0%</div>
    <div class="red dark">25%</div>
    <div class="red darker">50%</div>
    <div class="red darkest">75%</div>
    
    <br/><br/>
    
    <div class="purple">0%</div>
    <div class="purple dark">25%</div>
    <div class="purple darker">50%</div>
    <div class="purple darkest">75%</div>

    0 讨论(0)
  • 2020-11-28 01:27

    I am adding an answer using raw CSS3 and SVG without requiring LESS or SASS.

    Basically, if the question is to make a colour 10%,25%,50% ligher or darker for the sakes of a global hover effect you can create an SVG data call like this

    :root{
    --lighten-bg: url('data:image/svg+xml;utf8,<svg version="1.1" id="cssLighten" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50px" height="50px" viewBox="0 0 50 50" enable-background="new 0 0 50 50" xml:space="preserve"><rect opacity="0.2" fill="white" width="50" height="50"/></svg>') !important;
    --darken-bg: url('data:image/svg+xml;utf8,<svg version="1.1" id="cssDarken" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50px" height="50px" viewBox="0 0 50 50" enable-background="new 0 0 50 50" xml:space="preserve"><rect opacity="0.2" fill="black" width="50" height="50"/></svg>') !important;
    

    }

    .myButton{
        color: white;
        background-color:blue;
    }
    .myButton:hover{
        background-image: var(--lighten-bg);
    }
    

    For some reason, unknown to me, the SVG won't allow me to enter a hex value in the "fill" attribute but "white" and "black" satisfy my need.

    Food for thought: If you didn't mind using images, just use a 50% transparent PNG as the background image. If you wanted to be fancy, call a PHP script as the background image and pass it the HEX and OPACITY values and let it spit out the SVG code above.

    0 讨论(0)
  • 2020-11-28 01:29

    if you decide to use http://compass-style.org/, a sass-based css framework, it provides very useful darken() and lighten() sass functions to dynamically generate css. it's very clean:

    @import compass/utilities
    
    $link_color: #bb8f8f
    a
      color: $link_color
    a:visited
      color: $link_color
    a:hover
      color: darken($link_color,10)
    

    generates

    a {
      color: #bb8f8f;
    }
    
    a:visited {
      color: #bb8f8f;
    }
    
    a:hover {
      color: #a86f6f;
    }
    
    0 讨论(0)
  • 2020-11-28 01:29

    You can use a JavaScript library such as JXtension which gives the user the ability to make a color lighter or darker. If you would like to find documentation for this brand new library, click here. You can also use JXtension to combine one color with another.

    0 讨论(0)
  • 2020-11-28 01:30

    In LESS, you would use the following variables:

    @primary-color: #999;
    @primary-color-lighter: lighten(@primary-color, 20%);
    

    This would take the 1st variable and lighten it by 20% (or any other percentage). In this example, you'd end up with your lighter color being: #ccc

    0 讨论(0)
  • 2020-11-28 01:31

    There is "opacity" which will make the background shine through:

    opacity: 0.5;
    

    but I'm not sure this is what you mean. Define "reduce color": Make transparent? Or add white?

    0 讨论(0)
提交回复
热议问题