HTML/CSS中的复选框大小

RaG*_*aGE 422 html css

是否可以使用CSS或HTML设置复选框的大小?

width并且size在IE6 +下工作,但不适用于Firefox,即使我设置较小的尺寸,复选框也保持16x16.

jdw*_*jdw 410

它有点难看(由于扩展),但它适用于大多数较新的浏览器:

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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 添加`transform:scale(2);`也是,以确保任何浏览器更多 (9认同)
  • 缩放看起来很糟糕,在规模2你已经看到了单个像素.我绝不会选择这个解决方案来获得漂亮的大css复选框. (7认同)
  • 这似乎是正确的答案,因为标记为答案的答案在许多情况下都没有提供解决方案(在xp上根本没有解决方案吗?根本没有镀铬?),已经过时并且只包含一个链接和评论没有太多价值. (3认同)
  • @jdw为这个答案+1,甚至我有必要这样做,你的答案是有帮助的.但不知怎的,我发现'scaled'复选框看起来有点扭曲.我不知道这是FF还是我的操作系统(Ubuntu 12.04).不管怎么说,还是要谢谢你 :) (2认同)

Num*_*ers 348

所有现代浏览器的工作解决方案

input[type=checkbox] {
    transform: scale(1.5);
}
Run Code Online (Sandbox Code Playgroud)

兼容性:

  • IE:10+
  • FF:16+
  • Chrome:36+
  • Safari:9+
  • 歌剧:23岁以上
  • iOS Safari:9.2+
  • 适用于Android的Chrome:51+

出现:

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

  • 第一次看的工作,但在Chrome 47中像素化 (13认同)
  • 使用macOS 10.13.3在Chrome 64上极其像素化:( https://www.timo-ernst.net/misc/zeug/cb-pixel.jpg (7认同)

小智 63

一个简单的解决方案是使用该属性zoom:

input[type="checkbox"] {
    zoom: 1.5;
}
Run Code Online (Sandbox Code Playgroud)

  • `zoom`属性在Firefox中不起作用,http://caniuse.com/#feat=css-zoom (12认同)
  • 不确定它是否适用于所有浏览器,但它在屏幕上看起来比缩放变换解决方案更漂亮. (8认同)
  • 有趣的是,Mozilla 不鼓励它,而 Firefox 几乎是唯一不支持它的浏览器:https://caniuse.com/#feat=css-zoom (4认同)
  • 在使用macOS 10.13.3的Chrome 64上,这看起来比使用transform + scale更不像素化 (2认同)
  • Mozilla 强烈劝阻。https://developer.mozilla.org/en-US/docs/Web/CSS/zoom (2认同)

Vil*_*usL 39

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

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

为什么不使用变换?它在某些浏览器上看起来很难看,包括mac上的chrome.

Chrome - 作品
Firefox(mac) - 作品
Opera - 作品
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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


小智 26

预览: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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 漂亮的复选框!但绝对没有回答原来的问题.. (39认同)

Sha*_*oux 23

我刚刚出来了:

input[type="checkbox"] {display:none;}
input[type="checkbox"] + label:before {content:"?";}
input:checked + label:before {content:"?";}
label:hover {color:blue;}
Run Code Online (Sandbox Code Playgroud)
<input id="check" type="checkbox" /><label for="check">Checkbox</label>
Run Code Online (Sandbox Code Playgroud)

当然,多亏了这一点,您可以根据content需要更改值,如果您愿意或使用其他字体,可以使用图像...

这里的主要兴趣是:

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

  2. 您可以控制方面,颜色和复选框的大小

  3. 无需额外的HTML!

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

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

  • 不幸的是,在 input 上设置 `display:none` 会阻止它被 `tab` 键选中,所以我认为这不是一个好主意。 (3认同)
  • 好话。您可以在输入上添加 tabindex=0 来解决这个问题。 (3认同)

小智 9

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

正如这篇文章中提到的,zoom 属性在 Firefox 上似乎不起作用,并且转换可能会导致不良影响。

在 Chrome 和 Firefox 上测试,应该适用于所有现代浏览器。只需根据您的需要更改属性(颜色、大小、底部、左侧等)。希望能帮助到你!


Man*_*nen 9

这应该有效

input {
  width: 25px;
  height: 25px;
}
Run Code Online (Sandbox Code Playgroud)

它至少对我的 Firefox 和 Chrome 以及 iPhone 的 Firefox、Chrome 和 Safari 有效。


Jus*_*tin 8

我想制作一个稍微大一点的复选框,查看37Signals Basecamp的源代码,找到以下解决方案 -

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

font-size: x-large;
Run Code Online (Sandbox Code Playgroud)

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

vertical-align: top;
margin-top: 3px; /* change to center it */
Run Code Online (Sandbox Code Playgroud)

  • font-size似乎根本不影响实际复选框的大小 (3认同)

Jac*_*ler 8

默认情况下,复选框的外观似乎是固定的。但是正如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;
}
Run Code Online (Sandbox Code Playgroud)
<label><input type="checkbox"> Test</label>
Run Code Online (Sandbox Code Playgroud)

屏幕截图:

铬: 在此处输入图片说明

Firefox: 在此处输入图片说明

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

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


Avi*_*lax 6

今天发现了这个有用的提示:你可以在Safari中使复选框更大 - 这通常可以抵抗通常的方法 - 使用以下属性:-webkit-transform: scale(1.3, 1.3);


小智 6

我的声誉有点太低,无法发表评论,但我对上面的 Jack Miller 的代码进行了修改,以便让它在选中和取消选中时不改变大小。这给我带来了文本对齐问题。

    input[type=checkbox] {
        width: 17px;
        -webkit-appearance: none;
        -moz-appearance: none;
        height: 17px;
        border: 1px solid black;
    }

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

    input[type=checkbox]:checked:after {
        margin-left: 4px;
        margin-top: -1px;
        width: 4px;
        height: 12px;
        border: solid white;
        border-width: 0 2px 2px 0;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        content: "";
        display: inline-block;
    }
    input[type=checkbox]:after {
        margin-left: 4px;
        margin-top: -1px;
        width: 4px;
        height: 12px;
        border: solid white;
        border-width: 0;
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        content: "";
        display: inline-block;
    }
Run Code Online (Sandbox Code Playgroud)
<label><input type="checkbox"> Test</label>
Run Code Online (Sandbox Code Playgroud)


loc*_*red 5

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


小智 5

使用这个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;
}
Run Code Online (Sandbox Code Playgroud)