【问题标题】:Replace the 'Tick' mark of a HTML check box with an image or any other symbol [duplicate]用图像或任何其他符号替换 HTML 复选框的“勾号”标记 [重复]
【发布时间】:2015-08-22 21:00:53
【问题描述】:

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

我阅读了许多 SO 问题,但无法做到。正如我看到的this question to change the color of the tick markthis。都是老问题。但这太复杂了。

有什么简单的方法可以做到这一点。我希望现在有一个简单更好的方法?

【问题讨论】:

  • 不..还是一样..

标签: html css


【解决方案1】:

实际上,使用 CSS 和伪元素有一个简单的方法来做到这一点。我正在使用content 在 Unicode 中添加复选标记,但您也可以使用背景,只要确保定位正确即可。

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

input[type="checkbox"]{
    -webkit-appearance: initial;
    appearance: initial;
    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" />

【讨论】:

    【解决方案2】:

    /* 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 示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2020-09-28
      • 2016-03-12
      • 2015-03-20
      • 1970-01-01
      相关资源
      最近更新 更多