我可以使用CSS更改复选框的大小吗?

HTML CSS

十三泡芙猿

2020-03-18

是否可以在所有浏览器中使用CSS或HTML设置复选框的大小?

widthsize可以在IE6 +中使用,但不适用于Firefox,即使我设置了较小的尺寸,该复选框也保持16x16的比例。

第2186篇《我可以使用CSS更改复选框的大小吗?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

13个回答
Green阿飞泡芙 2020.03.18

您可以在下面的代码中更改高度和宽度

.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 20px;
    width: 20px;
    border-radius:5px;
    border:1px solid #ff7e02;
}
<div class="check">

     <label class="container1">Architecture/Landscape
										  

    <input type="checkbox" checked="checked">
										  

    <span class="checkmark"></span>
								 

	</label>
</div>

伽罗路易Tom 2020.03.18

问题是Firefox不听宽度和高度。禁用它,您就可以使用了。

input[type=checkbox] {
    width: 25px;
    height: 25px;
    -moz-appearance: none;
}
<label><input type="checkbox"> Test</label>

飞云Tony 2020.03.18

我认为最简单的解决方案是按照某些用户的建议重新设置复选框的样式。下面的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 */
}

如本文所述,zoom属性似乎在Firefox上不起作用,并且转换可能会导致不良后果。

经过Chrome和Firefox的测试,适用于所有现代浏览器。只需更改属性(颜色,大小,底部,左侧等)即可。希望能帮助到你!

神奇凯 2020.03.18

我的理解是,跨浏览器一点都不容易。不必尝试操纵复选框控件,您可以始终使用图像,javascript和隐藏的输入字段来构建自己的实现。我假设这与niceforms相似(根据上面的Staicu lonut的回答),但是实现起来并不是特别困难。我相信jQuery还有一个插件也允许这种自定义行为(如果我可以找到的话,会在此处查找链接并发布)。

Tony小卤蛋 2020.03.18

默认情况下,复选框的外观似乎是固定的。但是正如Worthy7指出的那样,可以使用CSS appearance属性来解决它将使复选框完全为空,因此您可以定义自己的外观。这样做的好处是:您可以使用现有的HTML代码。缺点:是实验技术Edge和IE不使用自定义样式。

以下是所需的CSS样式:

input[type=checkbox] {
    width: 14mm;
    -webkit-appearance: none;
    -moz-appearance: none;
    height: 14mm;
    border: 0.1mm solid black;
}

input[type=checkbox]:checked {
    background-color: lightblue;
}

input[type=checkbox]:checked:after {
    margin-left: 4.3mm;
    margin-top: -0.4mm;
    width: 3mm;
    height: 10mm;
    border: solid white;
    border-width: 0 2mm 2mm 0;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    content: "";
    display: inline-block;
}
<label><input type="checkbox"> Test</label>

屏幕截图:

铬: 在此处输入图片说明

Firefox: 在此处输入图片说明

边缘: 在此处输入图片说明

IE浏览器: 在此处输入图片说明

逆天LEY逆天 2020.03.18

我一直在寻找一个稍微大一点的复选框,然后查看37Signals Basecamp的源代码以找到以下解决方案-

您可以更改字体大小以使复选框略大:

font-size: x-large;

然后,您可以通过以下操作正确对齐复选框:

vertical-align: top;
margin-top: 3px; /* change to center it */
凯理查德 2020.03.18

您可以使用以下属性在Safari中增加复选框(通常可以抵制通常的做法): -webkit-transform: scale(1.3, 1.3);

资源

阿飞古一A 2020.03.18

预览: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>

宝儿猿斯丁 2020.03.18

我刚想到这个:

input[type="checkbox"] {display:none;}
input[type="checkbox"] + label:before {content:"☐";}
input:checked + label:before {content:"☑";}
label:hover {color:blue;}
<input id="check" type="checkbox" /><label for="check">Checkbox</label>

当然,由于这个原因,您可以根据content需要更改的值,并根据需要使用图像或使用其他字体...

这里的主要兴趣是:

  1. 复选框大小与文本大小成比例

  2. 您可以控制纵横比,颜色,复选框的大小

  3. 无需额外的HTML!

  4. 只需要3行CSS(最后一行只是为了给您提供想法)

编辑:如评论中指出的那样,按键导航将无法访问该复选框。您可能应该tabindex=0为标签添加属性,以使其具有焦点。

十三Davaid 2020.03.18

2017版本https : //jsfiddle.net/ksvx2txb/11/

关于:如果支持,浏览器将使用css缩放功能;如果不支持,则将使用transform:scale。

为什么只是不使用变换?在某些浏览器上看起来很难看,包括Mac上的chrome。

铬-确定
歌剧-确定
Edge-确定

Firefox-大但模糊
Safari-大但模糊

@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>

番长Jim 2020.03.18

一个简单的解决方案是使用property zoom

input[type="checkbox"] {
    zoom: 1.5;
}
<input type="checkbox" />

阿飞小哥 2020.03.18

这有点丑陋(由于放大),但是它可以在大多数较新的浏览器上使用:

input[type=checkbox]
{
  /* Double-sized Checkboxes */
  -ms-transform: scale(2); /* IE */
  -moz-transform: scale(2); /* FF */
  -webkit-transform: scale(2); /* Safari and Chrome */
  -o-transform: scale(2); /* Opera */
  transform: scale(2);
  padding: 10px;
}

/* Might want to wrap a span around your checkbox text */
.checkboxtext
{
  /* Checkbox text */
  font-size: 110%;
  display: inline;
}
<input  type="checkbox" name="optiona" id="opta" checked />
<span class="checkboxtext">
  Option A
</span>
<input type="checkbox" name="optionb" id="optb" />
<span class="checkboxtext">
  Option B
</span>
<input type="checkbox" name="optionc" id="optc" />
<span class="checkboxtext">
  Option C
</span>

JinJin小卤蛋 2020.03.18

Working solution for all modern browsers.

input[type=checkbox] {
    transform: scale(1.5);
}
<label><input type="checkbox"> Test</label>


Compatibility:

  • IE: 10+
  • FF: 16+
  • Chrome: 36+
  • Safari: 9+
  • Opera: 23+
  • iOS Safari: 9.2+
  • Chrome for Android: 51+

Appearance:

  • Chrome,Win 10上的缩放复选框 Chrome 58 (May 2017), Windows 10

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android