复选框样式在Safari移动版上无法生效。

9

我想将复选框样式设置为以下样子:

enter image description here

这是我的CSS

.checkboxcontact input[type="checkbox"] {
    -webkit-appearance: none;
    background-color: white;
    border: 1px solid #d44803;
    padding: 9px;
    border-radius: 3px;
    display: inline-block;
    position: relative;
    float:left;
}

.checkboxcontact input[type="checkbox"]:checked {
    background-color: #d44803;
    border: 1px solid white;
    color: #fff;
}

.checkboxcontact input[type="checkbox"]:checked:after {
    content: '\2714';
    font-size: 14px;
    position: absolute;
    top: 0;
    left: 3px;
    color: #fff;
    font-family: "FontAwesome";
}

这在桌面上和Chrome移动端上表现完美。

enter image description here

但是问题出现在iPhone的Safari上,显示如下:

enter image description here

我该如何解决这个问题?是否有备选方案或其他解决方法?


1
复选框(例如类似单选按钮)没有内容。因此,向它们添加伪元素(如after)是不正确的,并且会导致跨浏览器出现这种行为。 - Mihai T
@MihaiT, 有没有一个好的方法来实现这个?(获得相同的结果) - nielsv
3个回答

10
正如上面所建议的那样,伪元素与输入框不兼容,然而,将复选框置于标签内是一个好主意——它符合w3c规范,并且可以按预期工作,您也不必为标签使用for标记,也不必为输入复选框使用id标记。

以下是HTML代码:

<label class="custom-checkbox">
  <input type="checkbox" value="checkbox1">
  <span>Checkbox</span>
</label>

这段代码非常灵活,如果您只需要复选框而不需要标签,可以将span标签留空 - 但必须保留该标签 - 或者您可以创建自己的自定义类来应用伪元素在标签本身上。

以下是CSS代码:

.custom-checkbox {
  position: relative;
  display: block;
  margin-top: 10px;
  margin-bottom: 10px;
  line-height: 20px;
}

.custom-checkbox span {
  display: block;
  margin-left: 20px;
  padding-left: 7px;
  line-height: 20px;
  text-align: left;
}

.custom-checkbox span::before {
  content: "";
  display: block;
  position: absolute;
  width: 20px;
  height: 20px;
  top: 0;
  left: 0;
  background: #fdfdfd;
  border: 1px solid #e4e5e7;
  @include vendorize(box-shadow, inset 2px 2px 0px 0px rgba(0, 0, 0, 0.1));
}

.custom-checkbox span::after {
  display: block;
  position: absolute;
  width: 20px;
  height: 20px;
  top: 0;
  left: 0;
  font-size: 18px;
  color: #0087b7;
  line-height: 20px;
  text-align: center;
}

.custom-checkbox input[type="checkbox"] {
  opacity: 0;
  z-index: -1;
  position: absolute;
}

.custom-checkbox input[type="checkbox"]:checked + span::after {
  font-family: "FontAwesome";
  content: "\f00c";
  background:#d44803;
  color:#fff;
}

以下是一个可用的演示代码:

https://jsfiddle.net/ee1uhb3g/

已验证在所有浏览器上均可使用,包括FF,Chrome,Safari,IE等。


1
在我们的情况下,是 line-height: 20px; 解决了这个问题。 - CedricSoubrie
1
@CedricSoubrie 如果这篇文章对你有所帮助,我很高兴。 - scooterlord

2

:after:before伪元素在与输入类型元素一起使用时无法正常工作。因此,向它们添加类似于after的伪元素是不正确的。因此,应该在复选框旁边添加标签,并为其添加样式。

.checkboxcontact label {
    display: inline-block;
    position: relative;
    vertical-align: middle;
    padding-left: 5px;
}
.checkboxcontact input[type="checkbox"] {
    opacity: 0;
}
.checkboxcontact label::before {
    content: "";
    display: inline-block;
    position: absolute;
    width: 17px;
    height: 17px;
    left: 0;
    margin-left: -20px;
    border: 1px solid #d44803;
    border-radius: 3px;
    background-color: #fff;    
}

.checkboxcontact input[type="checkbox"]:checked + label::before,
.checkboxcontact input[type="checkbox"]:focus + label::before {
    background-color: #d44803;
    border: 1px solid white;
}

.checkboxcontact input[type="checkbox"]:checked + label::after {
    content: '\f00c';
    font-size: 14px;
    position: absolute;
    top: 2px;
    left: -17px;
    color: #fff;
    font-family: "FontAwesome";
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="checkboxcontact">
  <input type="checkbox" id="check1"/>
  <label for="check1">
      Check me out
  </label>
</div>


1
我曾经也遇到过同样的问题。建议您根据设计使用选中和未选中的图像。当用户选中时,图像src应更改为选中的图像。您可以使用javascript函数来实现。确保您使用相同大小的图像,这样用户就不会感觉这是2个不同的图像。供您参考,我附上了我使用过的这些图片。 选中图像 未选中图像 以下是我的代码。我在我的样式表中提供了这些图像,并在我的javascript中呈现了它们。当用户单击选中的图像时,我将删除“selected”类。因此,未选中的图像将显示给用户。

function ClearSelection() {
    $(".filterlist ul li").each(function () {
        $(this).removeClass("selected");
    });
}
.filterlist ul li
{
 padding:5px;
 border-bottom:1px solid gray;
 cursor:pointer; 
 background-image: url("../images/untick.png");
 background-repeat:no-repeat;
 background-position: 10 8;
}
.filterlist ul li.selected{background-image: url("../images/tick.png");}


你有JavaScript的示例可以做到这一点吗? - nielsv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接