Reputation: 3109
Today I read about HTML5's color input and I thought I'd give it a try:
<input type="color" name="background" id="background" value="#ff0000">
When I click the input (in chrome and firefox, on windows), a color picker appears. However, it is positioned in the top left corner of my screen, not above the input.
Is this a known issue and will this be 'fixed' in the future? Is it possible to position the color picker through code? Or is this something that browsers can't do much about and that users have to live with?
Upvotes: 18
Views: 16079
Reputation: 9139
One more caveat is that when creating a custom picker control, use 0-size
instead of display: none
. Otherwise browser will place the picker in the corner (out of the Visual DOM tree).
<input type="color" width="0" height="0" value="#ff0000">
<button onClick="/* logic to show the picker */">Pick a colour</button>
Upvotes: 6
Reputation: 318
Here's a hacked solution which worked for me.
See the code below:
let colorPickerInputs = document.querySelectorAll('input[type=color]');
if (colorPickerInputs)
colorPickerInputs[0].setAttribute('style', 'position: absolute; top: 20px; opacity: 0;');
Upvotes: 2
Reputation: 19
I had this same question. I wanted to create a Theme editor and wanted to do this. Like the VS Code when editing a CSS file. I figured out some strategies for solving this problem:
Method A using positioned iframe and signalling changes between iframe and parent.
Method B using window.open(…)+ Ajax
Upvotes: 1
Reputation: 31839
The positioning of the input
of type color
is browser-specific implementation, in the official documentation there is no given rule for user-agents (i.e. browsers) how to position it over the page's element. This makes custom positioning via CSS for example, or JavaScript not possible.
However, there are some other rules (for example, there is always a color picked, and there is no way to set the value to the empty string.)
Keep in mind when using the input
of type color
, that Internet Explorer and Safari browsers do not support it yet.
Upvotes: 17