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

后端 未结 24 1665
-上瘾入骨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:38

    You could use RGBa ('a' being alpha transparency), but it's not widely supported yet. It will be, though, so you could use it now and add a fallback:

    a:link { 
        color: rgb(0,0,255); 
        }
    a:link.lighter {
        color: rgb(128,128,255); /* This gets applied only in browsers that don't apply the rgba line */
        }
    a:link.lighter { /* This comes after the previous line, so has priority in supporting browsers */
        color: rgba(0,0,255,0.5); /* last value is transparency */
        }
    
    0 讨论(0)
  • 2020-11-28 01:39

    At the time of writing, here's the best pure CSS implementation for color manipulation I found:

    Use CSS variables to define your colors in HSL instead of HEX/RGB format, then use calc() to manipulate them.

    Here's a basic example:

    :root {
      --link-color-h: 211;
      --link-color-s: 100%;
      --link-color-l: 50%;
      --link-color-hsl: var(--link-color-h), var(--link-color-s), var(--link-color-l);
    
      --link-color: hsl(var(--link-color-hsl));
      --link-color-10: hsla(var(--link-color-hsl), .1);
      --link-color-20: hsla(var(--link-color-hsl), .2);
      --link-color-30: hsla(var(--link-color-hsl), .3);
      --link-color-40: hsla(var(--link-color-hsl), .4);
      --link-color-50: hsla(var(--link-color-hsl), .5);
      --link-color-60: hsla(var(--link-color-hsl), .6);
      --link-color-70: hsla(var(--link-color-hsl), .7);
      --link-color-80: hsla(var(--link-color-hsl), .8);
      --link-color-90: hsla(var(--link-color-hsl), .9);
    
      --link-color-warm: hsl(calc(var(--link-color-h) + 80), var(--link-color-s), var(--link-color-l));
      --link-color-cold: hsl(calc(var(--link-color-h) - 80), var(--link-color-s), var(--link-color-l));
    
      --link-color-low: hsl(var(--link-color-h), calc(var(--link-color-s) / 2), var(--link-color-l));
      --link-color-lowest: hsl(var(--link-color-h), calc(var(--link-color-s) / 4), var(--link-color-l));
    
      --link-color-light: hsl(var(--link-color-h), var(--link-color-s), calc(var(--link-color-l) / .9));
      --link-color-dark: hsl(var(--link-color-h), var(--link-color-s), calc(var(--link-color-l) * .9));
    }
    
    .flex {
      display: flex;
    }
    
    .flex > div {
      flex: 1;
      height: calc(100vw / 10);
    }
    <h3>Color Manipulation (alpha)</h3>
    
    <div class="flex">
      <div style="background-color: var(--link-color-10)"></div>
      <div style="background-color: var(--link-color-20)"></div>
      <div style="background-color: var(--link-color-30)"></div>
      <div style="background-color: var(--link-color-40)"></div>
      <div style="background-color: var(--link-color-50)"></div>
      <div style="background-color: var(--link-color-60)"></div>
      <div style="background-color: var(--link-color-70)"></div>
      <div style="background-color: var(--link-color-80)"></div>
      <div style="background-color: var(--link-color-90)"></div>
      <div style="background-color: var(--link-color)"></div>
    </div>
    
    <h3>Color Manipulation (Hue)</h3>
    
    <div class="flex">
      <div style="background-color: var(--link-color-warm)"></div>
      <div style="background-color: var(--link-color)"></div>
      <div style="background-color: var(--link-color-cold)"></div>
    </div>
    
    <h3>Color Manipulation (Saturation)</h3>
    
    <div class="flex">
      <div style="background-color: var(--link-color)"></div>
      <div style="background-color: var(--link-color-low)"></div>
      <div style="background-color: var(--link-color-lowest)"></div>
    </div>
    
    <h3>Color Manipulation (Lightness)</h3>
    
    <div class="flex">
      <div style="background-color: var(--link-color-light)"></div>
      <div style="background-color: var(--link-color)"></div>
      <div style="background-color: var(--link-color-dark)"></div>
    </div>

    I also created a CSS framework (still in early stage) to provide basic CSS variables support called root-variables.

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

    You could use a little javascript to calculate the darker and lighter color using the rgb().

    Fiddle : Not really nice, but it is just for illustration.

    What it essentially does is sets a color and selects 20 colors with the same amount (compared to one another) of rgb only with 10 apart.

    for (var i=-10; i < $('.row:eq(0) .block').length/2 ; i++) {
     var r = 91;
     var g = 192;
     var b = 222;
     $('.row:eq(1) .block:eq('+(i+10)+')').css('background' , color(r+(i*10),g+(i*10),b+   (i*10))      );
    };
    
    0 讨论(0)
  • 2020-11-28 01:39

    See my comment on Ctford's reply.

    I'd think the easy way to lighten a color would be to take each of the RGB components, add to 0xff and divide by 2. If that doesn't give the exact results you want, take 0xff minus the current value times some constant and then add back to the current value. For example if you want to shift 1/3 of the way toward white, take (0xff - current)/3+current.

    You'd have to play with it to see what results you got. I would worry that with this simple a formula, a factor big enough to make dark colors fade nicely might make light colors turn completely white, while a factor small enough to make light colors only lighten a little might make dark colors not lighten enough.

    Still, I think going by a fraction of the distance to white is more promising than a fixed number of steps.

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

    I know it's late but, you could use a wrapper to your buttons and change a rgba color function opacity level, as said in other answers but with no explicit example.

    Here's a pen:

    https://codepen.io/aronkof/pen/WzGmjR

    #wrapper {
      width: 50vw;
      height: 50vh;
      background-color: #AAA;
      margin: 20px auto;
      border-radius: 5px;
      display: grid;
      place-items: center;
    } 
    
    .btn-wrap {
      background-color: #000;
      display: inline-block;
    }
    
    button {
      transition: all 0.6s linear;
      background-color: rgba(0, 255, 0, 1);
      border: none;
      outline: none;
      color: #fff;
      padding: 50px;
      font-weight: 700;
      font-size: 2em;
    }
    
    button:hover {
      background-color: rgba(0, 255, 0, .5);
    }
    <div id="wrapper">
      <div class="btn-wrap">
        <button class="btn">HEY!</buutton>
      </div>
    </div>

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

    Try:

    a {
      color: hsl(240, 100%, 50%);
    }
    
    a:hover {
      color: hsl(240, 100%, 70%);
    }
    
    0 讨论(0)
提交回复
热议问题