【问题标题】:Change check box background color更改复选框背景颜色
【发布时间】:2020-09-08 22:53:44
【问题描述】:

下午好。

我在更改选中复选框的背景颜色时遇到问题。

CheckBox to change

我在互联网上看到了一些选项,并在堆栈中看到了一些问题。但他们都没有成功。下面是插入元素的 DOM 图像。

Dom about element

我测试的最后一个代码是这样的,它仍然没有工作

input[type=checkbox]:checked:after{
    background-color: red !important;
    color: blue !important;
}

目标是在选中时具有此颜色的背景 #FFEA00,选中时箭头为 #000。

最好的问候和感谢您的帮助

【问题讨论】:

  • :after 不适用于input。将样式应用于label
  • 我想确认我收到了您的要求:您希望复选框的背景为#FFEA00,复选标记(通常为白色)为黑色?
  • 您好@LaurentC,非常感谢您的回复。是的,当它被检查为黄色(#FFEA00)和黑色箭头时,我想要背景颜色。我注意到蓝色(我要更改的颜色)是 Chrome 浏览器的默认颜色。在 firefox 的情况下,它不再有背景颜色并且有边框。换句话说,我认为它们是浏览器的默认行为。我尝试自定义标签和其他选项但没有成功
  • @LaurentC 请帮忙 :)
  • @PedroEstevesAntunes 好的,让我看看这个,我不确定这些元素是否可定制,但我会为你仔细检查。而且,是的,默认值因浏览器而异,绝对不同。

标签: css input checkbox background-color


【解决方案1】:

选中时为黄色背景,选中时为黑色箭头。在这里:

.checkbox-label {
    display: block;
    position: relative;
    margin: auto;
    cursor: pointer;
    font-size: 22px;
    line-height: 24px;
    height: 24px;
    width: 24px;
    clear: both;
}

.checkbox-label input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}

.checkbox-label .checkbox-custom {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 24px;
    width: 24px;
    background-color: transparent;
    border-radius: 5px;
    transition: all 0.3s ease-out;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    border: 2px solid #000;
}


.checkbox-label input:checked ~ .checkbox-custom {
    background-color: #FFEA00;
    border-radius: 5px;
    -webkit-transform: rotate(0deg) scale(1);
    -ms-transform: rotate(0deg) scale(1);
    transform: rotate(0deg) scale(1);
    opacity:1;
    border: 2px solid #000;
}

.checkbox-label .checkbox-custom::after {
    position: absolute;
    content: "";
    left: 12px;
    top: 12px;
    height: 0px;
    width: 0px;
    border-radius: 5px;
    border: solid #000;
    border-width: 0 3px 3px 0;
    -webkit-transform: rotate(0deg) scale(0);
    -ms-transform: rotate(0deg) scale(0);
    transform: rotate(0deg) scale(0);
    opacity:1;
    transition: all 0.3s ease-out;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
}


.checkbox-label input:checked ~ .checkbox-custom::after {
  -webkit-transform: rotate(45deg) scale(1);
  -ms-transform: rotate(45deg) scale(1);
  transform: rotate(45deg) scale(1);
  opacity:1;
  left: 8px;
  top: 3px;
  width: 6px;
  height: 12px;
  border: solid #000000;
  border-width: 0 2px 2px 0;
  background-color: transparent;
  border-radius: 0;
}
<div class="checkbox-container">
        <label class="checkbox-label">
            <input type="checkbox">
            <span class="checkbox-custom"></span>
        </label>
</div>

【讨论】:

  • 对像这样设置默认元素的样式请注意:它可能(将)在不同浏览器甚至浏览器版本中的行为不同,因此通常不建议这样做。这已经过测试并且在 85.0.4183.83 (Chrome) 中完全有效。
  • 伟大的 LaurentC。非常感谢。我有无法编辑类或添加的问题,因为 HTML 是由 wordpress 插件构建的。我想我将不得不使用 JavaScript 向元素添加类。我会测试然后给出反馈。非常感谢
  • 我很高兴,但如果你想在你的 WP 网站上实现你的 CSS 方面获得更多帮助,请告诉我,我有很多 WP 经验和一些空闲时间。我觉得您不需要使用 JS 来为您的 HTML 元素添加类...您可以在这里找到我的电子邮件联系人:laurentchevrette (dot) digital ....cheers - 感谢您接受我的回答。
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 2018-06-30
  • 2011-08-17
  • 2015-05-17
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多