【问题标题】:Checkbox styling doesn't work on Safari mobile复选框样式在 Safari 移动设备上不起作用
【发布时间】:2018-05-04 18:10:27
【问题描述】:

我想为复选框设置如下样式:

这是我的 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 上显示完美。

但问题在于 iPhone 上的 Safari。它显示如下:

我该如何解决这个问题?有没有退路之类的?

【问题讨论】:

  • 复选框(例如单选按钮)没有内容。所以给它们添加像after这样的伪元素是不正确的,会导致这种跨浏览器的行为。
  • @MihaiT,有什么好的方法吗? (得到相同的结果)

标签: html css checkbox safari


【解决方案1】:

我也遇到过同样的问题。我建议您根据您的设计使用选中和取消选中的图像。当用户检查时,图像 src 应更改为检查图像。您可以为此使用 javascript 函数。确保您使用两个相同大小的图像。这样用户就不会感觉到这是 2 个不同的图像。供您参考,我附上了我用过的那些图片。

Check image

Uncheck image

这是我的代码。我为你找到的一些地方。看看它。已经在我的样式表中给出了这些图像并在我的 javascript 中呈现了这些图像。当用户单击选中的图像时,我正在删除“选定”类。因此,该取消选中的图像将显示给用户。

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的例子吗?
【解决方案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>

【讨论】:

    【解决方案3】:

    正如上面所建议的,伪元素不能很好地与输入一起工作,但是,将复选框 包装在标签内 是一个好主意 - 它是 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 等

    【讨论】:

    • 在我们的例子中是line-height: 20px; 解决了这个问题
    • @CedricSoubrie 很高兴这篇文章对您有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多