thin-border: Retina屏幕下的1px解决方案

问题来源

众所周知,Retina屏幕下将 border 设置为1px , 其显示却比1px大,通常为2px。原因很简单,同样大小的手机一个为普通手机,一个为Retina手机,在css的世界中,其大小都为320px。而Retina屏幕的实际宽度却为640px。这就导致了css中的1px在Retina屏幕中实际为2px。这也是Retina屏幕显示效果比普通要更清晰,售价也更贵的原因所在。

解决方案

问题的原因清楚了,那我们就来谈谈具体的解决办法吧。

方案一: .5px

@media screen and (-webkit-min-device-pixel-ratio: 2) {
  border: .5px solid red;
}

该方案本应是最理想的方案,无奈Android与低版本的iOS(ios8才开始支持)不支持该方案。故,放弃!

方案二: border-image

@media screen and (-webkit-min-device-pixel-ratio: 2){ 
    .border{ 
        border: 1px solid transparent;
        border-image: url(border.gif) 2 repeat;
    }
}

缺点:处理圆角只能这么放大或缩小图片,麻烦

方案三:background渐变(FrozenUI采用该方式实现)

@media screen and (-webkit-min-device-pixel-ratio: 2){
    .ui-border-t {
        background-position: left top;
        background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0.5,transparent),color-stop(0.5,#fff),to(#fff));
    }
}

缺点:无法实现圆角、一个div上下左右只能实现一个方向、占用background属性

方案四::before、:after与transform结合 《极力推荐》

简单的实现代码如下:

.radius-border{
  position: relative;  /*该句不可少,因为要对:before 进行绝对定位*/
}
@media screen and (-webkit-min-device-pixel-ratio: 2){
    .radius-border:before{
        content: "";
  pointer-events: none; /* 防止点击触发 */
  box-sizing: border-box;
  position: absolute;
  width: 200%;
  height: 200%;
  left: 0;
  top: 0;
  border-radius: 8px;
  border:1px solid #999;
  -webkit-transform(scale(0.5));    /*核心:其实border还是1px,只是缩小一倍后可显示为.5px的效果;同时由于是对:before进行缩小,所以不会影响到div的大小及内容*/
  -webkit-transform-origin: 0 0;
  transform(scale(0.5));
  transform-origin: 0 0;
 }
}

优点:圆角、一个div上下左右可同时实现一个或多个(本人目前工作中使用该方式)
缺点:代码量在,且占用了伪元素

结论:最优方案-方案四-sass封装minxin

方案二、三、四可行,《如果考虑到 div大小不定问题》、《圆角问题》、《一个div多边同时实现问题》 这三个问题,毫无疑问,只能选择方案四。但是如果每次都要写一大堆的代码来实现 thin-border 效果。这对我们码农来说简直是一场灾难。
所以本人 用sass实现了 thin-border 系列的 minxin
用法如下:按需求将下面四个中的一个 class 赋给 div 即可,不用对div进行任何额外的处理 (此时该 div 的before元素将被占用)

.thin-border-left{      // 只让 left 边实现thin-border
    @include thin-border-left(red);
}
.thin-border-right{     // 只让 right 边实现thin-border
    @include thin-border-right(red);
}
.thin-border-top{       // 只让 top 边实现thin-border
    @include thin-border-top(red);
}
.thin-border-bottom{    // 只让 bottom 边实现thin-border
    @include thin-border-bottom(red);
}
.thin-border-all{   // 四个边同时实现thin-border, 其中 5px 为 border-radio 值
    @include thin-border-all(red, 5px);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 全局css变量 */ $--color-primary: #409EFF; .primary-color { color: #409EFF; } .background-opacity { background: rgba(64, 158, 255, 0.6); } .form-widget-list { .ghost{ content: ''; font-size: 0; height: 3px; box-sizing: border-box; background: #409EFF; border: 2px solid #409EFF; outline-width: 0; padding: 0; overflow: hidden; } } .el-form-item--medium { .el-radio { line-height: 36px !important; } .el-rate{ margin-top: 8px; } } .el-form-item--small { .el-radio { line-height: 32px !important; } .el-rate{ margin-top: 6px; } } .el-form-item--mini { .el-radio { line-height: 28px !important; } .el-rate{ margin-top: 4px; } } .el-card { margin-top: 3px; margin-bottom: 3px; } input[type="password"]::-ms-reveal { /* 隐藏IE/Edge原生的密码查看按钮 */ display: none; } /* 滚动条样式 begin */ ::-webkit-scrollbar { width: 8px; height: 8px; } ::-webkit-scrollbar-track { width: 8px; background: rgba(#101F1C, 0.1); -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb { background-color: rgba(#101F1C, 0.35); background-clip: padding-box; min-height: 28px; -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb:hover { background-color: rgba(#101F1C, 0.85); } * {//Firefox浏览器滚动条样式 scrollbar-color: #e5e5e5 #f7f7f9; //滚动条轨道颜色、滚动条滑块的颜色 scrollbar-width: thin; //thin模式下滚动条两端的三角按钮会消失 } /* body {//IE浏览器滚动条样式 scrollbar-shadow-color: #e5e5e5; scrollbar-face-color: #e5e5e5; scrollbar-base-color: #ffffff; scrollbar-arrow-color: #444040; } */ /* 滚动条样式 end */
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值