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

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

    Use the filter pure CSS property. for a complete description of the filter property functions read this awesome article.

    I had a same issue like yours, and I fixed it by using the brightness function of filter property:

    .my-class {
      background-color: #18d176;
      filter: brightness(90%);
    }
    
    0 讨论(0)
  • 2020-11-28 01:48

    If you need to brute force it for older browser compatibility, you can use Colllor to automatically select similar color variations.

    Example (color: #a9dbb4):

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

    You can do this with CSS filters in all modern browsers (see the caniuse compatibility table).

    .button {
      color: #ff0000;
    }
    
    /* note: 100% is baseline so 85% is slightly darker, 
       20% would be significantly darker */
    .button:hover {
      filter: brightness(85%);
    }
    <button class="button">Foo lorem ipsum</button>

    Here's more reading from CSS Tricks about the various filters you can use: https://css-tricks.com/almanac/properties/f/filter/

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

    HSL Colors provide an answer, a HSL color value is specified with: hsl(hue [0,255], saturation %, lightness %).

    HSL is supported in IE9+, Firefox, Chrome, Safari, and in Opera 10+

    a
    {
    color:hsl(240,65%,50%);
    }
    a.lighter 
    {
    color:hsl(240,65%,75%);
    }
    
    0 讨论(0)
  • 2020-11-28 01:50

    If you're using a stack which lets you use SASS, you can use the lighten function:

    $linkcolour: #0000FF;
    
    a {
      color: $linkcolour;
    }
    
    a.lighter {
      color: lighten($linkcolour, 50%);
    }
    
    0 讨论(0)
  • 2020-11-28 01:51

    Not directly, no. But you could use a site, such as colorschemedesigner.com, that will give you your base color and then give you the hex and rgb codes for different ranges of your base color.

    Once I find my color schemes for my site, I put the hex codes for the colors and name them inside a comment section at the top of my stylesheet.

    Some other color scheme generators include:

    • http://www.colorschemer.com/online.html
    • http://colorschemegenerator.com/
    • http://www.cssjuice.com/25-popular-color-scheme-and-palette-generators/
    0 讨论(0)
提交回复
热议问题