Webkit CSS to control the box around the color in an input[type=color]?

后端 未结 11 2068
天涯浪人
天涯浪人 2020-11-28 06:29

Is there a Webkit-specific CSS style that will allow me to control the color/size/style of the box around the color in an input[type=color]?

I\'m setting

11条回答
  •  不知归路
    2020-11-28 07:21

    WebKit has special CSS selectors you can use to customize form controls but they aren't official.
    An update to WebKit in the future will probably break it.

    Please don't use it for production!!

    But feel free to play with it for personal projects :)

    Method 1

    Uses webkit-specific selectors to mostly hide the non-colored part of the input.

    input[type="color"] {
    	-webkit-appearance: none;
    	border: none;
    	width: 32px;
    	height: 32px;
    }
    input[type="color"]::-webkit-color-swatch-wrapper {
    	padding: 0;
    }
    input[type="color"]::-webkit-color-swatch {
    	border: none;
    }

    Method 2

    Hides the color input (opacity:0) and uses JavaScript to set the background of the wrapper to the input's value.

    var color_picker = document.getElementById("color-picker");
    var color_picker_wrapper = document.getElementById("color-picker-wrapper");
    color_picker.onchange = function() {
    	color_picker_wrapper.style.backgroundColor = color_picker.value;    
    }
    color_picker_wrapper.style.backgroundColor = color_picker.value;
    input[type="color"] {
    	opacity: 0;
    	display: block;
    	width: 32px;
    	height: 32px;
    	border: none;
    }
    #color-picker-wrapper {
    	float: left;
    }

提交回复
热议问题