仅使用 CSS 和 HTML 的自定义复选框 [英] Custom checkbox using only CSS and HTML

查看:27
本文介绍了仅使用 CSS 和 HTML 的自定义复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要仅使用 html 和 CSS 创建一个自定义复选框.到目前为止,我有:

HTML/CSS:

.checkbox-custom {不透明度:0;位置:绝对;}.checkbox-自定义,.checkbox-custom-label {显示:内联块;垂直对齐:中间;边距:5px;光标:指针;}.checkbox-custom + .checkbox-custom-label:before {内容: '';背景:#fff;边框半径:5px;边框:2px 实心#ddd;显示:内联块;垂直对齐:中间;宽度:10px;高度:10px;填充:2px;右边距:10px;文本对齐:居中;}.checkbox-custom:checked + .checkbox-custom-label:before {内容: "";显示:内联块;宽度:1px;高度:5px;边框:纯蓝色;边框宽度:0 3px 3px 0;变换:旋转(45度);-webkit-transform:旋转(45度);-ms-transform:旋转(45度);边界半径:0px;边距:0px 15px 5px 5px;}

<input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox"><label for="checkbox-1" class="checkbox-custom-label">首选</label>

<div><input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox"><label for="checkbox-2" class="checkbox-custom-label">第二选择</label>

checked 复选框应该是一个带有方框的复选标记,而不仅仅是一个复选标记.在寻找答案时,我只发现了使用 UTF-8 字符或图像显示复选标记的情况.我无法将其中任何一个用于我想要完成的工作.我只能使用纯 CSS 和 HTML.有什么建议吗?

codepen 在这里:http://codepen.io/alisontague/pen/EPXagW?editors=110

解决方案

问题是您对方形边框和复选标记使用了相同的伪元素.简单的解决方案是继续使用 :before 伪元素作为边框,然后使用 :after 伪元素作为复选标记.

更新示例

您必须绝对定位 :after 伪元素 相对 到父 .checkbox-custom 标签元素.

这是更新的代码:

.checkbox-custom {显示:无;}.checkbox-custom-label {显示:内联块;位置:相对;垂直对齐:中间;边距:5px;光标:指针;}.checkbox-custom + .checkbox-custom-label:before {内容: '';背景:#fff;边框半径:5px;边框:2px 实心#ddd;显示:内联块;垂直对齐:中间;宽度:10px;高度:10px;填充:2px;右边距:10px;}.checkbox-custom:checked + .checkbox-custom-label:after {内容: "";填充:2px;位置:绝对;宽度:1px;高度:5px;边框:纯蓝色;边框宽度:0 3px 3px 0;变换:旋转(45度);顶部:2px;左:5px;}

<h3>复选框</h3><div><input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox"><label for="checkbox-1" class="checkbox-custom-label">首选</label>

<div><input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox"><label for="checkbox-2" class="checkbox-custom-label">第二选择</label>

I need to create a custom checkbox using only html and CSS. So far I have:

HTML/CSS:

.checkbox-custom {
  opacity: 0;
  position: absolute;
}
.checkbox-custom,
.checkbox-custom-label {
  display: inline-block;
  vertical-align: middle;
  margin: 5px;
  cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
  content: '';
  background: #fff;
  border-radius: 5px;
  border: 2px solid #ddd;
  display: inline-block;
  vertical-align: middle;
  width: 10px;
  height: 10px;
  padding: 2px;
  margin-right: 10px;
  text-align: center;
}
.checkbox-custom:checked + .checkbox-custom-label:before {
  content: "";
  display: inline-block;
  width: 1px;
  height: 5px;
  border: solid blue;
  border-width: 0 3px 3px 0;
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  border-radius: 0px;
  margin: 0px 15px 5px 5px;
}

<div>
  <input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
  <label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
  <input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
  <label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>

The checked checkbox should be a checkmark with the square bordered around it instead of just a checkmark. Searching for answers, I have only found cases where the checkmark is displayed using a UTF-8 character or an image. I am not able to use either of these for what I am trying to accomplish. I am only able to use plain CSS and HTML. Any suggestions?

codepen here: http://codepen.io/alisontague/pen/EPXagW?editors=110

解决方案

The problem is that you are using the same pseudo element for the square border and the checkmark. The simple solution would be to continue using the :before pseudo element for the border, and then use an :after pseudo element for the checkmark.

Updated Example

You would have to absolutely position the :after pseudo element relative to the parent .checkbox-custom label element.

Here is the updated code:

.checkbox-custom {
  display: none;
}
.checkbox-custom-label {
  display: inline-block;
  position: relative;
  vertical-align: middle;
  margin: 5px;
  cursor: pointer;
}
.checkbox-custom + .checkbox-custom-label:before {
  content: '';
  background: #fff;
  border-radius: 5px;
  border: 2px solid #ddd;
  display: inline-block;
  vertical-align: middle;
  width: 10px; height: 10px;
  padding: 2px; margin-right: 10px;
}
.checkbox-custom:checked + .checkbox-custom-label:after {
  content: "";
  padding: 2px;
  position: absolute;
  width: 1px;
  height: 5px;
  border: solid blue;
  border-width: 0 3px 3px 0;
  transform: rotate(45deg);
  top: 2px; left: 5px;
}

<h3>Checkboxes</h3>
<div>
  <input id="checkbox-1" class="checkbox-custom" name="checkbox-1" type="checkbox">
  <label for="checkbox-1" class="checkbox-custom-label">First Choice</label>
</div>
<div>
  <input id="checkbox-2" class="checkbox-custom" name="checkbox-2" type="checkbox">
  <label for="checkbox-2" class="checkbox-custom-label">Second Choice</label>
</div>

这篇关于仅使用 CSS 和 HTML 的自定义复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆