【问题标题】:Using a background-image for checkbox为复选框使用背景图像
【发布时间】:2021-09-30 23:45:37
【问题描述】:

我正在尝试设置一些复选框,以便在未选中时使用我拥有的一张图片,然后在选中时使用另一张图片。这是我当前的复选框:

<input type="checkbox" name="fordCheckBox" value="Ford">

我还使用以下 CSS 来设置每个复选框的背景图像。

input[type=checkbox][name=fordCheckBox] {
        background:url("https://pbs.twimg.com/profile_images/550291079719698432/LbybHjIh.jpeg") no-repeat;
        height: 25px;
        width: 25px;
        background-size: 50%;
    }

    input[type=checkbox][name=fordCheckBox]:checked {
        background:url("http://contentservice.mc.reyrey.net/image_v1.0.0/2267461") no-repeat;
        height: 25px;
        width: 25px;
        background-size: 50%;
    }

正如您在我的JSFiddle example 中看到的那样,图标大小正确,但它从未像应有的那样设置背景图像。有什么建议吗?

【问题讨论】:

  • 你的背景图片应该是什么样子???不清楚吗??

标签: html css checkbox


【解决方案1】:

你可以用标签来做

   input[type=checkbox] {
    display:none;
}

input[type=checkbox] + label {
    display:inline-block;
    padding: 0 0 0 0px;
    background:url("https://pbs.twimg.com/profile_images/550291079719698432/LbybHjIh.jpeg") no-repeat;
    height: 125px;
    width: 125px;;
    background-size: 50%;
}

input[type=checkbox]:checked + label {
    background:url("http://contentservice.mc.reyrey.net/image_v1.0.0/2267461") no-repeat;
    height: 125px;
    width: 125px;
    display:inline-block;
    background-size: 50%;
}

HTML

<input type="checkbox" name="fordCheckBox" id="Ford" value="Ford">
<label for="Ford"></label>

请检查更新的 jsfiddle https://jsfiddle.net/s4nr6q3d/

【讨论】:

  • 请更改背景图片的网址,然后您可以看到它的工作原理。
  • 我将如何处理具有多个图像的多个复选框。每个复选框都有不同的图像。
【解决方案2】:

您实际上不应该直接尝试更改复选框;某些浏览器将始终显示默认复选框。通常是通过为链接到输入 id 字段的复选框添加标签来完成的。例如:

<input type='checkbox' id='x' name='fordCheckBox'>
<label for='x'></label>

然后,您将复选框设置为 display: none,然后将实际的 CSS 应用到标签上。您的选择器应如下所示:input[type=checkbox]:checked + label

我发现这篇文章将帮助解释一切并逐步引导您完成:

http://webdesign.tutsplus.com/tutorials/quick-tip-easy-css3-checkboxes-and-radio-buttons--webdesign-8953

【讨论】:

    【解决方案3】:

    我使用了这个 CSS。在这个 CSS 中,当复选框被选中时,它会在复选框上放置一个刻度图像。它还默认更改复选框的大小、颜色和边框。

        input[type='checkbox'] {
            -webkit-appearance: none;
            width: 30px;
            height: 30px;
            background: white;
            border-radius: 5px;
            border: 2px solid #555;
            vertical-align:middle;
        }
    
        input[type='checkbox']:checked {
            background-image: url("Images/tick-sm.png");
            background-size: 90%;
            background-position: right center;
            background-repeat: no-repeat;
       }
    

    用您自己的图片替换“Images/tick-sm.png”。

    如果您也想为未选中状态使用不同的背景,请将第二个块的相同内容添加到第一个块并替换图像。

    【讨论】:

      【解决方案4】:

      我做了一个“使用像复选框和无线电纯Css这样的图像”的例子

      Link to Stackblitz

      在 index.html 文件中:

      <div class="cont-bg">
        <div class="cont-title">Checkbox</div>
        <div class="cont-main">
          <div class="cont-checkbox">
            <input type="checkbox" id="myCheckbox-1" />
            <label for="myCheckbox-1">
              <img
                src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2021-mazda-mx-5-miata-mmp-1-1593459650.jpg?crop=0.781xw:0.739xh;0.109xw,0.0968xh&resize=480:*"
              />
              <span class="cover-checkbox">
                <svg viewBox="0 0 12 10">
                  <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
                </svg>
              </span>
              <div class="info">Mazda MX-5 Miata</div>
            </label>
          </div>
          <div class="cont-checkbox">
            <input type="checkbox" id="myCheckbox-2" />
            <label for="myCheckbox-2">
              <img
                src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2020-chevrolet-corvette-c8-102-1571146873.jpg?crop=0.548xw:0.411xh;0.255xw,0.321xh&resize=980:*"
              />
              <span class="cover-checkbox">
                <svg viewBox="0 0 12 10">
                  <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
                </svg>
              </span>
              <div class="info">Toyota Supra</div>
            </label>
          </div>
        </div>
        <div class="cont-title">Radio</div>
        <div class="cont-main">
          <div class="cont-checkbox">
            <input type="radio" name="myRadio" id="myRadio-1" />
            <label for="myRadio-1">
              <img
                src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2021-mazda-mx-5-miata-mmp-1-1593459650.jpg?crop=0.781xw:0.739xh;0.109xw,0.0968xh&resize=480:*"
              />
              <span class="cover-checkbox">
                <svg viewBox="0 0 12 10">
                  <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
                </svg>
              </span>
              <div class="info">Mazda MX-5 Miata</div>
            </label>
          </div>
          <div class="cont-checkbox">
            <input type="radio" name="myRadio" id="myRadio-2" />
            <label for="myRadio-2">
              <img
                src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2020-chevrolet-corvette-c8-102-1571146873.jpg?crop=0.548xw:0.411xh;0.255xw,0.321xh&resize=980:*"
              />
              <span class="cover-checkbox">
                <svg viewBox="0 0 12 10">
                  <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
                </svg>
              </span>
              <div class="info">Toyota Supra</div>
            </label>
          </div>
        </div>
      </div>
      

      在 style.css 文件中:

      * {
        font-family: Lato;
        margin: 0;
        padding: 0;
        --transition: 0.15s;
        --border-radius: 0.5rem;
        --background: #ffc107;
        --box-shadow: #ffc107;
      }
      
      .cont-bg {
        min-height: 100vh;
        background-image: radial-gradient(
          circle farthest-corner at 7.2% 13.6%,
          rgba(37, 249, 245, 1) 0%,
          rgba(8, 70, 218, 1) 90%
        );
        padding: 1rem;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }
      
      .cont-title {
        color: white;
        font-size: 1.25rem;
        font-weight: 600;
        margin-bottom: 1rem;
      }
      
      .cont-main {
        display: flex;
        flex-wrap: wrap;
        align-content: center;
        justify-content: center;
      }
      
      .cont-checkbox {
        width: 150px;
        height: 100px;
        border-radius: var(--border-radius);
        box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
        background: white;
        transition: transform var(--transition);
      }
      
      .cont-checkbox:first-of-type {
        margin-bottom: 0.75rem;
        margin-right: 0.75rem;
      }
      
      .cont-checkbox:active {
        transform: scale(0.9);
      }
      
      input {
        display: none;
      }
      
      input:checked + label {
        opacity: 1;
        box-shadow: 0 0 0 3px var(--background);
      }
      
      input:checked + label img {
        -webkit-filter: none; /* Safari 6.0 - 9.0 */
        filter: none;
      }
      
      input:checked + label .cover-checkbox {
        opacity: 1;
        transform: scale(1);
      }
      
      input:checked + label .cover-checkbox svg {
        stroke-dashoffset: 0;
      }
      
      label {
        display: inline-block;
        cursor: pointer;
        border-radius: var(--border-radius);
        overflow: hidden;
        width: 100%;
        height: 100%;
        position: relative;
        opacity: 0.6;
      }
      
      label img {
        width: 100%;
        height: 70%;
        object-fit: cover;
        clip-path: polygon(0% 0%, 100% 0, 100% 81%, 50% 100%, 0 81%);
        -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
        filter: grayscale(100%);
      }
      
      label .cover-checkbox {
        position: absolute;
        right: 5px;
        top: 3px;
        z-index: 1;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: var(--box-shadow);
        border: 2px solid #fff;
        transition: transform var(--transition),
          opacity calc(var(--transition) * 1.2) linear;
        opacity: 0;
        transform: scale(0);
      }
      
      label .cover-checkbox svg {
        width: 13px;
        height: 11px;
        display: inline-block;
        vertical-align: top;
        fill: none;
        margin: 5px 0 0 3px;
        stroke: #fff;
        stroke-width: 2;
        stroke-linecap: round;
        stroke-linejoin: round;
        stroke-dasharray: 16px;
        transition: stroke-dashoffset 0.4s ease var(--transition);
        stroke-dashoffset: 16px;
      }
      
      label .info {
        text-align: center;
        margin-top: 0.2rem;
        font-weight: 600;
        font-size: 0.8rem;
      }
      

      我做了一个“Angular - 使用复选框和收音机等图像”的示例

      Link to Stackblitz

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 2014-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        相关资源
        最近更新 更多