首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Html和CSS更改复选框UI?

如何使用Html和CSS更改复选框UI?
EN

Stack Overflow用户
提问于 2018-05-28 13:44:03
回答 1查看 1.4K关注 0票数 0
代码语言:javascript
复制
<input type='checkbox' name='chkbox'/>

如果可能的话,我想更改复选框的高度和宽度以及背景颜色

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 13:48:52

你可以看看这段初学者的代码。

它使用before元素创建自定义形状,并隐藏“选中”和“未选中”两个状态的默认复选框。

还要为相应的复选框添加标签,以便单击此自定义形状将实际触发隐藏的默认复选框。

代码语言:javascript
复制
body {
  font-family: 'segoe ui';
  background-color: white;
  padding: 10px;
}

.space {
  height: 10px;
}

.checkbox * {
  box-sizing: border-box;
  position: relative;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.checkbox {
  display: inline-block;
}

.checkbox>input {
  display: none;
}

.checkbox>label {
  vertical-align: top;
  font-size: 18px;
  padding-left: 30px;
}

.checkbox>[type="checkbox"]+label:before {
  color: #777;
  content: '';
  position: absolute;
  left: 0px;
  display: inline-block;
  min-height: 20px;
  height: 20px;
  width: 20px;
  border: 2px solid #777;
  font-size: 15px;
  vertical-align: top;
  text-align: center;
  transition: all 0.2s ease-in;
  content: '';
}

.checkbox.radio-square>[type="checkbox"]+label:before {
  border-radius: 0px;
}

.checkbox.radio-rounded>[type="checkbox"]+label:before {
  border-radius: 25%;
}

.checkbox.radio-blue>[type="checkbox"]+label:before {
  border: 2px solid #ccc;
}

.checkbox>[type="checkbox"]+label:hover:before {
  border-color: lightgreen;
}

.checkbox>[type="checkbox"]:checked+label:before {
  width: 8px;
  height: 16px;
  border-top: transparent;
  border-left: transparent;
  border-color: green;
  border-width: 4px;
  transform: rotate(45deg);
  top: -4px;
  left: 4px;
}
代码语言:javascript
复制
<div class="checkbox">
  <input id="chkTest" type="checkbox" />
  <label for="chkTest">Task one</label>
  <div class="space">

  </div>

  <input id="chkTest1" type="checkbox" />
  <label for="chkTest1">Task two</label>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50559807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档