【问题标题】:Change CheckBox image to custom image将 CheckBox 图像更改为自定义图像
【发布时间】:2021-10-01 01:38:10
【问题描述】:

我创建了一个带有自定义复选框的 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;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以在不使用图像的情况下执行此操作,只需使用简单的字符和 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>
    • 请确保您的图像存在于正确的路径中。
    • 另外请注意,您也可以通过设置背景属性的水平和垂直位置来重新定位背景图像。

    【讨论】:

    • 请用图片给我建议。 @Meghdad Hadidi
    • @NehaPurwar 这是使用背景图像的实时版本。正如 Paran0a 所说,您可以使用 background-size 属性来控制背景图像的大小。另外请确保您使用的图像位于正确的路径中。
    • @NehaPurwar 我已经编辑了背景图像解决方案的答案。
    • 非常好。但是,我在 Chrome 54、Firefox 49 或 Safari 10 中没有看到蓝色焦点装饰。Chrome 焦点正在运行——tab 移动光标,空格键选中/取消选中,但没有蓝色焦点。 (Safari 和 Firefox 无法正确响应 tab 或空格键。可能是演示框架?)
    【解决方案2】:

    我所做的主要是将背景设置为 background-sizecover

    .checkbox {
     display: inline-block;
     cursor: pointer;
     font-size: 13px; line-height:18px;
    }
    
    input[type=checkbox] {
     display:none;   
    }
    
    .checkbox:before {
     content: "";
     display: inline-block;
     width: 40px;
     height: 40px;
     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");
     text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
     font-size: 15px;
     background-size: contain;
    
    }
     <div class="left">
      <input id="Option" type="checkbox">
      <label class="checkbox" for="Option"> </label>
     </div>

    【讨论】:

    • 实际上,当我在背景中添加图像路径时,背景对我不起作用。它不显示图像。请告诉我我的错误是什么。
    • 我在背景属性中添加我的图像路径,如下背景:url('../images/Tick.png');这是错误的方式吗?如果是正确的,还有其他方法可以在复选框中添加自定义图像吗?请让我知道。我为此挣扎了一个小时。@Paran0a
    • 是的,当然,当我在内容标签中提供路径 url 但在这里我无法调整它的大小时它正在工作。
    • 背景:url("../images/Tick.png");这样做。
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多