细线边框的具体实现方法有:伪元素缩放或渐变,box-shadow模拟,svg画线,border-image裁剪等。要实现小于1px的线条,有个先决条件:屏幕的分辨率要足够高,设备像素比要大于1,即css中的1个像素对应物理屏幕中1个以上的像素点。

  对于普通电脑,屏幕物理像素和css像素一一对应,显示的最小单位就是1px。而现在的手机,屏幕物理宽度一般都大于页面显示宽度。例如苹果6s的屏幕分辨率为1334x750像素,但是以375px的宽度显示页面,设备像素比就是750/375=2;此时在css中定义0.5px的宽度,实际上对应物理屏幕的1个像素点,这就是border小于1px的的实现基础。

 <!-- @media鉴别设备像素比 -->
<style>
@media only screen and (-webkit-min-device-pixel-ratio: 2){
.fineLine{
-webkit-transform: scaleY(.5);
}
}
</style> <!-- js获取设备像素比 -->
<script type="text/javascript">
var dpr = window.devicePixelRatio;
// 如下方法精确计算出了物理设备与css的像素比dppx。但实际使用中dpr更广泛,也足以满足一般需求
var dppx = window.screen.width/(window.innerWidth || document.documentElement.clientWidth);
</script>

一、缩放biefore/after伪元素

  伪元素进行绝对定位,background着色要优于border着色,适合画单线条:

 <div class="fineLine"></div>
<!-- fineLine的:before为上边框,:after为下边框 -->
<style type="text/css">
.fineLine {
position: relative;
}
.fineLine:before,.fineLine:after{
position: absolute;
content: " ";
height: 1px;
width: 100%;
left: 0;
transform-origin: 0 0;
-webkit-transform-origin: 0 0;
}
.fineLine:before {
top: 0;
background: #000;
}
.fineLine:after {
bottom: 0;
border-bottom: 1px solid #000;
}
@media only screen and (-webkit-min-device-pixel-ratio: 1.5) {
.fineLine:after,.fineLine:before {
-webkit-transform: scaleY(.667);
}
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
.fineLine:after,.fineLine:before {
-webkit-transform: scaleY(.5);
}
}
</style>

二、box-shadow模拟

  box-shaodw适合模拟box四周都需要细线border的情况,而且支持border-radius。此例中演示的是dppx鉴别:

 <div class="fineLine"></div>

 <style type="text/css">
.fineLine {
box-shadow: 0 0 0 1px;
}
@media (min-resolution: 2dppx) {
.fineLine {
box-shadow: 0 0 0 0.5px;
}
}
@media (min-resolution: 3dppx) {
.fineLine {
box-shadow: 0 0 0 0.33333333px;
}
}
</style>

最新文章

  1. CentOS6.5修改yum源
  2. android mk odex问题 push apk 不生效
  3. 房产企业如何借助K2 BPM脱颖而出?
  4. Hello world,Hello 2015,Bye 2014
  5. 在windows下使用linux的开发环境
  6. cocos2dx3.4 分割plist图片
  7. 使用XIB实现嵌套自定义视图
  8. Windows Server 2012安装IIS 8.0
  9. linux快捷键 常用快捷键
  10. 《Network Security A Decision and Game Theoretic Approach》阅读笔记
  11. git 命令(补充篇)的本质理解
  12. [React] 13 - Redux: react-redux
  13. Java Runnable与Callable区别
  14. Nodejs入门【转载】保留备用
  15. day21&lt;IO流+&amp;FIle递归&gt;
  16. Java 设计模式——单例模式
  17. 《Java并发编程实战》文摘
  18. [翻译] GSProgressView
  19. [转] Spark-Sql On YARN自动调整Executor数配置
  20. pager-taglib2.0中文传参乱码问题

热门文章

  1. HBase 安装设置
  2. web worker原理 &amp;&amp; SSE原理
  3. Css权重解析
  4. 解决python3与python2的pip命令冲突问题冲突(window版)
  5. mongodb-添加或删除字段
  6. java面试⑤前端部分
  7. nodejs --- 上传文件并保存到磁盘
  8. HTML5本地存储localStorage、sessionStorage基本用法、遍历操作、异常处理等
  9. @EnableAutoConfiguration和@SpringbootApplication注解
  10. 通过set赋值,与select赋值的区别