html - 将复选框图像更改为自定义图像

标签 html css

我创建了一个带有自定义复选框的 div,我想通过自定义图像更改复选框图像。我已经完成了,但我无法更改图像的大小。请帮助我。

我们将不胜感激。

            <div class="left">

                        <input id="Option" type="checkbox">
            <label class="checkbox" for="Option"> </label>
            </div>

还有我用过的CSS。

.checkbox {
display: inline-block;
cursor: pointer;
font-size: 13px; line-height:18px;
}
input[type=checkbox] {
display:none;   
}
.checkbox:before {
content: "";
display: inline-block;
width: 25px;
height: 25px;
vertical-align: middle;
background-color: rgb(248,183,51);
color: #f3f3f3;
text-align: center;
}
input[type=checkbox]:checked + .checkbox:before {
content: url("images/Tick.png");
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
font-size: 15px;
}

最佳答案

您可以在不使用图像的情况下执行此操作,只需使用简单的字符和 CSS,因此您只需调整字体大小或更改颜色即可对自定义复选框进行更改。

请看下面的例子:

/* 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: 3px; left: 4px;
  font-size: 18px;
  line-height: 0.8;
  color: #09ad7e;
  transition: all .1s;
  transform-origin: center;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
  transform-origin: center;
}
[type="checkbox"]:checked + label:after {
  opacity: 1;
  transform: scale(1);
  transform-origin: center;
}
/* 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;
}






/* Useless styles, just for demo design */

body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
}
h1, h2 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}
h2 {
  margin: 5px 0 2em;
  color: #aaa;
}
form {
  width: 80px;
  margin: 0 auto;
}
.txtcenter {
  margin-top: 4em;
  font-size: .9em;
  text-align: center;
  color: #aaa;
}
.copy {
 margin-top: 2em; 
}
.copy a {
 text-decoration: none;
 color: #4778d9;
}
<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>

或者您也可以在 CodePen 上查看:Custom Checkbox Without images


(已编辑)

自定义复选框背景图像属性

请检查以下使用背景图像自定义复选框的实例:

.checkbox {
display: inline-block;
cursor: pointer;
font-size: 13px; line-height:18px;
}
input[type=checkbox] {
display:none;   
}
.checkbox:before {
content: "";
 display: inline-block;
 width: 25px;
 height: 25px;
 vertical-align: middle;
 background-color: rgb(248,183,51);
 color: #f3f3f3;
 text-align: center;
}
input[type=checkbox]:checked + .checkbox:before {
  background: url("https://cdn1.iconfinder.com/data/icons/windows8_icons_iconpharm/26/checked_checkbox.png") no-repeat center center;
 text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
 font-size: 15px;
 background-size: 10px 10px;
}
<div class="left">
  <input id="Option" type="checkbox">
  <label class="checkbox" for="Option"> </label>
</div>

  • 请确保您的图像存在于正确的路径中。
  • 另请考虑,您也可以通过设置背景属性的水平和垂直位置来重新定位背景图片。

关于html - 将复选框图像更改为自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37315766/

相关文章:

javascript - 像 benthebodyguard 这样的 html5 网站?

css - 垂直对齐 Bootstrap 3 列的内容

javascript - 评论时出错

html - 可悬停的菜单项

html - 没有 Javascript 的悬停选项卡

javascript - 如何计算实际的 innerWidth/innerHeight?

html - 如何使此页面响应

java - 如何在 JSP 中单击一个按钮来提交来自 for 循环的多个表单

javascript - 如何在页面加载时隐藏滚动条?

html - 背景位置