david2tdw / blog

学习记录
1 stars 1 forks source link

[CSS] retina屏下的1px线的实现 #153

Open david2tdw opened 4 years ago

david2tdw commented 4 years ago
  1. 法一:使用渐变实现,使用两种颜色填充 1px 宽内容
  2. 法二:使用缩放实现,对 1px 高度线条进行0.5倍缩放
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <title>retina屏下的1px线的实现</title>
    <style>
      div {
        width: 100vw;
        height: 80px;
        line-height: 80px;
        margin: 30px auto;
        background-color: rgba(0, 0, 0, 0.1);
        text-align: center;
        font-size: 12px;
        box-sizing: border-box;
      }

      .normal-border,
      .gradient-border,
      .scale-border {
        border-top: 1px solid #999;
      }
      /* 2倍屏 */
      @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-prxel-ratio: 2) {
        .gradient-border {
          background-image: linear-gradient(to top, transparent 50%, #999 50%);
          background-size: 100% 1px;
          background-repeat: no-repeat;
          background-position: top center;
          border-top: 0 none;
          padding-top: 1px;
        }
        .scale-border {
          position: relative;
          padding-top: 1px;
          border-top: 0 none;
        }
        .scale-border::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          width: 200%;
          border-top: 1px solid #999;
          transform: scaleY(0.5);
          transform-origin: 0 0;
        }
      }

      /* 3倍屏 */
      @media only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (min-device-prxel-ratio: 3) {
        .gradient-border {
          background-image: linear-gradient(to top, transparent 66.66%, #999 66.66%);
          background-size: 100% 1px;
          background-repeat: no-repeat;
          background-position: top center;
          border-top: 0 none;
          padding-top: 1px;
        }
        .scale-border {
          position: relative;
          padding-top: 1px;
          border-top: 0 none;
        }
        .scale-border::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          width: 200%;
          border-top: 1px solid #999;
          transform: scaleY(0.333);
          transform-origin: 0 0;
        }
      }
    </style>
  </head>
  <body>
    <p id="dpr"></p>
    <div class="normal-border">正常使用1px border效果(本DEMO请在移动端环境下查看)</div>
    <div class="gradient-border">法一:使用渐变实现,使用两种颜色填充 1px 宽内容</div>
    <div class="scale-border">法二:使用缩放实现,对 1px 高度线条进行0.5/0.33倍缩放</div>
  </body>
  <script>
    var dpr = window.devicePixelRatio
    document.querySelector('#dpr').innerText = `当前设备dpr:${dpr}`
  </script>
</html>

retina屏下的1px线的实现

david2tdw commented 4 years ago

方法一: background-size: 100% 1px; 使背景图片的高度变为1像素。

background-repeat: no-repeat; 不重复填充背景。

border-top: 0 none; 不生成上边线。

padding-top: 1px; 把顶部边线的背景色空出来。

方法二: transform: scaleY(0.5); 元素本身不绘制外边框,添加伪元素通过dpr查询进行外边框进行Y轴缩放。