是否可以跨浏览器使用CSS或HTML设置复选框的大小?
width和size在IE6+中有效,但在Firefox中无效,在Firefox中,即使我设置较小的大小,复选框仍然保持16x16。
是否可以跨浏览器使用CSS或HTML设置复选框的大小?
width和size在IE6+中有效,但在Firefox中无效,在Firefox中,即使我设置较小的大小,复选框仍然保持16x16。
当前回答
我认为最简单的解决方案是像一些用户建议的那样重新设置复选框的样式。下面的CSS为我工作,只需要几行CSS,并回答OP问题:
input[type=checkbox] {
-webkit-appearance: none;
-moz-appearance: none;
vertical-align: middle;
width: 14px;
height: 14px;
font-size: 14px;
background-color: #eee;
}
input[type=checkbox]:checked:after {
position: relative;
bottom: 3px;
left: 1px;
color: blue;
content: "\2713"; /* check mark */
}
正如这篇文章中提到的,缩放属性似乎在Firefox上不起作用,并且转换可能会导致不希望看到的效果。
在Chrome和Firefox上测试,应该适用于所有现代浏览器。只需根据需要更改属性(颜色、大小、底部、左侧等)。希望能有所帮助!
其他回答
预览: http://jsfiddle.net/h4qka9td/
*,*:after,*:before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; padding: 0; margin: 0; } .switch { margin: 50px auto; position: relative; } .switch label { width: 100%; height: 100%; position: relative; display: block; } .switch input { top: 0; right: 0; bottom: 0; left: 0; opacity: 0; z-index: 100; position: absolute; width: 100%; height: 100%; cursor: pointer; } /* DEMO 3 */ .switch.demo3 { width: 180px; height: 50px; } .switch.demo3 label { display: block; width: 100%; height: 100%; background: #a5a39d; border-radius: 40px; box-shadow: inset 0 3px 8px 1px rgba(0,0,0,0.2), 0 1px 0 rgba(255,255,255,0.5); } .switch.demo3 label:after { content: ""; position: absolute; z-index: -1; top: -8px; right: -8px; bottom: -8px; left: -8px; border-radius: inherit; background: #ababab; background: -moz-linear-gradient(#f2f2f2, #ababab); background: -ms-linear-gradient(#f2f2f2, #ababab); background: -o-linear-gradient(#f2f2f2, #ababab); background: -webkit-gradient(linear, 0 0, 0 100%, from(#f2f2f2), to(#ababab)); background: -webkit-linear-gradient(#f2f2f2, #ababab); background: linear-gradient(#f2f2f2, #ababab); box-shadow: 0 0 10px rgba(0,0,0,0.3), 0 1px 1px rgba(0,0,0,0.25); } .switch.demo3 label:before { content: ""; position: absolute; z-index: -1; top: -18px; right: -18px; bottom: -18px; left: -18px; border-radius: inherit; background: #eee; background: -moz-linear-gradient(#e5e7e6, #eee); background: -ms-linear-gradient(#e5e7e6, #eee); background: -o-linear-gradient(#e5e7e6, #eee); background: -webkit-gradient(linear, 0 0, 0 100%, from(#e5e7e6), to(#eee)); background: -webkit-linear-gradient(#e5e7e6, #eee); background: linear-gradient(#e5e7e6, #eee); box-shadow: 0 1px 0 rgba(255,255,255,0.5); -webkit-filter: blur(1px); -moz-filter: blur(1px); -ms-filter: blur(1px); -o-filter: blur(1px); filter: blur(1px); } .switch.demo3 label i { display: block; height: 100%; width: 60%; border-radius: inherit; background: silver; position: absolute; z-index: 2; right: 40%; top: 0; background: #b2ac9e; background: -moz-linear-gradient(#f7f2f6, #b2ac9e); background: -ms-linear-gradient(#f7f2f6, #b2ac9e); background: -o-linear-gradient(#f7f2f6, #b2ac9e); background: -webkit-gradient(linear, 0 0, 0 100%, from(#f7f2f6), to(#b2ac9e)); background: -webkit-linear-gradient(#f7f2f6, #b2ac9e); background: linear-gradient(#f7f2f6, #b2ac9e); box-shadow: inset 0 1px 0 white, 0 0 8px rgba(0,0,0,0.3), 0 5px 5px rgba(0,0,0,0.2); } .switch.demo3 label i:after { content: ""; position: absolute; left: 15%; top: 25%; width: 70%; height: 50%; background: #d2cbc3; background: -moz-linear-gradient(#cbc7bc, #d2cbc3); background: -ms-linear-gradient(#cbc7bc, #d2cbc3); background: -o-linear-gradient(#cbc7bc, #d2cbc3); background: -webkit-gradient(linear, 0 0, 0 100%, from(#cbc7bc), to(#d2cbc3)); background: -webkit-linear-gradient(#cbc7bc, #d2cbc3); background: linear-gradient(#cbc7bc, #d2cbc3); border-radius: inherit; } .switch.demo3 label i:before { content: "off"; text-transform: uppercase; font-style: normal; font-weight: bold; color: rgba(0,0,0,0.4); text-shadow: 0 1px 0 #bcb8ae, 0 -1px 0 #97958e; font-family: Helvetica, Arial, sans-serif; font-size: 24px; position: absolute; top: 50%; margin-top: -12px; right: -50%; } .switch.demo3 input:checked ~ label { background: #9abb82; } .switch.demo3 input:checked ~ label i { right: -1%; } .switch.demo3 input:checked ~ label i:before { content: "on"; right: 115%; color: #82a06a; text-shadow: 0 1px 0 #afcb9b, 0 -1px 0 #6b8659; } <div class="switch demo3"> <input type="checkbox"> <label><i></i> </label> </div> <div class="switch demo3"> <input type="checkbox" checked> <label><i></i> </label> </div>
我发现这个css库非常有用: https://lokesh-coder.github.io/pretty-checkbox/
或者,你也可以像@Sharcoux发的帖子那样,用同样的基本概念自己动手。它基本上是:
隐藏正常的复选框(不透明度为0,放在它应该去的地方) 添加一个基于css的假复选框 对选中的样式使用input:checked~div标签 确保你的<label>是可点击的使用for=yourinputID
.pretty { position: relative; margin: 1em; } .pretty input { position: absolute; left: 0; top: 0; min-width: 1em; width: 100%; height: 100%; z-index: 2; opacity: 0; margin: 0; padding: 0; cursor: pointer; } .pretty-inner { box-sizing: border-box; position: relative; } .pretty-inner label { position: initial; display: inline-block; font-weight: 400; margin: 0; text-indent: 1.5em; min-width: calc(1em + 2px); } .pretty-inner label:after, .pretty-inner label:before { content: ''; width: calc(1em + 2px); height: calc(1em + 2px); display: block; box-sizing: border-box; border-radius: 0; border: 1px solid transparent; z-index: 0; position: absolute; left: 0; top: 0; background-color: transparent; } .pretty-inner label:before { border-color: #bdc3c7; } .pretty input:checked~.pretty-inner label:after { background-color: #00bb82; width: calc(1em - 6px); height: calc(1em - 6px); top: 4px; left: 4px; } /* Add checkmark character style */ .pretty input:checked~.pretty-inner.checkmark:after { content: '\2713'; color: #fff; position: absolute; font-size: 0.65em; left: 6px; top: 3px; } body { font-size: 20px; font-family: sans-serif; } <div class="pretty"> <input type="checkbox" id="demo" name="demo"> <div class="pretty-inner"><label for="demo">I agree.</label></div> </div> <div class="pretty"> <input type="checkbox" id="demo" name="demo"> <div class="pretty-inner checkmark"><label for="demo">Please check the box.</label></div> </div>
其他答案显示了一个像素的复选框,而我想要一些漂亮的东西。 结果如下所示:
尽管这个版本更复杂,但我认为值得一试。
.checkbox-list__item { position: relative; padding: 10px 0; display: block; cursor: pointer; margin: 0 0 0 34px; border-bottom: 1px solid #b4bcc2; } .checkbox-list__item:last-of-type { border-bottom: 0; } .checkbox-list__check { width: 18px; height: 18px; border: 3px solid #b4bcc2; position: absolute; left: -34px; top: 50%; margin-top: -12px; transition: border .3s ease; border-radius: 5px; } .checkbox-list__check:before { position: absolute; display: block; width: 18px; height: 22px; top: -2px; left: 0px; padding-left: 2px; background-color: transparent; transition: background-color .3s ease; content: '\2713'; font-family: initial; font-size: 19px; color: white; } input[type="checkbox"]:checked ~ .checkbox-list__check { border-color: #5bc0de; } input[type="checkbox"]:checked ~ .checkbox-list__check:before { background-color: #5bc0de; } <label class="checkbox-list__item"> <input class="checkbox_buttons" type="checkbox" checked="checked" style="display: none;"> <div class="checkbox-list__check"></div> </label>
JSFiddle: https://jsfiddle.net/asbd4hpr/
使用这个CSS代码
input[type=checkbox]
{
/* Double-sized Checkboxes */
-ms-transform: scale(1.5); /* IE */
-moz-transform: scale(1.5); /* FF */
-webkit-transform: scale(1.5); /* Safari and Chrome */
-o-transform: scale(1.5); /* Opera */
transform: scale(1.5);
padding: 10px;
}
适用于所有现代浏览器的解决方案。
输入[= 1]{型 用金币:规模(1 . 5); 的 <标签><输入类型=“checkbox”>测试</标签>
兼容性:
即:10 + FF: 16 + 铬:36 + Safari: 9 + 歌剧:23 + iOS Safari: 9.2+ Chrome for Android: 51+
外观:
Chrome 58(2017年5月),Windows 10