原码笔记

原码笔记

css实现各种0.5px的线(小结)

小诸哥 0

在PC端用1px的边框线,看起来还好,但在手机端看起来就很难看了,而0.5px的分割线会有种精致的感觉。用普通写法border:solid 0.5px red;iPhone可以正常显示,android下几乎所有的浏览器都会把0.5识别为0,即无边框状态.

原理

原理就是给需要加边框的元素插入一个伪类,伪类采用绝对定位,然后对伪类添加1px边框,最后进行0.5倍缩放。

transform的缩放和旋转默认都是按照元素的中心点来操作的

outline元素在缩放0.5之前尺寸就是红框元素,缩放后,位置到了红框中心,为了使之依然在左上角,缩放之前我们需进行left:-50%;top:-50%的位移。

0.5px边框

  1. <div class="first">
  2.      <div class="first-div">
  3.      HELLO WORLD
  4.      </div>
  5. </div>
  6. <style>
  7. .first{
  8.      position: relative;
  9.      font-size: 16px;
  10. }
  11. .first .first-div:before{
  12.      content: "";
  13.      position: absolute;
  14.      top: -50%;
  15.      bottom: -50%;
  16.      left: -50%;
  17.      right: -50%;
  18.      width: 200%;
  19.      height: 200%;
  20.      -webkit-transform: scale(0.5);
  21.      transform: scale(0.5);
  22.      border: solid 1px red;
  23.      box-sizing:border-box;
  24. }
  25. </style>

副作用

当用伪类的绝对定位来实现了边框后,我们在first类和first-div类上的点击事件会失效,因为此时的伪类是绝对定位,而且长宽等于父类元素的长宽,是脱离了文档流覆盖在父类上的,伪类不是真实的DOM元素,没有js点击事件

解决方案

再写一个绝对定位元素,覆盖在父元素上,层级优先级要高一点

  1. <div class="first">
  2.      <div class="first-div">
  3.      HELLO WORLD
  4.      </div>
  5.      <div class="click-able" onclick="alert('click')"></div>
  6. </div>
  7. <style>
  8. .first{
  9.      position: relative;
  10.      font-size: 16px;
  11. }
  12. .first .first-div:before{
  13.      content: "";
  14.      position: absolute;
  15.      top: -50%;
  16.      bottom: -50%;
  17.      left: -50%;
  18.      right: -50%;
  19.      width: 200%;
  20.      height: 200%;
  21.      -webkit-transform: scale(0.5);
  22.      transform: scale(0.5);
  23.      border: solid 1px red;
  24.      box-sizing:border-box;
  25. }
  26. .click-able{
  27.      position: absolute;
  28.      top: 0;
  29.      bottom: 0;
  30.      left: 0;
  31.      right: 0;
  32.      z-index: 10;
  33. }
  34.      </style>

0.5px圆角边框

  1. <div class="round">
  2.      <div class="round-div">
  3.      HELLO WORLD
  4.      </div>
  5. </div>
  6. <style>
  7. .round{
  8.      position: relative;
  9.      font-size: 16px;
  10.      }
  11. .round .round-div:before{
  12.      content: "";
  13.      position: absolute;
  14.      top: -50%;
  15.      bottom: -50%;
  16.      left: -50%;
  17.      right: -50%;
  18.      width: 200%;
  19.      height: 200%;
  20.      -webkit-transform: scale(0.5);
  21.      transform: scale(0.5);
  22.      border: solid 1px red;
  23.      border-radius: 22px;
  24.      box-sizing:border-box;
  25. }
  26. </style>

0.5px左边线

  1. <div class="left">
  2.      <div class="left-div">
  3.      HELLO WORLD
  4.      </div>
  5. </div>
  6. <style>
  7. .left{
  8.      position: relative;
  9.      font-size: 16px;
  10. }
  11. .left .left-div:before{
  12.      content: " ";
  13.      position: absolute;
  14.      left: 0;
  15.      bottom: 0;
  16.      width: 1px;
  17.      height: 100%;
  18.      border-left: 1px solid red;
  19.      -webkit-transform-origin: 0 0;
  20.      transform-origin: 0 0;
  21.      -webkit-transform: scaleX(0.5);
  22.      transform: scaleX(0.5);
  23. }
  24. </style>

0.5px右边线

  1. <div class="right">
  2.      <div class="right-div">
  3.      HELLO WORLD
  4.      </div>
  5. </div>
  6. <style>
  7. .right{
  8.      position: relative;
  9.      font-size: 16px;
  10.      display: inline-block;
  11. }
  12. .right .right-div:before{
  13.      content: " ";
  14.      position: absolute;
  15.      right: 0;
  16.      bottom: 0;
  17.      width: 1px;
  18.      height: 100%;
  19.      border-right: 1px solid red;
  20.      -webkit-transform-origin: 0 0;
  21.      transform-origin: 0 0;
  22.      -webkit-transform: scaleX(0.5);
  23.      transform: scaleX(0.5);
  24. }
  25. </style>

0.5px底部线

  1. <div class="bottom">
  2.      <div class="bottom-div">
  3.      HELLO WORLD
  4.      </div>
  5. </div>
  6. <style>
  7. .bottom{
  8.      position: relative;
  9.      font-size: 16px;
  10. }
  11. .bottom .bottom-div:before{
  12.      content: " ";
  13.      position: absolute;
  14.      left: 0;
  15.      bottom: 0;
  16.      width: 100%;
  17.      height: 1px;
  18.      border-top: 1px solid red;
  19.      -webkit-transform-origin: 0 0;
  20.      transform-origin: 0 0;
  21.      -webkit-transform: scaleY(0.5);
  22.      transform: scaleY(0.5);
  23. }
  24. </style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

标签: 实现 各种