I use a JavaScript-generated HTML5 color picker element to let the user set the color of another element. When the browser executes input type="color"
it draws a rectangle that shows the currently selected color. I name this rectangle the "color well". The color well can be styled:
const pickerElem = document.createElement('input');
pickerElem.setAttribute ('type', 'color');
pickerElem.setAttribute ('style', "position: absolute; "
+ "top: 10px;" + "left: 10px;"
+ "width: 100px;" + "height: 100px;");
document.body.appendChild(pickerElem);
This draws the color well as expected, but with a white border about five pixels wide. I want to delete the white border.
I’ve tried adding properties like: border: none;
, border-style: hidden;
, padding: 0;
, margin: 0;
and border-color: none;
to the style, but the browser ignores them.
If I use border-color: black;
the browser adds a thin black border around the inner white border, but doesn’t change the inner border color.
How can I style the picker to completely delete the inner white border?
2
Answers
Since the color picker render is mostly ruled by the browser and hard to custom style, you may choose to embed it inside a wrapper div and set its opacity as zero and yet have a way to pick the color chosen to style such wrapper.
Here I used a wrapper div styled with the css rule
.colorpicker
and its background color controlled by a function called when theoninput
event triggers on the embedded color picker:As Diego suggested, you could wrap your input in a wrapper and update the background color on
change
.This solution below provides a reusable function (plugin) that can be used to handle multiple inputs. It is even capable of syncing inputs with predefined
value
attributes.