英文:
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 -->
<!-- some other html up here -->
<div class="color-holder">
<div class="content">
<div id="colors">
<!-- some colors here -->
</div>
<input type="color" id="color-picker">
</div>
</div>
<!-- some other html down here -->
<!-- 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("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 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 -->
<!-- some other html up here -->
<div class="color-holder">
<div class="content">
<div id="colors">
<!-- some colors here -->
</div>
<label id="trigger-color-picker" for="color-picker"></label>
<input type="color" id="color-picker">
</div>
</div>
<!-- some other html down here -->
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论