The input type="color"
has a default color which is black: #000000
.
Even if I give it an empty value...
<input type="color" value="" />
...the default color is always black.
I want the user to have the option of picking a color, but if he doesn't it means no color was picked, not even white #FFFFFF
.
Is there a way to force an input type="color"
not to have black color as default?
I can use some kind of a "listener" to check if the user changed the color for the first time, if not, ignore the value, but I would like to avoid Javascript.
While using hexcode for
value
attribute in<input type="color">
, one thing I noticed is that it has to be six digits, if for white you use#fff
, it does not work. It has to be#ffffff
.The same thing happens when setting it through javascript.
document.querySelector('input[type="color"]').value = '#fff'
does not work. The color remains black. You have to usedocument.querySelector('input[type="color"]').value = '#ffffff'
for it to work.Something to be careful about.
Use value:
If you want to know if input remain unchanged, you can do something like this (with jQuery):
http://jsfiddle.net/j3hZB/
Don't know if this issue is only available on chrome, but I just found the quick fix for chrome. We need to set the input value to #FFFFFF first and after that set it to default value, the default color will appear instead of black
Hope it help someone :)
I have implemented this kind of solution for myself. It displays nice "transparent" button. When clicked it triggers the normal hidden input-color. When color is picked up, the transparent button will hide and the input-color will show up.
Cheers.
Here is my solution, switching input type from text to color:
Edit: Now since, I have understood your question correctly, I have updated my answer.
Although the W3C Spec defines that the
value
attribute has a string representing the color, it doesn't define the default color. So I think that the implementation of default color is left at the discretion of the browser.However, the WhatWG Spec anwers your question with this note,
Moreover, based on your expectation, the CSS language never defined a
NULL
attribute for any element, which makes it impossible for theinput type='color'
to haveNULL
as the default value.Workaround:
The workaround is present in the Shadow DOM API.
Using Chrome Developer Tools, I found that we can give a
transparent
color to the pseudo element::-webkit-color-swatch
background property -For the above CSS, your HTML should like this -
<input type="color">
. Now you don't need to have any kind of listener to tell if the user has changed the default color or not. You can simply treat thetransparent
color as the NULL color based on which you can make a decision whether the value was changed or not!I am sure that you will find similar kind of information from the Shadow DOM for Firefox to set transparent value for background. IE still remains a pain for us.