【问题标题】:Checkbox with image and overlayed image on checked state选中状态下带有图像和覆盖图像的复选框
【发布时间】:2016-01-14 00:05:19
【问题描述】:

我需要关于仅使用 CSS 的复选框的帮助。

首先它运行正常。但是,我想在单击按钮时将选中的图像覆盖在未选中的图像之上。如何做到这一点?

.frm_checkbox {border: 1px solid red;}

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

input#field_32gyuu-0 {
	background: url('http://www.uswebcompany.com/plugins/formidable/wp-content/uploads/2016/01/christmasLights.png') 0 0px no-repeat;
    z-index: 10;
	content: "";
	display: inline-block;
	font-size: 12px;
	height: 75px;
	width: 75px;
	line-height: 16px;
	margin: -2px 6px 0 0;
	text-align: center;
	vertical-align: middle;
}

#frm_checkbox_112-0 input[type="checkbox"]:checked + label:before {
	background: url('http://www.uswebcompany.com/plugins/formidable/wp-content/uploads/2016/01/lightbox-checkmark.png') 0 0px no-repeat;
	height: 75px;
	width: 75px;
}
<div id="frm_field_162_container" class="frm_form_field form-field  form-group frm_top_container">
    <label  class="frm_primary_label control-label">Checkboxes
        <span class="frm_required"></span>
    </label>
    <div class="frm_opt_container">
        <div class="frm_checkbox checkbox" id="frm_checkbox_162-0">
            <label for="field_32gyuu-0">
                <input type="checkbox" name="item_meta[162][]" id="field_32gyuu-0" value="Option 1"  /> Option 1</label>
        </div>
        <div class="frm_checkbox checkbox" id="frm_checkbox_162-1">
            <label for="field_32gyuu-1">
                <input type="checkbox" name="item_meta[162][]" id="field_32gyuu-1" value="Option 2"  /> Option 2</label>
        </div>
    </div>
</div>

【问题讨论】:

  • 这里不起作用。尝试使用 jsfiddle 或 jsbin。
  • 先生,它正在工作至复选框。但是,我正在尝试删除复选框并放置图像,并使用覆盖的复选标记使其可点击。

标签: html css checkbox


【解决方案1】:

我需要将标签文本包装在span 中,但请看下面的代码。我不确定您是否还想看到该复选框,因此请根据需要切换display: none

.checkbox label {
  position: relative;
  display: table;
  margin-bottom: 15px;
  padding-left: 80px;
  height: 75px;
  background-color: #eee;
}
.checkbox label > span {
  display: table-cell;
  vertical-align: middle;
}
.checkbox label > span:before {
  content: url("http://www.uswebcompany.com/plugins/formidable/wp-content/uploads/2016/01/christmasLights.png");
  position: absolute;
  top: 0;
  left: 0;
  width: 75px;
  height: 75px;
}
[type="checkbox"] {
  display: none;
  margin-top: 31px;
}
[type="checkbox"]:checked + span:before {
  content: url("http://www.uswebcompany.com/plugins/formidable/wp-content/uploads/2016/01/lightbox-checkmark.png");
}
<div id="frm_field_162_container" class="frm_form_field form-field  form-group frm_top_container">
  <label class="frm_primary_label control-label">Checkboxes
    <span class="frm_required"></span>
  </label>
  <div class="frm_opt_container">
    <div class="frm_checkbox checkbox" id="frm_checkbox_162-0">
      <label for="field_32gyuu-0">
        <input type="checkbox" name="item_meta[162][]" id="field_32gyuu-0" value="Option 1" /> <span>Option 1</span>
      </label>
    </div>
    <div class="frm_checkbox checkbox" id="frm_checkbox_162-1">
      <label for="field_32gyuu-1">
        <input type="checkbox" name="item_meta[162][]" id="field_32gyuu-1" value="Option 2" /> <span>Option 2</span>
      </label>
    </div>
  </div>
</div>

【讨论】:

  • 有没有办法用原始代码和id来做到这一点?因为 i/m 使用了强大的 pro 并且代码是自动生成的。 (即)不添加跨度。
  • 别想了——我不这么认为,您不能在输入元素上使用::before::after 或使用CSS 向上导航。用 JavaScript 添加span 实用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多