如何在CSS中更改复选框的边框样式?

Ric*_*cky 112 html css

如何更改复选框(输入)边框的样式?我戴上border:1px solid #1e5180它,但在FireFox 3.5中,没有任何反应!

小智 199

我建议使用"outline"而不是"border".例如:outline: 1px solid #1e5180.

  • @Yises你要担心IE7吗?哎呀,对不起. (13认同)
  • 它不与圆角半径复选框对齐 (5认同)
  • outline非常好,它适用于所有输入和选择框,这对".error"类很有用. (4认同)
  • 大纲是一个很好的选择. (3认同)
  • 使用大纲时,您可能需要在css中添加`!important`.我在Firefox 12.0中测试了这个并没有!重要的是,只要我点击复选框,轮廓就会消失. (2认同)
  • 这是一个很好的选择,但大纲的问题是在IE7它不起作用! (2认同)

Val*_*rij 68

你应该用

-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;
Run Code Online (Sandbox Code Playgroud)

然后你摆脱默认的复选框图像/样式,并可以设置它的样式.无论如何,Firefox中仍然存在边框

  • 哎呀 - 说得太快了:" - webkit-appearance:none;" 似乎禁用了复选框行为. (7认同)
  • 对我来说(铬5)它没有,你需要设置:检查状态额外 (6认同)
  • Opera可以让你在没有任何额外hackery的情况下设置复选框样式 - 我会放弃-o-appearance风格.-moz-appearance和-webkit-appearance很棒. (2认同)

小智 59

如果在任何浏览器中发生某些事情,我会感到惊讶.这是浏览器往往不会让你风格那么多的优秀表单元素之一,并且人们通常会尝试使用javascript替换它们,以便他们可以设置样式/代码来查看和操作像复选框一样.

这是一个例子:prettyCheckboxes.

  • 2023年还是丑:) (9认同)
  • 2021 年依然丑陋;( (8认同)
  • 2020年还是丑 (7认同)
  • 预计 2023 年以后会很丑。 (6认同)
  • **Opera**在复选框上处理样式非常好.**IE**有一些包围复选框的样式(不在复选框本身上). (4认同)
  • 现在它们实际上是丑陋的:D (4认同)

Den*_*den 18

您可以使用框阴影伪造边框:

-webkit-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
-moz-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
Run Code Online (Sandbox Code Playgroud)


小智 11

我已经过时了,我知道..但是有一点解决方法是将您的复选框放在标签标签内,然后用边框设置标签样式:

<label class='hasborder'><input type='checkbox' /></label>
Run Code Online (Sandbox Code Playgroud)

然后设置标签样式:

.hasborder { border:1px solid #F00; }
Run Code Online (Sandbox Code Playgroud)


Ada*_*tla 11

这是一个基于Martin的自定义复选框和带有CSS3 LINK的单选按钮的纯CSS(无图像)跨浏览器解决方案:http://martinivanov.net/2012/12/21/imageless-custom-checkboxes-and-radio-buttons -with-CSS3-重新/

这是一个jsFiddle:http://jsfiddle.net/DJRavine/od26wL6n/

我在以下浏览器上测试了这个:

  • FireFox(41.0.2)(42)
  • 谷歌浏览器(46.0.2490.80米)
  • 歌剧(33.0.1990.43)
  • Internet Explorer(11.0.10240.16431 [更新版本:11.0.22])
  • Microsoft Edge(20.10240.16384.0)
  • Safari Mobile iPhone iOS9(601.1.46)

label,
input[type="radio"] + span,
input[type="radio"] + span::before,
label,
input[type="checkbox"] + span,
input[type="checkbox"] + span::before
{
    display: inline-block;
    vertical-align: middle;
}
 
label *,
label *
{
    cursor: pointer;
}
 
input[type="radio"],
input[type="checkbox"]
{
    opacity: 0;
    position: absolute;
}
 
input[type="radio"] + span,
input[type="checkbox"] + span
{
    font: normal 11px/14px Arial, Sans-serif;
    color: #333;
}
 
label:hover span::before,
label:hover span::before
{
    -moz-box-shadow: 0 0 2px #ccc;
    -webkit-box-shadow: 0 0 2px #ccc;
    box-shadow: 0 0 2px #ccc;
}
 
label:hover span,
label:hover span
{
    color: #000;
}
 
input[type="radio"] + span::before,
input[type="checkbox"] + span::before
{
    content: "";
    width: 12px;
    height: 12px;
    margin: 0 4px 0 0;
    border: solid 1px #a8a8a8;
    line-height: 14px;
    text-align: center;
     
    -moz-border-radius: 100%;
    -webkit-border-radius: 100%;
    border-radius: 100%;
     
    background: #f6f6f6;
    background: -moz-radial-gradient(#f6f6f6, #dfdfdf);
    background: -webkit-radial-gradient(#f6f6f6, #dfdfdf);
    background: -ms-radial-gradient(#f6f6f6, #dfdfdf);
    background: -o-radial-gradient(#f6f6f6, #dfdfdf);
    background: radial-gradient(#f6f6f6, #dfdfdf);
}
 
input[type="radio"]:checked + span::before,
input[type="checkbox"]:checked + span::before
{
    color: #666;
}
 
input[type="radio"]:disabled + span,
input[type="checkbox"]:disabled + span
{
    cursor: default;
     
    -moz-opacity: .4;
    -webkit-opacity: .4;
    opacity: .4;
}
 
input[type="checkbox"] + span::before
{
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
}
 
input[type="radio"]:checked + span::before
{
    content: "\2022";
    font-size: 30px;
    margin-top: -1px;
}
 
input[type="checkbox"]:checked + span::before
{
    content: "\2714";
    font-size: 12px;
}



input[class="blue"] + span::before
{
    border: solid 1px blue;
    background: #B2DBFF;
    background: -moz-radial-gradient(#B2DBFF, #dfdfdf);
    background: -webkit-radial-gradient(#B2DBFF, #dfdfdf);
    background: -ms-radial-gradient(#B2DBFF, #dfdfdf);
    background: -o-radial-gradient(#B2DBFF, #dfdfdf);
    background: radial-gradient(#B2DBFF, #dfdfdf);
}
input[class="blue"]:checked + span::before
{
    color: darkblue;
}



input[class="red"] + span::before
{
    border: solid 1px red;
    background: #FF9593;
    background: -moz-radial-gradient(#FF9593, #dfdfdf);
    background: -webkit-radial-gradient(#FF9593, #dfdfdf);
    background: -ms-radial-gradient(#FF9593, #dfdfdf);
    background: -o-radial-gradient(#FF9593, #dfdfdf);
    background: radial-gradient(#FF9593, #dfdfdf);
}
input[class="red"]:checked + span::before
{
    color: darkred;
}
Run Code Online (Sandbox Code Playgroud)
 <label><input type="radio" checked="checked" name="radios-01" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-01" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-01" disabled="disabled" /><span>disabled radio button</span></label>

<br/>

 <label><input type="radio" checked="checked" name="radios-02"  class="blue" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-02" class="blue" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-02" disabled="disabled" class="blue" /><span>disabled radio button</span></label>

<br/>

 <label><input type="radio" checked="checked" name="radios-03"  class="red" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-03" class="red" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-03" disabled="disabled" class="red" /><span>disabled radio button</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" /><span>disabled checkbox</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" class="blue" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="blue" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="blue" /><span>disabled checkbox</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" class="red" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="red" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="red" /><span>disabled checkbox</span></label>
Run Code Online (Sandbox Code Playgroud)


awe*_*awe 9

对于Firefox,ChromeSafari,没有任何反应.

对于IE,边框应用于复选框之外(不作为复选框的一部分),复选框中的"奇特"着色效果消失(显示为旧式复选框).

对于Opera,边框样式实际上是在复选框元素上应用边框.
Opera还比其他浏览器更好地处理复选框上的其他样式:颜色应用于刻度线的颜色,背景颜色应用于复选框内的背景颜色(IE应用背景,就好像复选框位于<div>带背景的内部)) .

结论

最简单的解决方案是将复选框包装在<div>其他人建议的内容中.
如果你想完全控制外观,你必须采用先进的图像/ javascript方法,也是其他人的意思.

  • 用于研究的+1,但将其包含在div中是*不是解决方案.它不会让你设置复选框的样式,它可以让你围绕*复选框设置样式,除非你开始尝试隐藏它并在它上面绘制它会有自己的问题. (2认同)

pen*_*fox 9

这是我使用FontAwesome作为复选框自动收录器的版本,我认为几乎所有人都使用FontAwesome,因此可以安全地假设你也拥有它.没有在IE/Edge中测试,我认为没有人关心.

input[type=checkbox] {
	-moz-appearance:none;
	-webkit-appearance:none;
	-o-appearance:none;
	outline: none;
	content: none;	
}

input[type=checkbox]:before {
	font-family: "FontAwesome";
    content: "\f00c";
    font-size: 15px;
    color: transparent !important;
    background: #fef2e0;
    display: block;
    width: 15px;
    height: 15px;
    border: 1px solid black;
    margin-right: 7px;
}

input[type=checkbox]:checked:before {

	color: black !important;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>

<input type="checkbox">
Run Code Online (Sandbox Code Playgroud)

  • 对于那些不想使用 FontAwesome 的人,您可以删除 font-family 属性并将内容值替换为实际的复选标记字符 - 复制并粘贴此内容: ✓ 然后,您可以使用 font-size 和 line 自定义它:before 元素上的 -height 属性。 (7认同)

Chr*_*isR 7

如果您想要彻底改变视觉外观,那么使用纯CSS将无法实现样式复选框(以及该母版的许多其他输入元素).

你最好的办法是实现类似jqTransform的东西,它实际上用图像替换你的输入并对它应用javascript行为来模仿一个复选框(或其他元素)


LGT*_*LGT 5

不,您仍然不能为复选框本身设置样式,但是我(最后)想出了如何为幻觉设置样式,同时保留单击复选框的功能。这意味着即使光标不是完全静止也可以切换它,而不必冒险选择文本或触发拖放操作!

该示例使用了跨度“按钮”以及标签中的一些文本,但是它使您了解如何使复选框不可见并在其后面绘制任何内容。

此解决方案也可能适合单选按钮。

以下内容可在IE9,FF30.0和Chrome 40.0.2214.91中使用,并且只是一个基本示例。您仍然可以将其与背景图像和伪元素结合使用。

http://jsfiddle.net/o0xo13yL/1/

label {
    display: inline-block;
    position: relative; /* needed for checkbox absolute positioning */
    background-color: #eee;
    padding: .5rem;
    border: 1px solid #000;
    border-radius: .375rem;
    font-family: "Courier New";
    font-size: 1rem;
    line-height: 1rem;
}

label > input[type="checkbox"] {
    display: block;
    position: absolute; /* remove it from the flow */
    width: 100%;
    height: 100%;
    margin: -.5rem; /* negative the padding of label to cover the "button" */
    cursor: pointer;
    opacity: 0; /* make it transparent */
    z-index: 666; /* place it on top of everything else */
}

label > input[type="checkbox"] + span {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 1px solid #000;
    margin-right: .5rem;
}

label > input[type="checkbox"]:checked + span {
    background-color: #666;
}

<label>
    <input type="checkbox" />
    <span>&nbsp;</span>Label text
</label>
Run Code Online (Sandbox Code Playgroud)