将HTML复选框的"勾号"标记替换为图像或任何其他符号

pri*_*ime 5 html css

如何Tick在HTML复选框中使用不同的图形(图像或其他符号)而不是默认标记.

我读了许多SO问题,但不能那样做.当我看到这个问题改变刻度线的颜色这个.一切都是老问题.但它太复杂了.

有没有简单的方法来做到这一点.我希望现在有一个简单而好的方法吗?

som*_*ere 7

实际上,使用CSS和伪元素这是一种简单的方法.我正在使用contentUnicode添加复选标记,但您也可以使用背景,只需确保定位正确.

这个实现只需要一个复选框本身,所以不需要额外的label等等......你的文本标签甚至可以在任何地方正常运行!

input[type="checkbox"]{
    -webkit-appearance: initial;
    appearance: initial;
    background: gray;
    width: 40px;
    height: 40px;
    border: none;
    background: gray;
    position: relative;
}
input[type="checkbox"]:checked {
    background: red;
}
input[type="checkbox"]:checked:after {
    /* Heres your symbol replacement - this is a tick in Unicode. */
    content: "\2713";
    color: #fff;
    /* The following positions my tick in the center, 
     * but you could just overlay the entire box
     * with a full after element with a background if you want to */
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%,-50%);
    -moz-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    /*
     * If you want to fully change the check appearance, use the following:
     * content: " ";
     * width: 100%;
     * height: 100%;
     * background: blue;
     * top: 0;
     * left: 0;
     */
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" />
Run Code Online (Sandbox Code Playgroud)


Dan*_*han 0

\r\n
\r\n
/* Base for label styling */\r\n[type="checkbox"]:not(:checked),\r\n[type="checkbox"]:checked {\r\n  position: absolute;\r\n  left: -9999px;\r\n}\r\n[type="checkbox"]:not(:checked) + label,\r\n[type="checkbox"]:checked + label {\r\n  position: relative;\r\n  padding-left: 25px;\r\n  cursor: pointer;\r\n}\r\n\r\n/* checkbox aspect */\r\n[type="checkbox"]:not(:checked) + label:before,\r\n[type="checkbox"]:checked + label:before {\r\n  content: \'\';\r\n  position: absolute;\r\n  left:0; top: 2px;\r\n  width: 17px; height: 17px;\r\n  border: 1px solid #aaa;\r\n  background: #f8f8f8;\r\n  border-radius: 3px;\r\n  box-shadow: inset 0 1px 3px rgba(0,0,0,.3)\r\n}\r\n/* checked mark aspect */\r\n[type="checkbox"]:not(:checked) + label:after,\r\n[type="checkbox"]:checked + label:after {\r\n  content: \'\xe2\x9c\x94\';\r\n  position: absolute;\r\n  top: 0; left: 4px;\r\n  font-size: 14px;\r\n  color: #09ad7e;\r\n  transition: all .2s;\r\n}\r\n/* checked mark aspect changes */\r\n[type="checkbox"]:not(:checked) + label:after {\r\n  opacity: 0;\r\n  transform: scale(0);\r\n}\r\n[type="checkbox"]:checked + label:after {\r\n  opacity: 1;\r\n  transform: scale(1);\r\n}\r\n/* disabled checkbox */\r\n[type="checkbox"]:disabled:not(:checked) + label:before,\r\n[type="checkbox"]:disabled:checked + label:before {\r\n  box-shadow: none;\r\n  border-color: #bbb;\r\n  background-color: #ddd;\r\n}\r\n[type="checkbox"]:disabled:checked + label:after {\r\n  color: #999;\r\n}\r\n[type="checkbox"]:disabled + label {\r\n  color: #aaa;\r\n}\r\n/* accessibility */\r\n[type="checkbox"]:checked:focus + label:before,\r\n[type="checkbox"]:not(:checked):focus + label:before {\r\n  border: 1px dotted blue;\r\n}\r\n\r\n/* hover style just for information */\r\nlabel:hover:before {\r\n  border: 1px solid #4778d9!important;\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
<form action="#">\r\n  <p>\r\n    <input type="checkbox" id="test1" />\r\n    <label for="test1">Red</label>\r\n  </p>\r\n  <p>\r\n    <input type="checkbox" id="test2" checked="checked" />\r\n    <label for="test2">Yellow</label>\r\n  </p>\r\n  <p>\r\n    <input type="checkbox" id="test3" checked="checked" disabled="disabled" />\r\n    <label for="test3">Green</label>\r\n  </p>\r\n    <p>\r\n      <input type="checkbox" id="test4" disabled="disabled" />\r\n      <label for="test4">Brown</label>\r\n  </p>\r\n</form>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

这是一个非常简单、纯 html 和 css 的自定义复选框示例。

\n