如何在HTML复选框中使用不同的图形(图像或其他符号)而不是默认的Tick
标记。
我读了许多SO问题,但却无法做到。在我看到this question to change the color of the tick mark和this时。一切都是老问题。但它太复杂了。
有没有简单的方法可以做到这一点。我希望现在有一个简单而好的方法吗?
答案 0 :(得分:7)
实际上,使用CSS和伪元素这是一种简单的方法。我使用content
在Unicode中添加复选标记,但您也可以使用背景,只需确保定位正确。
此实现只需要一个复选框本身,所以没有额外的label
等等......您的文本标签甚至可以在任何地方正常运行!< / p>
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;
*/
}
<input type="checkbox" />
答案 1 :(得分:0)
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 25px;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 17px; height: 17px;
border: 1px solid #aaa;
background: #f8f8f8;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 4px;
font-size: 14px;
color: #09ad7e;
transition: all .2s;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
box-shadow: none;
border-color: #bbb;
background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
color: #999;
}
[type="checkbox"]:disabled + label {
color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
border: 1px dotted blue;
}
/* hover style just for information */
label:hover:before {
border: 1px solid #4778d9!important;
}
<form action="#">
<p>
<input type="checkbox" id="test1" />
<label for="test1">Red</label>
</p>
<p>
<input type="checkbox" id="test2" checked="checked" />
<label for="test2">Yellow</label>
</p>
<p>
<input type="checkbox" id="test3" checked="checked" disabled="disabled" />
<label for="test3">Green</label>
</p>
<p>
<input type="checkbox" id="test4" disabled="disabled" />
<label for="test4">Brown</label>
</p>
</form>
这是自定义复选框的非常简单和纯粹的html和css示例。