问题
In my CSS I need an color based on user picked color. The color picked is used with a fixed transparency of 80%.
The following form element will let the user choose a color easily.
<input type=color value=#00ffff> // #00ffff
But how can I add transparency to it in CSS?
rgba(#00ffff,.8) // doesn't work
Update: I specifically asked how to do it in CSS, not in JS. BTW it's for a PHP driven CMS. I don't wanna force JS on users, and I prefer not to write conversion functions in PHP.
Probably I have to wait for CSS4 (SASS/LESS like) color functions, or for the input type color element to be enhanced with an hsla/rgba pattern attribute functionality.
回答1:
you can do it like this using jquery, you can apply a custom color with a custom transparency to an element.
$(document).ready(function() {
$("input").change(function() {
var opacity = $("input[type=range]").val();
var color = $("input[type=color]").val();
var rgbaCol = 'rgba(' + parseInt(color.slice(-6, -4), 16) + ',' + parseInt(color.slice(-4, -2), 16) + ',' + parseInt(color.slice(-2), 16) + ',' + opacity + ')';
$('div').css('background-color', rgbaCol)
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type=color value=#00ffff>
<!-- color -->
<input type="range" min="0" max="1" step="0.1">
<!-- transparency -->
<div>this is a div</div>
回答2:
Use like this,
background-color : rgba(0, 255, 255, 0.8);
Refer this,
http://www.wufoo.com/html5/types/6-color.html
回答3:
In CSS4 there is the 8 digits RGB hexadecimal notation: #RRGGBBAA. Last to digits are values for the alpha channel.
That makes it easy to concatenate the alpha value easily to the hex string.
Not sure about browser support at the moment.
回答4:
If you want to use this color but only knowing the HEX type, try convert it into RGB or HSL by using color picker tool, there r many online. Then just add alpha chanel ( 0 -1) for transparency. Eg. Rgb(225,10,10, 0.7) Use this: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Colors/Color_picker_tool
回答5:
Try this
opacity: 0.5;
This will display the transparent color for the user picked one.
来源:https://stackoverflow.com/questions/40280110/how-to-add-transparency-to-a-value-from-a-html-input-type-color-field-in-css