input[type=checkbox] 使用hover设置label无效

场景:自定义一个checkbox的时候,发现input[type=checkbox] 使用伪类hover的时候无论怎么设置:hover均无法使用

解释:
在CSS中,:hover伪类用于选择鼠标指针悬停在上面的元素。对于input[type=checkbox]元素,浏览器默认样式通常是不允许直接修改其外观的,因此直接使用:hover可能不会产生预期效果。

解决方法:
要使input[type=checkbox]在hover时改变样式,可以通过隐藏的checkbox和label元素配合实现。

<input type="checkbox" id="checkbox" />
<label for="checkbox"></label>

重点: input必须加id,且label的for属性必须等于该id才能设置

/* 隐藏原生的checkbox */
input[type=checkbox] {
  display: none;
}

/* 定义label样式 */
label {
  display: inline-block;
  width: 20px;
  height: 20px;
  background-color: #fff;
  border: 1px solid #ccc;
  cursor: pointer;
}

/* 当鼠标悬停在label上时改变背景色 */
label:hover {
  background-color: #f0f0f0;
}

/* 当checkbox被选中时,label显示为选中状态 */
input[type=checkbox]:checked + label {
  background-color: blue;
  border-color: blue;
}
<form action="xxx.jsp" method="post" > <table width="500"> <style> body { display: grid; place-content: center; min-height: 100vh; margin: 20px; font-family: 'Microsoft YaHei', sans-serif; font-size: 16px; } table { border-spacing: 10px; } </style> <tr> <td><b style="font-size: 16px;">性别</b></td> <td> <input type="radio" id="man" name="sex"/> <label for="man"><img src="images/man.jpg"> 男 </label> <input type="radio" id="women" name="sex"/> <label for="women"><img src="images/women.jpg"> 女 </label> </td> </tr> <tr> <td><b>生日</b></td> <td> <select name="year"> <option selected="selected">--请选择年--</option> <option>1990</option> <option>2000</option> <option>2010</option> </select> <select name="month"> <option selected="selected">--请选择月--</option> <option>1</option> <option>2</option> <option>3</option> </select> <select name="day"> <option selected="selected">--请选择日--</option> <option>11</option> <option>12</option> <option>13</option> </select> </td> </tr> <tr> <td><b>所在地区</b></td> <td> <input type="text" value="安徽" name="area"> </td> </tr> <tr> <td><b>婚姻状况</b></td> <td> <input type="radio" name="marital_status" id="spinsterhood"> <label for="spinsterhood"> 未婚 </label> <input type="radio" name="marital_status" id="married"> <label for="married"> 已婚 </label> <input type="radio" name="marital_status" id="divorce"> <label for="divorce"> 离婚 </label> <input type="radio" name="marital_status" id="secret"> <label for="secret">保密</label> </td> </tr> <tr> <td><b>学历</b></td> <td><input type="text" name="edu_bg"></td> </tr> <tr> <td><b>喜欢的类型</b></td> <td> <input type="checkbox" name="like_type" id="wumei"> <label for="wumei"> 妩媚的 </label> <input type="checkbox" name="like_type" id="keai"> <label for="keai"> 可爱的 </label> <input type="checkbox" name="like_type" id="xiaoxianrou"> <label for="xiaoxianrou"> 小鲜肉 </label> <input type="checkbox" name="like_type" id="laolarou"> <label for="laolarou"> 老腊肉 </label> <input type="checkbox" name="like_type" id="douxihuan"> <label for="douxihuan"> 都喜欢 </label> </td> </tr> <tr> <td><b>自我介绍</b></td> <td> <textarea cols="30" rows="10" name="self_introduction"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align: center;"> <input type="submit" value=" 免费注册 "> </td> </tr> </table> </fieldset> </form>
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值