用复选框checkbox制作标签

10 篇文章 0 订阅

效果:

未选时:

选择若干标签:

 

代码:

html:

<div class="optionrow">
                    <label class="optiontpye">建设年份:</label>
                    <input class="optionitem" type="checkbox" name="year" checked="checked" id="allyear"/>
                    <label class="optionlabel" for="allyear">全部</label>
                    <input class="optionitem" type="checkbox" name="year" id="y2019"/>
                    <label class="optionlabel" for="y2019">2019年</label>
                    <input class="optionitem" type="checkbox" name="year"  id="y2018"/>
                    <label class="optionlabel" for="y2018">2018年</label>
                    <input class="optionitem" type="checkbox" name="year"  id="y2017"/>
                    <label class="optionlabel" for="y2017">2017年</label>
</div>

label写在对应input的相邻后面

css:

将input设置为不可见display:none,仅对label设置样式

.optionitem[type='checkbox'] {
    display: none;
}

.optionlabel{
    cursor: pointer;
    border: 1px solid #5a6268;
    border-radius: 8px;
    padding: 2px 10px;
}
.optionlabel:hover, .optionitem:checked + .optionlabel{
    color: #00ffff;
    border: 1px solid #00ffff;
}

当标签选中时,改变相邻的label样式,即使用css中的+选择器(相邻兄弟选择器,参考:http://www.w3school.com.cn/css/css_selector_adjacent_sibling.asp

 

第二种

效果:

代码:

css:

.optionitem + label::before {
    height: 13px;
    width: 13px;
    background: url(../images/newImages/projectlist/选框未选中.png) no-repeat center;
    content: "\a0\a0\a0\a0\a0";
}
.optionitem[type="checkbox"]:checked + label::before {
    background: url(../images/newImages/projectlist/选框选中.png) no-repeat center;
}

使用伪元素label::before,用图片代替默认框

注意:需要设置content,否则显示不出来,"\a0"是空格

此方法参考:https://www.cnblogs.com/xinjie-just/p/7302020.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值