【发布时间】:2011-05-07 10:33:28
【问题描述】:
在大多数浏览器中呈现的标准复选框非常小,即使使用较大的字体也不会增加大小。显示较大复选框的最佳、独立于浏览器的方式是什么?
【问题讨论】:
在大多数浏览器中呈现的标准复选框非常小,即使使用较大的字体也不会增加大小。显示较大复选框的最佳、独立于浏览器的方式是什么?
【问题讨论】:
如果这可以帮助任何人,这里有一个简单的 CSS 作为起点。将其变成一个基本的圆角正方形,其大小足以容纳带有切换背景颜色的拇指。
input[type='checkbox'] {
-webkit-appearance:none;
width:30px;
height:30px;
background:white;
border-radius:5px;
border:2px solid #555;
}
input[type='checkbox']:checked {
background: #abd;
}
<input type="checkbox" />
【讨论】:
实际上有一种方法可以让它们变得更大,复选框就像其他任何东西一样(甚至是像 facebook 按钮这样的 iframe)。
将它们包装在“缩放”元素中:
.double {
zoom: 2;
transform: scale(2);
-ms-transform: scale(2);
-webkit-transform: scale(2);
-o-transform: scale(2);
-moz-transform: scale(2);
transform-origin: 0 0;
-ms-transform-origin: 0 0;
-webkit-transform-origin: 0 0;
-o-transform-origin: 0 0;
-moz-transform-origin: 0 0;
}
<div class="double">
<input type="checkbox" name="hello" value="1">
</div>
它可能看起来有点“重新调整”但它有效。
当然你可以让那个 div 浮动:left 并把你的标签放在它旁边,float:left 也一样。
【讨论】:
zoom: 2和transform: scale(2)不能直接应用到复选框上吗?为什么需要包装器?
试试这个 CSS
input[type=checkbox] {width:100px; height:100px;}
【讨论】:
我尝试更改padding 和margin 以及width 和height,然后最后发现如果你只是增加规模就可以了:
input[type=checkbox] {
transform: scale(1.5);
}
【讨论】:
纯粹的现代 2020 仅 CSS 决定,没有模糊的缩放或不方便的转换。并带有滴答声! =)
在 Firefox 和基于 Chromium 的浏览器中运行良好。
因此,您可以完全自适应来统治您的复选框,只需设置父块的font-size,它就会随着文本而增长!
input[type='checkbox'] {
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
vertical-align: middle;
outline: none;
font-size: inherit;
cursor: pointer;
width: 1.0em;
height: 1.0em;
background: white;
border-radius: 0.25em;
border: 0.125em solid #555;
position: relative;
}
input[type='checkbox']:checked {
background: #adf;
}
input[type='checkbox']:checked:after {
content: "✔";
position: absolute;
font-size: 90%;
left: 0.0625em;
top: -0.25em;
}
<label for="check1"><input type="checkbox" id="check1" checked="checked" /> checkbox one</label>
<label for="check2"><input type="checkbox" id="check2" /> another checkbox</label>
<label for="check3" style="font-size:150%"><input type="checkbox" id="check3" checked="checked" /> bigger checkbox </label>
【讨论】:
这是一个适用于最新浏览器 (IE9+) 的技巧,它是一种纯 CSS 解决方案,可以使用 javascript 进行改进以支持 IE8 及更低版本。
<div>
<input type="checkbox" id="checkboxID" name="checkboxName" value="whatever" />
<label for="checkboxID"> </label>
</div>
为label 设置您希望复选框的样式
#checkboxID
{
position: absolute fixed;
margin-right: 2000px;
right: 100%;
}
#checkboxID + label
{
/* unchecked state */
}
#checkboxID:checked + label
{
/* checked state */
}
对于 javascript,您将能够向标签添加类以显示状态。此外,最好使用以下函数:
$('label[for]').live('click', function(e){
$('#' + $(this).attr('for') ).click();
return false;
});
编辑以修改#checkboxID 样式
【讨论】:
我正在编写一个 phonegap 应用程序,并且复选框的大小、外观等各不相同。 所以我自己做了一个简单的复选框:
首先是 HTML 代码:
<span role="checkbox"/>
然后是 CSS:
[role=checkbox]{
background-image: url(../img/checkbox_nc.png);
height: 15px;
width: 15px;
display: inline-block;
margin: 0 5px 0 5px;
cursor: pointer;
}
.checked[role=checkbox]{
background-image: url(../img/checkbox_c.png);
}
为了切换复选框状态,我使用了 JQuery:
CLICKEVENT='touchend';
function createCheckboxes()
{
$('[role=checkbox]').bind(CLICKEVENT,function()
{
$(this).toggleClass('checked');
});
}
但是没有它也可以轻松完成...
希望对你有帮助!
【讨论】: