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

我怎样才能改变复选框(输入)边框的样式? 我已经把 border:1px solid #1e5180放在上面了,但是在 FireFox 3.5中,什么都没有发生!

399259 次浏览

把它放在一个 div 中,然后给 div 添加边框

如果在 任何浏览器中发生了什么事情,我会感到惊讶。这是那些优秀的表单元素之一,浏览器往往不会让你设计那么多,人们通常会尝试用 javascript 代替,这样他们就可以设计/编码一些东西,看起来像一个复选框。

如果您想彻底改变视觉外观,使用纯 CSS 是不可能实现样式化复选框(以及其他许多相关的输入元素)的。

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

对于 火狐铬合金旅行,什么都不会发生。

对于 IE,边框应用在复选框之外(而不是作为复选框的一部分) ,复选框中的“奇特”阴影效果消失了(显示为一个老式的复选框)。

对于 歌剧,边框样式实际上是在复选框元素上应用边框。
Opera 也比其他浏览器更好地处理复选框中的其他样式: 颜色被应用为勾选的颜色,背景色被应用为复选框内的背景颜色(IE 应用背景就好像复选框是在带背景的 <div>中一样)。

结论

最简单的解决方案是像其他人建议的那样将复选框封装在 <div>中。
如果你想完全控制外观,你必须使用高级的图像/javascript 方法,也就是其他方法。

<div style="border-style: solid;width:13px">
<input type="checkbox" name="mycheck" style="margin:0;padding:0;">
</input>
</div>

你应该用

-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;

然后去掉默认的复选框图像/样式,可以对其进行样式设置。无论如何,Firefox 中仍然会有一个边框

我建议使用“大纲”而不是“边框”。例如: outline: 1px solid #1e5180

我知道我已经过时了。.但是一个小小的变通方法是将复选框放入标签标签中,然后用边框给标签设置样式:

<label class='hasborder'><input type='checkbox' /></label>

然后为标签设计风格:

.hasborder { border:1px solid #F00; }

下面是一个简单的方法(在伪元素/类之前或之后使用) :

input[type=checkbox] {
position: relative;
}


input[type=checkbox]:after {
position: absolute;
top: 0;
left: 0;
/* Above three lines allow the checkbox:after position at checkbox's position */
content: '';
width: 32px;
height: 32px;
z-index: 1; /* This allows the after overlap the checkbox */
/* Anything you want */
}

不,你仍然不能设置复选框本身的样式,但是我(最后)弄明白了如何设置一个错觉,而 保持功能点击一个复选框。这意味着,即使光标不是完全静止的,您也可以切换它,而不必冒选择文本或触发拖放的风险!

这个示例使用 span“按钮”以及标签中的一些文本,但是它提供了如何使复选框不可见并在其后面绘制任何内容的想法。

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

下面的代码适用于 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>

这里是一个纯 CSS (无图像)跨浏览器解决方案的基础上,马丁的自定义复选框和单选按钮与 CSS3链接: http://martinivanov.net/2012/12/21/imageless-custom-checkboxes-and-radio-buttons-with-css3-revisited/

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

我已经在以下浏览器上测试过了:

  • FireFox (41.0.2)(42)
  • 谷歌 Chrome 浏览器(46.0.2490.80 m)
  • Opera (33.0.1990.43)
  • Internet Explorer (11.0.10240.16431[更新版本: 11.0.22])
  • Microsoft Edge (20.10240.16384.0)
  • Safari 移动 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;
}
 <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>

你可以使用方块阴影来伪造边框:

-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);

这是我的版本,使用 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;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>


<input type="checkbox">

<!DOCTYPE html>
<html>
<head>
<style>


.abc123
{
-webkit-appearance:none;
width: 14px;
height: 14px;
display: inline-block;
background: #FFFFFF;
border: 1px solid rgba(220,220,225,1);
}
.abc123:after {
content: "";
display: inline-block;
position: relative;
top: -3px;
left: 4px;
width: 3px;
height: 5px;
border-bottom: 1px solid #fff;
border-right: 1px solid #fff;
-webkit-transform: rotate(45deg);
}


input[type=checkbox]:checked   {
background: #327DFF;
outline: none;
border: 1px solid rgba(50,125,255,1);
}
input:focus,input:active {
outline: none;
}


input:hover {
border: 1px solid rgba(50,125,255,1);
}


</style>
</head>
<body>


<input class="abc123" type="checkbox"></input>




</body>
</html>

其实你只需要做两件事

outline: 1px solid #63DDCF
border: none !important;