是否可以跨浏览器使用CSS或HTML设置复选框的大小?
width和size在IE6+中有效,但在Firefox中无效,在Firefox中,即使我设置较小的大小,复选框仍然保持16x16。
是否可以跨浏览器使用CSS或HTML设置复选框的大小?
width和size在IE6+中有效,但在Firefox中无效,在Firefox中,即使我设置较小的大小,复选框仍然保持16x16。
当前回答
2020版-使用伪元素,大小取决于字体大小。
默认复选框/单选是在屏幕外呈现的,但CSS创建的虚拟元素与默认元素非常相似。支持所有浏览器,无模糊。大小取决于字体大小。还支持键盘操作(空格,制表符)。
https://jsfiddle.net/ohf7nmzy/2/
body{ padding:0 20px; } .big{ font-size: 50px; } /* CSS below will force radio/checkbox size be same as font size */ label{ position: relative; line-height: 1.4; } /* radio */ input[type=radio]{ width: 1em; font-size: inherit; margin: 0; transform: translateX(-9999px); } input[type=radio] + label:before{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border:none; border-radius: 50%; background-color: #bbbbbb; } input[type=radio] + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 50%; transform: scale(0.8); } /*checked*/ input[type=radio]:checked + label:before{ position:absolute; content:''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; } input[type=radio]:checked + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 50%; transform: scale(0.3); } /*focused*/ input[type=radio]:focus + label:before{ border: 0.2em solid #8eb9fb; margin-top: -0.2em; margin-left: -0.2em; box-shadow: 0 0 0.3em #3b88fd; } /*checkbox/*/ input[type=checkbox]{ width: 1em; font-size: inherit; margin: 0; transform: translateX(-9999px); } input[type=checkbox] + label:before{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border:none; border-radius: 10%; background-color: #bbbbbb; } input[type=checkbox] + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 10%; transform: scale(0.8); } /*checked*/ input[type=checkbox]:checked + label:before{ position:absolute; content:''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; } input[type=checkbox]:checked + label:after{ position: absolute; content: "\2713"; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; border-radius: 10%; color: white; text-align: center; line-height: 1; } /*focused*/ input[type=checkbox]:focus + label:before{ border: 0.1em solid #8eb9fb; margin-top: -0.1em; margin-left: -0.1em; box-shadow: 0 0 0.2em #3b88fd; } <input type="checkbox" name="checkbox_1" id="ee" checked /> <label for="ee">Checkbox small</label> <br /> <input type="checkbox" name="checkbox_2" id="ff" /> <label for="ff">Checkbox small</label> <hr /> <div class="big"> <input type="checkbox" name="checkbox_3" id="gg" checked /> <label for="gg">Checkbox big</label> <br /> <input type="checkbox" name="checkbox_4" id="hh" /> <label for="hh">Checkbox big</label> </div> <hr /> <input type="radio" name="radio_1" id="aa" value="1" checked /> <label for="aa">Radio small</label> <br /> <input type="radio" name="radio_1" id="bb" value="2" /> <label for="bb">Radio small</label> <hr /> <div class="big"> <input type="radio" name="radio_2" id="cc" value="1" checked /> <label for="cc">Radio big</label> <br /> <input type="radio" name="radio_2" id="dd" value="2" /> <label for="dd">Radio big</label> </div>
2017版-使用缩放或缩放
浏览器将使用非标准缩放功能,如果它是支持的(不错的质量)或标准转换:缩放(模糊的Safari)作为后备。
https://jsfiddle.net/ksvx2txb/11/
@supports (zoom:2) { input[type="radio"], input[type=checkbox]{ zoom: 2; } } @supports not (zoom:2) { input[type="radio"], input[type=checkbox]{ transform: scale(2); margin: 15px; } } label{ /* fix vertical align issues */ display: inline-block; vertical-align: top; margin-top: 10px; } <input type="radio" name="aa" value="1" id="aa" checked /> <label for="aa">Radio 1</label> <br /> <input type="radio" name="aa" value="2" id="bb" /> <label for="bb">Radio 2</label> <br /><br /> <input type="checkbox" name="optiona" id="cc" checked /> <label for="cc">Checkbox 1</label> <br /> <input type="checkbox" name="optiona" id="dd" /> <label for="dd">Checkbox 1</label>
其他回答
我认为最简单的解决方案是像一些用户建议的那样重新设置复选框的样式。下面的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上测试,应该适用于所有现代浏览器。只需根据需要更改属性(颜色、大小、底部、左侧等)。希望能有所帮助!
问题是Firefox不听宽度和高度。把它拆了,你就可以走了。
输入(type =复选框){ 宽度:25 px; 高度:25 px; -moz-appearance:没有; } <label><input type="checkbox"> Test</label>
我想做一个复选框,只是稍大一点,并查看37Signals Basecamp的源代码,以找到以下解决方案-
您可以更改字体大小,使复选框略大:
font-size: x-large;
然后,你可以正确对齐复选框,这样做:
vertical-align: top;
margin-top: 3px; /* change to center it */
我想说的是:
输入(type = "复选框"]{显示:没有;} Input [type="checkbox"] + label:before {content:"㈠";} 输入:勾选+标签:前面{内容:“☑”;} 标签:悬停{颜色:蓝色;} <input id="check" type="checkbox" /><label for="check"> checkbox </label>
当然,多亏了这一点,您可以根据需要更改内容的值,如果您愿意,可以使用图像或使用另一种字体……
这里的主要兴趣是:
复选框大小与文本大小成比例 你可以控制方面,颜色,复选框的大小 不需要额外的HTML ! 只需要3行CSS(最后一行只是给你一些想法)
编辑: 正如评论中指出的,该复选框将无法通过键导航访问。您可能应该为标签添加tabindex=0属性,以使其可聚焦。
2020版-使用伪元素,大小取决于字体大小。
默认复选框/单选是在屏幕外呈现的,但CSS创建的虚拟元素与默认元素非常相似。支持所有浏览器,无模糊。大小取决于字体大小。还支持键盘操作(空格,制表符)。
https://jsfiddle.net/ohf7nmzy/2/
body{ padding:0 20px; } .big{ font-size: 50px; } /* CSS below will force radio/checkbox size be same as font size */ label{ position: relative; line-height: 1.4; } /* radio */ input[type=radio]{ width: 1em; font-size: inherit; margin: 0; transform: translateX(-9999px); } input[type=radio] + label:before{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border:none; border-radius: 50%; background-color: #bbbbbb; } input[type=radio] + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 50%; transform: scale(0.8); } /*checked*/ input[type=radio]:checked + label:before{ position:absolute; content:''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; } input[type=radio]:checked + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 50%; transform: scale(0.3); } /*focused*/ input[type=radio]:focus + label:before{ border: 0.2em solid #8eb9fb; margin-top: -0.2em; margin-left: -0.2em; box-shadow: 0 0 0.3em #3b88fd; } /*checkbox/*/ input[type=checkbox]{ width: 1em; font-size: inherit; margin: 0; transform: translateX(-9999px); } input[type=checkbox] + label:before{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border:none; border-radius: 10%; background-color: #bbbbbb; } input[type=checkbox] + label:after{ position: absolute; content: ''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: white; border-radius: 10%; transform: scale(0.8); } /*checked*/ input[type=checkbox]:checked + label:before{ position:absolute; content:''; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; } input[type=checkbox]:checked + label:after{ position: absolute; content: "\2713"; left: -1.3em; top: 0; width: 1em; height: 1em; margin: 0; border: none; background-color: #3b88fd; border-radius: 10%; color: white; text-align: center; line-height: 1; } /*focused*/ input[type=checkbox]:focus + label:before{ border: 0.1em solid #8eb9fb; margin-top: -0.1em; margin-left: -0.1em; box-shadow: 0 0 0.2em #3b88fd; } <input type="checkbox" name="checkbox_1" id="ee" checked /> <label for="ee">Checkbox small</label> <br /> <input type="checkbox" name="checkbox_2" id="ff" /> <label for="ff">Checkbox small</label> <hr /> <div class="big"> <input type="checkbox" name="checkbox_3" id="gg" checked /> <label for="gg">Checkbox big</label> <br /> <input type="checkbox" name="checkbox_4" id="hh" /> <label for="hh">Checkbox big</label> </div> <hr /> <input type="radio" name="radio_1" id="aa" value="1" checked /> <label for="aa">Radio small</label> <br /> <input type="radio" name="radio_1" id="bb" value="2" /> <label for="bb">Radio small</label> <hr /> <div class="big"> <input type="radio" name="radio_2" id="cc" value="1" checked /> <label for="cc">Radio big</label> <br /> <input type="radio" name="radio_2" id="dd" value="2" /> <label for="dd">Radio big</label> </div>
2017版-使用缩放或缩放
浏览器将使用非标准缩放功能,如果它是支持的(不错的质量)或标准转换:缩放(模糊的Safari)作为后备。
https://jsfiddle.net/ksvx2txb/11/
@supports (zoom:2) { input[type="radio"], input[type=checkbox]{ zoom: 2; } } @supports not (zoom:2) { input[type="radio"], input[type=checkbox]{ transform: scale(2); margin: 15px; } } label{ /* fix vertical align issues */ display: inline-block; vertical-align: top; margin-top: 10px; } <input type="radio" name="aa" value="1" id="aa" checked /> <label for="aa">Radio 1</label> <br /> <input type="radio" name="aa" value="2" id="bb" /> <label for="bb">Radio 2</label> <br /><br /> <input type="checkbox" name="optiona" id="cc" checked /> <label for="cc">Checkbox 1</label> <br /> <input type="checkbox" name="optiona" id="dd" /> <label for="dd">Checkbox 1</label>