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

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

    As far as I know, there's no way you can do this in CSS.

    But I think that a little server-side logic could easily do as you suggest. CSS stylesheets are normally static assets, but there is no reason they couldn't be dynamically generated by server-side code. Your server-side script would:

    1. Check a URL parameter to determine the user and therefore the user's chosen colour. Use a URL parameter rather than a session variable so that you can still cache the CSS.
    2. Break up the colour into its red, green and blue components
    3. Increment each of the three components by a set amount. Experiment with this to get the results you are after.
    4. Generate CSS incorporating the new colour

    Links to this CSS-generating page would look something like:

    
    

    If you don't use the .css extension be sure to set the MIME-type correctly so that the browser knows to interpret the file as CSS.

    (Note that to make colours lighter you have to raise each of the RGB values)

提交回复
热议问题