输入类型为“颜色”,边框和所选颜色之间的空间

huangapple go评论127阅读模式
英文:

input type="color" space between border and selected color

问题

我有一个关于颜色输入的问题

输入类型为“颜色”,边框和所选颜色之间的空间

如您所见,黑色边框与颜色之间存在间隙,但我想要移除它

#color-picker {
  border: 5px solid black;
  border-radius: 5px;
  width: 207px;
  height: 60px;
  padding: 0px;
  appearance: none;
  cursor: pointer;
  background: none;
}

我以为 padding: 0 可能有帮助,但它什么都没做,我真的不知道该怎么办。

英文:

I have a problem with color input

输入类型为“颜色”,边框和所选颜色之间的空间

As you can see there's a gap between black border and color itself but I want to remove it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#color-picker {
  border: 5px solid black;
  border-radius: 5px;
  width: 207px;
  height: 60px;
  padding: 0px;
  appearance: none;
  cursor: pointer;
  background: none;
}

<!-- language: lang-html -->

&lt;!-- some other html up here --&gt;
&lt;div class=&quot;color-holder&quot;&gt;
  &lt;div class=&quot;content&quot;&gt;
    &lt;div id=&quot;colors&quot;&gt;
      &lt;!-- some colors here --&gt;
    &lt;/div&gt;
    &lt;input type=&quot;color&quot; id=&quot;color-picker&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- some other html down here --&gt;

<!-- end snippet -->

I thought padding: 0 could help but it did nothing at all and I just have no idea what to do

答案1

得分: 1

你试过使用轮廓属性吗?请记住如果使用它要考虑可访问性。

英文:

Did you try with outline property? Keep in mind accesibility if you use it

答案2

得分: 0

你可以为输入框创建一个标签并隐藏输入框...并通过一些 JavaScript 代码,你可以实现你想要的效果。

var theInput = document.getElementById("color-picker");
var theLabel = document.getElementById("trigger-color-picker");

theInput.addEventListener("input", function() {
  theLabel.style.background = theInput.value;
}, false);
theInput.dispatchEvent(new Event('input'));  // 触发加载时的输入事件
#trigger-color-picker {
    border: 5px solid black;
    border-radius: 5px;
    width: 207px;
    height: 60px;
    padding: 0px;
    appearance: none;
    cursor: pointer;
    display: block;
 }
 #color-picker{
  display: none;
 }
<!-- 这里是一些其他的 HTML 代码 -->
<div class="color-holder">
    <div class="content">
        <div id="colors">
            <!-- 这里是一些颜色 -->
        </div>
        <label id="trigger-color-picker" for="color-picker"></label>
        <input type="color" id="color-picker">
    </div>
</div>
<!-- 这里是一些其他的 HTML 代码 -->

这里的想法是:输入框的颜色类型默认具有样式,无法更改...因此我们将创建一个自定义输入框,而不是更改输入框的样式,我们将更改标签的样式。

英文:

You can create a label for the input and hide the input .. and with some lines of javascript you can achieve what you want

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var theInput = document.getElementById(&quot;color-picker&quot;);
var theLabel = document.getElementById(&quot;trigger-color-picker&quot;);

theInput.addEventListener(&quot;input&quot;, function() {
  theLabel.style.background = theInput.value;
}, false);
theInput.dispatchEvent(new Event(&#39;input&#39;));  // trigger the input event on load

<!-- language: lang-css -->

#trigger-color-picker {
    border: 5px solid black;
    border-radius: 5px;
    width: 207px;
    height: 60px;
    padding: 0px;
    appearance: none;
    cursor: pointer;
    display: block;
 }
 #color-picker{
  display: none;
 }

<!-- language: lang-html -->

&lt;!-- some other html up here --&gt;
&lt;div class=&quot;color-holder&quot;&gt;
    &lt;div class=&quot;content&quot;&gt;
        &lt;div id=&quot;colors&quot;&gt;
            &lt;!-- some colors here --&gt;
        &lt;/div&gt;
        &lt;label id=&quot;trigger-color-picker&quot; for=&quot;color-picker&quot;&gt;&lt;/label&gt;
        &lt;input type=&quot;color&quot; id=&quot;color-picker&quot;&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;!-- some other html down here --&gt;

<!-- end snippet -->

The idea here is: the input type color has styles by default you can't change it .. So we will create a custom one and instead of change the input style we will change the label style

答案3

得分: 0

感谢大家提供了一些想法,但我最终通过使用 ::-webkit-color-swatch::-webkit-color-swatch-wrapper 解决了这个问题。

border: none 移除了颜色值周围的1像素白色边框。

padding: 0px 移除了边框和颜色值之间的间隙。

显然,还需要添加 ::-moz-color-swatch::-moz-color-swatch-wrapper,并使用相同的属性。

英文:

Thanks everyone for some ideas but I finally resolved this issue with ::-webkit-color-swatch and ::-webkit-color-swatch-wrapper

输入类型为“颜色”,边框和所选颜色之间的空间

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#color-picker::-webkit-color-swatch {
    border: none;
}

#color-picker::-webkit-color-swatch-wrapper {
    padding: 0px;
}

<!-- end snippet -->

border: none removes that 1px white outline around color value

padding: 0px removes that gap between border and color value

And obviously also need to add ::-moz-color-swatch and ::-moz-color-swatch-wrapper with same props

huangapple
  • 本文由 发表于 2023年6月19日 01:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定